Skip to content

Instantly share code, notes, and snippets.

@theabbie

theabbie/index.js

Last active Sep 25, 2020
Embed
What would you like to do?
DevRant Wikipedia Bot
var app = require('express')();
var devRant = require("rantscript");
const Fs = require('fs')
const Path = require('path')
const Axios = require('axios')
const cheerio = require("cheerio")
var url = require("url");
async function load(url) {
const writer = Fs.createWriteStream("img.jpg")
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
})
response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
}
app.get("/*", async function (req, res) {
try {
var token = (await devRant.login('wiki', process.env.devpass))["auth_token"]
var notifs = await devRant.notifications(token);
await devRant.clearNotifications(token);
for (x of notifs.data.items) {
try {
if (x.type == "comment_mention" && x.read == 0) {
var rant = (await devRant.rant(x["rant_id"], token)).comments;
for (p of rant) {
if (p.id == x["comment_id"]) {
/*var img = cheerio.load((await Axios("https://www.google.com/search?tbm=isch&safe=active&q="+p.body.split("@wiki").reverse()[0])).data);
await load(img("img").eq(1)[0].attribs["src"]);*/
var $ = cheerio.load((await Axios("https://www.google.com/search?q=" + p.body.split("@wiki").reverse()[0].replace(/site:/gi, "") + " wikipedia")).data);
var wurl = url.parse($("a:has(h3)").eq(0)[0].attribs.href, true).query.q;
var $ = cheerio.load((await Axios(wurl)).data);
$("sup").remove();
var comment = await devRant.postComment("@" + p["user_username"] + " " + $("p:has(a)").eq(0).text() + "\n" + wurl, x["rant_id"], token);
}
}
}
} catch (e) {
continue;
}
}
res.json(comment)
} catch (e) {
res.end(e.message);
}
})
app.listen(process.env.PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.