2023-10-02 02:11:24 +00:00
|
|
|
|
const config = require('../config/default.json')
|
2023-10-02 02:11:27 +00:00
|
|
|
|
const uconfig = require('../config/usersettings.json')
|
2023-10-02 02:11:24 +00:00
|
|
|
|
const { parentPort, workerData } = require('worker_threads');
|
2023-10-02 02:11:27 +00:00
|
|
|
|
const { d1, d2, d3 } = workerData;
|
2023-10-02 02:11:26 +00:00
|
|
|
|
var provfeed = d1;
|
|
|
|
|
var n = d2;
|
2023-10-02 02:11:27 +00:00
|
|
|
|
var nick = d3;
|
2023-10-02 02:11:24 +00:00
|
|
|
|
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');
|
|
|
|
|
const timer = ms => new Promise(res => setTimeout(res, ms))
|
2023-10-02 02:11:24 +00:00
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
|
|
|
|
|
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
|
2023-10-02 02:11:25 +00:00
|
|
|
|
|
2023-10-02 02:11:26 +00:00
|
|
|
|
function consoleLog(log) {
|
|
|
|
|
if (config.misc.logging === "true") {
|
|
|
|
|
console.log(log)
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:24 +00:00
|
|
|
|
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) {
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog('[feed-preset.errorMessage] '+error.code)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
if (code == "404") {
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var error = errorMsg+" 404: " + extra + " not found"
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else if (error.code == "ECONNREFUSED") {
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var error = errorMsg+" Connection Refused"
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else if (error.code == "ERR_UNESCAPED_CHARACTERS"){
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var error = errorMsg+" Unescaped Characters"
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else {
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var error = errorMsg+" Unknown error"
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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:26 +00:00
|
|
|
|
consoleLog('[feed-preset.fetchFeed] Fetching '+feedURL)
|
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;
|
2023-10-02 02:11:25 +00:00
|
|
|
|
content.push(warningMsg+" 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) {
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var n = config.feed.default_amount
|
|
|
|
|
content.push(warningMsg+" You requested a number less than 1. Reverting to default ("+config.feed.default_amount+")");
|
2023-10-02 02:11:24 +00:00
|
|
|
|
}
|
2023-10-02 02:11:24 +00:00
|
|
|
|
//for (let i = 0; i < newFeed.items.length; i++) {
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
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)
|
2023-10-02 02:11:27 +00:00
|
|
|
|
if (uconfig[nick].timezone != undefined) {
|
|
|
|
|
var syncDate = date.tz(uconfig[nick].timezone)
|
|
|
|
|
} else {
|
|
|
|
|
var syncDate = date.tz(config.feed.timezone)
|
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
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
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
date = ''+config.colours.brackets+'['+config.colours.date+date+''+config.colours.brackets+'] '
|
2023-10-02 02:11:25 +00:00
|
|
|
|
title = ''+config.colours.title+title+' '
|
|
|
|
|
author = ''+config.colours.author+data.creator+' '
|
|
|
|
|
body = ''+config.colours.body+body+' '
|
|
|
|
|
link = ''+config.colours.link+data.link+' '
|
|
|
|
|
|
|
|
|
|
//console.log(data);
|
|
|
|
|
//var string = "15[11" + date + "15] 08" + title + " " + body + " " + data.link;
|
|
|
|
|
var string = date+title+body+link;
|
|
|
|
|
content.push(string)
|
2023-10-02 02:11:24 +00:00
|
|
|
|
}
|
|
|
|
|
sendUpstream(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchFeed(provfeed, n);
|