2023-10-02 02:11:24 +00:00
|
|
|
|
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");
|
2023-10-02 02:11:25 +00:00
|
|
|
|
const moment = require('moment');
|
|
|
|
|
const tz = require('moment-timezone');
|
2023-10-02 02:11:24 +00:00
|
|
|
|
const timer = ms => new Promise(res => setTimeout(res, ms))
|
|
|
|
|
|
|
|
|
|
async function sendUpstream(content) {
|
|
|
|
|
var output = content.join("\n")
|
|
|
|
|
parentPort.postMessage(output);
|
|
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
function errorMessage(error, code, extra) {
|
|
|
|
|
console.log(error.code)
|
|
|
|
|
if (code == "404") {
|
|
|
|
|
var error = "[04ERROR] 404: " + extra + " not found"
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else if (error.code == "ECONNREFUSED") {
|
|
|
|
|
var error = "[04ERROR] Connection Refused"
|
|
|
|
|
} else if (error.code == "ERR_UNESCAPED_CHARACTERS"){
|
|
|
|
|
var error = "[04ERROR] Unescaped Characters"
|
|
|
|
|
} else {
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var error = "[04ERROR] Unknown error"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parentPort.postMessage(error);
|
|
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:24 +00:00
|
|
|
|
async function fetchFeed(feedURL, n) {
|
|
|
|
|
var content = [];
|
2023-10-02 02:11:25 +00:00
|
|
|
|
|
|
|
|
|
if (feedURL.startsWith('@') == true) {
|
|
|
|
|
var feedURL = feedURL.substring(1,feedURL.length);
|
|
|
|
|
}
|
2023-10-02 02:11:24 +00:00
|
|
|
|
var randomNitter = config.twitter.nitter_instances[Math.floor(Math.random() * config.twitter.nitter_instances.length)];
|
|
|
|
|
var feedURL = "https://" + randomNitter + "/" + feedURL + "/rss"
|
2023-10-02 02:11:25 +00:00
|
|
|
|
try {
|
|
|
|
|
var newFeed = await parser.parseURL(feedURL);
|
|
|
|
|
} catch (e) {
|
2023-10-02 02:11:25 +00:00
|
|
|
|
if (e.code !== undefined) {
|
|
|
|
|
errorMessage(e)
|
|
|
|
|
} else {
|
|
|
|
|
errorMessage(e, "404", feedURL);
|
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:24 +00:00
|
|
|
|
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);
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else if (n < 1) {
|
|
|
|
|
var n = 5
|
|
|
|
|
content.push("[08WARNING] You requested a number less than 1. Reverting to 5");
|
2023-10-02 02:11:24 +00:00
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
|
2023-10-02 02:11:24 +00:00
|
|
|
|
for (let i = 0; i < n; i++) {
|
2023-10-02 02:11:25 +00:00
|
|
|
|
|
2023-10-02 02:11:24 +00:00
|
|
|
|
await timer(50);
|
2023-10-02 02:11:25 +00:00
|
|
|
|
|
2023-10-02 02:11:24 +00:00
|
|
|
|
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);
|
2023-10-02 02:11:25 +00:00
|
|
|
|
|
|
|
|
|
if (data.isoDate !== undefined) {
|
|
|
|
|
var date = moment(data.isoDate)
|
|
|
|
|
var syncDate = date.tz(config.feed.timezone)
|
|
|
|
|
console.log(syncDate.format())
|
|
|
|
|
var date = syncDate.format(config.feed.time_format)
|
|
|
|
|
} else {
|
|
|
|
|
var date = data.pubDate
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:24 +00:00
|
|
|
|
if (body.length >= config.feed.body_max_chars) {
|
|
|
|
|
var truncatedString = body.substring(0,config.feed.body_max_chars);
|
|
|
|
|
var body = truncatedString + "..."
|
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
//console.log(data);
|
|
|
|
|
var string = "15[11" + date + "15] 08" + data.creator + " " + body + " " + data.link;
|
2023-10-02 02:11:24 +00:00
|
|
|
|
var output = string;
|
|
|
|
|
content.push(output)
|
|
|
|
|
}
|
|
|
|
|
sendUpstream(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchFeed(provfeed, n);
|