From d491326e75b030e6ef0266f1d10b34d5db465161 Mon Sep 17 00:00:00 2001 From: hgw Date: Mon, 2 Oct 2023 02:11:25 +0000 Subject: [PATCH] time zone support + some error handling --- commands/feed.js | 34 ++++++++++++++++++++++++-- commands/twitter.js | 48 ++++++++++++++++++++++++++++++++----- config/example.default.json | 4 +++- package-lock.json | 34 ++++++++++++++++++++++++++ package.json | 2 ++ 5 files changed, 113 insertions(+), 9 deletions(-) diff --git a/commands/feed.js b/commands/feed.js index c09ea45..93460ca 100644 --- a/commands/feed.js +++ b/commands/feed.js @@ -6,6 +6,9 @@ let parser = new Parser({ headers: {'User-Agent': config.feed.useragent}, }); 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) { var output = content.join("\n") @@ -13,12 +16,31 @@ async function sendUpstream(content) { 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) { 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) { var n = 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++) { @@ -29,12 +51,20 @@ async function fetchFeed(feedURL, n) { var body = data.contentSnippet.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks .replace(/\s{2,}/g, ' ') //idk 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) { var truncatedString = body.substring(0,config.feed.body_max_chars); var body = truncatedString + "..." } 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; content.push(output) } diff --git a/commands/twitter.js b/commands/twitter.js index 7762192..8608dc1 100644 --- a/commands/twitter.js +++ b/commands/twitter.js @@ -6,28 +6,54 @@ let parser = new Parser({ headers: {'User-Agent': config.feed.useragent}, }); 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) { var output = content.join("\n") parentPort.postMessage(output); 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) { 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 feedURL = "https://" + randomNitter + "/" + feedURL + "/rss" - let newFeed = await parser.parseURL(feedURL); - console.log(newFeed.items.length); + try { + var newFeed = await parser.parseURL(feedURL); + } catch (e) { + errorMessage(e, "404", feedURL); + } + 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); + } 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++) { + 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 @@ -35,12 +61,22 @@ async function fetchFeed(feedURL, n) { var body = data.contentSnippet.replace(/(\r\n|\n|\r)/gm, " ") //remove line breaks .replace(/\s{2,}/g, ' ') //idk 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) { 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; + //console.log(data); + var string = "15[11" + date + "15] 08" + data.creator + " " + body + " " + data.link; var output = string; content.push(output) } diff --git a/config/example.default.json b/config/example.default.json index a177a95..8a1eaff 100644 --- a/config/example.default.json +++ b/config/example.default.json @@ -15,7 +15,9 @@ }, "feed": { "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": { "nitter_instances": [ diff --git a/package-lock.json b/package-lock.json index d970ff3..9b6a1a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,8 @@ "config": "^3.3.9", "fs": "^0.0.1-security", "irc": "^0.5.2", + "moment": "^2.29.4", + "moment-timezone": "^0.5.43", "rss-parser": "^3.13.0", "striptags": "^3.2.0" } @@ -87,6 +89,25 @@ "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": { "version": "2.17.0", "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", "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": { "version": "2.17.0", "resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz", diff --git a/package.json b/package.json index bb04dc7..09422ac 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "config": "^3.3.9", "fs": "^0.0.1-security", "irc": "^0.5.2", + "moment": "^2.29.4", + "moment-timezone": "^0.5.43", "rss-parser": "^3.13.0", "striptags": "^3.2.0" },