time zone support + some error handling

This commit is contained in:
hgw 2023-10-02 02:11:25 +00:00
parent a0ec99175b
commit d491326e75
5 changed files with 113 additions and 9 deletions

View File

@ -6,6 +6,9 @@ let parser = new Parser({
headers: {'User-Agent': config.feed.useragent}, headers: {'User-Agent': config.feed.useragent},
}); });
const striptags = require("striptags"); const striptags = require("striptags");
const moment = require('moment');
const tz = require('moment-timezone');
const timer = ms => new Promise(res => setTimeout(res, ms))
async function sendUpstream(content) { async function sendUpstream(content) {
var output = content.join("\n") var output = content.join("\n")
@ -13,12 +16,31 @@ async function sendUpstream(content) {
process.exit() process.exit()
} }
function errorMessage(error, code, extra) {
console.log(error.code)
if (code == "404") {
var error = "[04ERROR] 404: " + extra + " not found"
} else {
var error = "[04ERROR] Unknown error"
}
parentPort.postMessage(error);
process.exit()
}
async function fetchFeed(feedURL, n) { async function fetchFeed(feedURL, n) {
var content = []; var content = [];
let newFeed = await parser.parseURL(feedURL); try {
var newFeed = await parser.parseURL(feedURL);
} catch (e) {
errorMessage(e, "404", feedURL);
}
if (n > newFeed.items.length) { if (n > newFeed.items.length) {
var 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); content.push("[08WARNING] Your requested post amount exceeded the total available. Reverting to " + newFeed.items.length);
} else if (n < 1) {
var n = 5
content.push("[08WARNING] You requested a number less than 1. Reverting to 5");
} }
//for (let i = 0; i < newFeed.items.length; i++) { //for (let i = 0; i < newFeed.items.length; i++) {
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
@ -29,12 +51,20 @@ async function fetchFeed(feedURL, n) {
var body = data.contentSnippet.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks var body = data.contentSnippet.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks
.replace(/\s{2,}/g, ' ') //idk .replace(/\s{2,}/g, ' ') //idk
var body = striptags(body); var body = striptags(body);
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
}
if (body.length >= config.feed.body_max_chars) { if (body.length >= config.feed.body_max_chars) {
var truncatedString = body.substring(0,config.feed.body_max_chars); var truncatedString = body.substring(0,config.feed.body_max_chars);
var body = truncatedString + "..." var body = truncatedString + "..."
} }
console.log(data); console.log(data);
var string = "15[11" + data.pubDate + "15] - 08" + title + " - " + body + " - " + data.link; var string = "15[11" + date + "15] 08" + title + " " + body + " " + data.link;
var output = string; var output = string;
content.push(output) content.push(output)
} }

View File

