revamping structure

This commit is contained in:
Michael (GP) 2022-12-14 12:41:23 -05:00
parent f8e26275db
commit 999d7e27b7

73
bot.js
View File

@ -11,6 +11,12 @@ let Parser = require("rss-parser");
let parser = new Parser(); let parser = new Parser();
let maxPostPerScan = process.env.MAX_POST_PER_SCAN; let maxPostPerScan = process.env.MAX_POST_PER_SCAN;
const M = new Mastodon({
access_token: `${process.env.MASTODON_ACCESS_KEY}`,
timeout_ms: 60 * 1000, // optional HTTP request timeout to apply to all requests.
api_url: `${process.env.MASTODON_API_URL}`,
});
const download_image = async (url, image_path) => { const download_image = async (url, image_path) => {
let response = await axios({ let response = await axios({
url, url,
@ -32,38 +38,44 @@ const download_image = async (url, image_path) => {
}; };
(async () => { (async () => {
await postFeed(); runBot();
setInterval(async () => { setInterval(async () => {
console.log("Starting postFeed()"); await runBot();
await postFeed();
console.log("Completed postFeed()");
}, 60 * 60 * 1000); }, 60 * 60 * 1000);
})(); })();
async function postFeed() { async function getLastPostDate() {
const M = new Mastodon({
access_token: `${process.env.MASTODON_ACCESS_KEY}`,
timeout_ms: 60 * 1000, // optional HTTP request timeout to apply to all requests.
api_url: `${process.env.MASTODON_API_URL}`,
});
let feed = await parser.parseURL("http://feeds.feedburner.com/ign/games-all");
let timeline = await M.get( let timeline = await M.get(
`accounts/${process.env.MASTODON_ACCOUNT_ID}/statuses`, `accounts/${process.env.MASTODON_ACCOUNT_ID}/statuses`,
{} {}
); );
let postDate = new Date(timeline.data[0].created_at); let postDate = new Date(timeline.data[0].created_at);
return postDate;
}
async function readFeeds() {
console.log("Processing Feeds: readFeeds()");
let feed = await parser.parseURL("http://feeds.feedburner.com/ign/games-all");
return feed;
}
async function processFeed(feed, postDate) {
let count = 0; let count = 0;
return feed.items.every(async (item) => { let validFeeds = feed.items
let pubDate = new Date(item.pubDate); .filter(async (item) => {
let pubDate = new Date(item.pubDate);
if (pubDate > postDate) { if (pubDate > postDate) {
let currentCount = ++count; return item;
}
})
.slice(0, maxPostPerScan);
if (currentCount > maxPostPerScan) return false; return Promise.all(
validFeeds.map(async (item) => {
let currentCount = count++;
let metadata = await urlMetadata(item.link); let metadata = await urlMetadata(item.link);
@ -79,15 +91,28 @@ async function postFeed() {
file: fs.createReadStream(path), file: fs.createReadStream(path),
}); });
await M.post("statuses", { return M.post("statuses", {
status: `${item.title}\n\n#NeoVibe #${process.env.POST_HASHTAG}\n\n${item.link}`, status: `${
item.contentSnippet ? item.contentSnippet : item.title
}\n\n#NeoVibe #${process.env.POST_HASHTAG}\n\n${item.link}`,
media_ids: [mediaup.data.id], media_ids: [mediaup.data.id],
}); });
return true; })
} );
}
return true; async function runBot() {
}); console.log("Running Bot: runBot()");
let feed = await readFeeds();
let postDate = await getLastPostDate();
let processedFeed = await processFeed(feed, postDate);
console.log("Completed Running Bot: runBot()");
return processedFeed;
} }
const requestListener = function (req, res) { const requestListener = function (req, res) {