Last active
September 25, 2020 06:08
-
-
Save theabbie/233d3b9386838a28ca9499722b08e162 to your computer and use it in GitHub Desktop.
DevRant Wikipedia Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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