@ -6,28 +6,54 @@ let parser = new Parser({
headers: {'User-Agent': config.feed.useragent}, headers: {'User-Agent': config.feed.useragent},
}); });
const striptags = require("striptags"); const striptags = require("striptags");
const moment = require('moment');
const tz = require('moment-timezone');
const timer = ms => new Promise(res => setTimeout(res, ms)) const timer = ms => new Promise(res => setTimeout(res, ms))
async function sendUpstream(content) { async function sendUpstream(content) {
var output = content.join("\n") var output = content.join("\n")
parentPort.postMessage(output); parentPort.postMessage(output);
process.exit() process.exit()
} }
function errorMessage(error, code, extra) {
console.log(error.code)
if (code == "404") {
var error = "[04ERROR] 404: " + extra + " not found"
} else {
var error = "[04ERROR] Unknown error"
}
parentPort.postMessage(error);
process.exit()
}
async function fetchFeed(feedURL, n) { async function fetchFeed(feedURL, n) {
var content = []; var content = [];
if (feedURL.startsWith('@') == true) {
var feedURL = feedURL.substring(1,feedURL.length);
}
var randomNitter = config.twitter.nitter_instances[Math.floor(Math.random() * config.twitter.nitter_instances.length)]; var randomNitter = config.twitter.nitter_instances[Math.floor(Math.random() * config.twitter.nitter_instances.length)];
var feedURL = "https://" + randomNitter + "/" + feedURL + "/rss" var feedURL = "https://" + randomNitter + "/" + feedURL + "/rss"
let newFeed = await parser.parseURL(feedURL); try {
console.log(newFeed.items.length); var newFeed = await parser.parseURL(feedURL);
} catch (e) {
errorMessage(e, "404", feedURL);
}
if (n > newFeed.items.length) { if (n > newFeed.items.length) {
var 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); content.push("[08WARNING] Your requested post amount exceeded the total available. Reverting to " + newFeed.items.length);
} else if (n < 1) {
var n = 5
content.push("[08WARNING] You requested a number less than 1. Reverting to 5");
} }
//for (let i = 0; i < newFeed.items.length; i++) {
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
await timer(50); await timer(50);
var data = newFeed.items[i] var data = newFeed.items[i]
var title = data.title.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks var title = data.title.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks
.replace(/\s{2,}/g, ' ') //idk .replace(/\s{2,}/g, ' ') //idk
@ -35,12 +61,22 @@ async function fetchFeed(feedURL, n) {
var body = data.contentSnippet.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks var body = data.contentSnippet.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks
.replace(/\s{2,}/g, ' ') //idk .replace(/\s{2,}/g, ' ') //idk
var body = striptags(body); var body = striptags(body);
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
}
if (body.length >= config.feed.body_max_chars) { if (body.length >= config.feed.body_max_chars) {
var truncatedString = body.substring(0,config.feed.body_max_chars); var truncatedString = body.substring(0,config.feed.body_max_chars);
var body = truncatedString + "..." var body = truncatedString + "..."
} }
console.log(data); //console.log(data);
var string = "15[11" + data.pubDate + "15] 08" + data.creator + " " + body + " " + data.link; var string = "15[11" + date + "15] 08" + data.creator + " " + body + " " + data.link;
var output = string; var output = string;
content.push(output) content.push(output)
} }

View File

@ -15,7 +15,9 @@
}, },
"feed": { "feed": {
"useragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 13.4; rv:109.0) Gecko/20100101 Firefox/114.0", "useragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 13.4; rv:109.0) Gecko/20100101 Firefox/114.0",
"body_max_chars": "200" "body_max_chars": "200",
"time_format": "DD MMM YYYY HH:mm:ss ZZ",
"timezone": "Pacific/Auckland"
}, },
"twitter": { "twitter": {
"nitter_instances": [ "nitter_instances": [

34
package-lock.json generated
View File

@ -12,6 +12,8 @@
"config": "^3.3.9", "config": "^3.3.9",
"fs": "^0.0.1-security", "fs": "^0.0.1-security",
"irc": "^0.5.2", "irc": "^0.5.2",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"rss-parser": "^3.13.0", "rss-parser": "^3.13.0",
"striptags": "^3.2.0" "striptags": "^3.2.0"
} }
@ -87,6 +89,25 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/moment": {
"version": "2.29.4",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
"integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==",
"engines": {
"node": "*"
}
},
"node_modules/moment-timezone": {
"version": "0.5.43",
"resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.43.tgz",
"integrity": "sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==",
"dependencies": {
"moment": "^2.29.4"
},
"engines": {
"node": "*"
}
},
"node_modules/nan": { "node_modules/nan": {
"version": "2.17.0", "version": "2.17.0",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz", "resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz",
@ -194,6 +215,19 @@
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
"integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==" "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg=="
}, },
"moment": {
"version": "2.29.4",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
"integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w=="
},
"moment-timezone": {
"version": "0.5.43",
"resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.43.tgz",
"integrity": "sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==",
"requires": {
"moment": "^2.29.4"
}
},
"nan": { "nan": {
"version": "2.17.0", "version": "2.17.0",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz", "resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz",

View File

@ -7,6 +7,8 @@
"config": "^3.3.9", "config": "^3.3.9",
"fs": "^0.0.1-security", "fs": "^0.0.1-security",
"irc": "^0.5.2", "irc": "^0.5.2",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"rss-parser": "^3.13.0", "rss-parser": "^3.13.0",
"striptags": "^3.2.0" "striptags": "^3.2.0"
}, },