twitter command

This commit is contained in:
hgw 2023-10-02 02:11:24 +00:00
parent f5217bbdab
commit 25adb4ccd9
2 changed files with 76 additions and 0 deletions

26
bot.js
View File

@ -23,6 +23,11 @@ async function help(chan, sub) {
var sub = "default"
}
if (sub === "default") {
bot.say(chan, ' ____ ___ ___ ____________ _________ __')
bot.say(chan, ' / __ `__ \\/ _ \\/ ___/ ___/ / / / ___/ / / /')
bot.say(chan, ' / / / / / / __/ / / /__/ /_/ / / / /_/ / ')
bot.say(chan, '/_/ /_/ /_/\\___/_/ \\___/\\__,_/_/ \\__, / ')
bot.say(chan, ' /____/ ')
bot.say(chan, 'Mercury - https://git.supernets.org/hogwart7/mercury')
bot.say(chan, 'm!feed [FEED] [ENTRIES] - Return the last x amount of entries from any RSS feed')
bot.say(chan, "m!set [OPTION] [VALUE] - run r!help set for details")
@ -48,12 +53,33 @@ async function feed(chan, provfeed, n) {
});
}
async function twitter(chan, provfeed, n) {
if (provfeed === undefined) {
bot.say(chan, "No account has been provided.")
}
if (n === undefined) {
var n = 5;
}
const worker = new Worker('./commands/twitter.js', {
workerData: {
provfeed,
n
}
});
worker.once('message', (string) => {
console.log('Received output from twitter worker, posting.');
bot.say(chan, string);
});
}
bot.addListener('message', function(nick, to, text, from) {
var args = text.split(' ');
if (args[0] === 'm!help') {
help(to, args[1]);
} else if (args[0] === 'm!feed') {
feed(to, args[1], args[2]);
} else if (args[0] === 'm!twitter') {
twitter(to, args[1], args[2])
}
});

50
commands/twitter.js Normal file
View File

@ -0,0 +1,50 @@
const config = require('../config/default.json')
const { parentPort, workerData } = require('worker_threads');
const { provfeed, n } = workerData;
let Parser = require('rss-parser');
let parser = new Parser({
headers: {'User-Agent': config.feed.useragent},
});
const striptags = require("striptags");
const timer = ms => new Promise(res => setTimeout(res, ms))
async function sendUpstream(content) {
var output = content.join("\n")
parentPort.postMessage(output);
process.exit()
}
async function fetchFeed(feedURL, n) {
var content = [];
var randomNitter = config.twitter.nitter_instances[Math.floor(Math.random() * config.twitter.nitter_instances.length)];
var feedURL = "https://" + randomNitter + "/" + feedURL + "/rss"
let newFeed = await parser.parseURL(feedURL);
console.log(newFeed.items.length);
if (n > newFeed.items.length) {
var n = newFeed.items.length;
content.push("[08WARNING] Your requested post amount exceeded the total available. Reverting to " + newFeed.items.length);
}
//for (let i = 0; i < newFeed.items.length; i++) {
for (let i = 0; i < n; i++) {
await timer(50);
var data = newFeed.items[i]
var title = data.title.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks
.replace(/\s{2,}/g, ' ') //idk
var title = striptags(title);
var body = data.contentSnippet.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks
.replace(/\s{2,}/g, ' ') //idk
var body = striptags(body);
if (body.length >= config.feed.body_max_chars) {
var truncatedString = body.substring(0,config.feed.body_max_chars);
var body = truncatedString + "..."
}
console.log(data);
var string = "15[11" + data.pubDate + "15] 08" + data.creator + " " + body + " " + data.link;
var output = string;
content.push(output)
}
sendUpstream(content);
}
fetchFeed(provfeed, n);