enwnbot

Converts MediaWiki [[links]] and {{templates}} to links on IRC
git clone http://git.hanabi.in/repos/enwnbot.git
Log | Files | Refs | README | LICENSE

commit fcc56e7af3fcab78cfca8c6c4864dea83f5c1a17
parent 66cfef5ab7313aed1d06c030be52271f7bd67b01
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Thu, 28 May 2020 10:35:31 +0530

Add /RQ option

Diffstat:
Mconfig.js | 4++++
Mirc.js | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mpackage.json | 3++-
Myarn.lock | 5+++++
4 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/config.js b/config.js @@ -4,7 +4,11 @@ const config = { channels: ['#foo', '##bar'], maintainers: ['ssmith'], report: '!ADMIN', + RQAPI: + 'https://en.wikinews.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Review&format=json', server: 'irc.freenode.net', + shortURL: + 'https://meta.wikimedia.org/w/api.php?action=shortenurl&format=json&url=', URL: 'https://en.wikinews.org/w/index.php?title=', }; diff --git a/irc.js b/irc.js @@ -1,4 +1,5 @@ const irc = require('irc'); +const fetch = require('node-fetch'); const { admins, @@ -6,7 +7,9 @@ const { channels, maintainers, report, + RQAPI, server, + shortURL, URL, } = require('./config'); @@ -49,9 +52,66 @@ function handleTemplate(template, channel) { client.say(channel, `${URL}Template:${word}`); } +async function fetchData(URI) { + const res = {}; + try { + const data = await fetch(URI); + const parsed = await data.json(); + res.list = parsed.query.categorymembers; + } catch (error) { + res.error = true; + } + return res; +} + +async function urlShortener(URI) { + const req = `${shortURL}${URI}`; + const res = {}; + try { + const data = await fetch(req, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + }); + const parsed = await data.json(); + res.url = parsed.shortenurl.shorturl; + } catch (error) { + res.err = true; + } + return res; +} + +async function announceRQ(from, channel) { + const data = await fetchData(RQAPI); + if (data.error) + client.say( + channel, + `Error occurred, ${from}. Try this instead: "[[CAT:REV]]"` + ); + else { + const { list } = data; + if (!list.length) client.say(channel, `Review queue is empty, ${from}.`); + else { + client.say( + channel, + `${list.length} articles to review, ${from}. They are:` + ); + const urls = list.map(({ title }) => { + const fmtTitle = title.replace(/ /g, '%20'); + return `${URL}${fmtTitle}`; + }); + const shortUrls = await Promise.all(urls.map(urlShortener)); + shortUrls.forEach(({ url }) => { + client.say(channel, url); + }); + } + } +} + function groupChat(from, to, msg) { - if (msg.toLowerCase().includes(`thanks ${botName}`)) + const lcMsg = msg.toLowerCase(); + if (lcMsg.includes(`thanks ${botName}`)) client.say(to, `You are welcome, ${from}.`); + if (msg.includes(`${botName} /RQ`)) announceRQ(from, to); const regex1 = /\[{2}(.*?)\]{2}/g; const regex2 = /\{{2}(.*?)\}{2}/g; const links = msg.match(regex1); @@ -65,4 +125,3 @@ function groupChat(from, to, msg) { client.addListener('error', err); client.addListener('pm', pm); client.addListener('message', groupChat); -// channels.forEach(client.join); diff --git a/package.json b/package.json @@ -1,6 +1,7 @@ { "dependencies": { - "irc": "^0.5.2" + "irc": "^0.5.2", + "node-fetch": "^2.6.0" }, "scripts": { "start": "node irc.js" diff --git a/yarn.lock b/yarn.lock @@ -29,6 +29,11 @@ nan@^2.3.3, nan@^2.3.5: resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.1.tgz#d7be34dfa3105b91494c3147089315eff8874b01" integrity sha512-isWHgVjnFjh2x2yuJ/tj3JbwoHu3UC2dX5G/88Cm24yB6YopVgxvBObDY7n5xW6ExmFhJpSEQqFPvq9zaXc8Jw== +node-fetch@^2.6.0: + version "2.6.0" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd" + integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA== + node-icu-charset-detector@~0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/node-icu-charset-detector/-/node-icu-charset-detector-0.2.0.tgz#c2320da374ddcb671fc54cb4a0e041e156ffd639"