neovibe-bots/bot.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-11-23 15:30:51 -05:00
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
let Mastodon = require("mastodon-api");
let Parser = require("rss-parser");
let parser = new Parser();
2022-11-23 16:34:57 -05:00
let maxPostPerScan = process.env.MAX_POST_PER_SCAN;
2022-11-23 15:30:51 -05:00
(async () => {
2022-11-29 09:45:49 -05:00
console.log("Starting Bot");
2022-11-23 16:34:57 -05:00
await postFeed();
setInterval(async () => {
await postFeed();
2022-11-23 19:12:40 -05:00
}, 60 * 60 * 1000);
2022-11-23 16:34:57 -05:00
})();
async function postFeed() {
2022-11-29 09:45:49 -05:00
console.log("Running postFeed()");
2022-11-23 15:30:51 -05:00
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}`,
});
2022-11-23 17:25:52 -05:00
let feed = await parser.parseURL("http://feeds.feedburner.com/ign/games-all");
2022-11-23 15:30:51 -05:00
let timeline = await M.get(
`accounts/${process.env.MASTODON_ACCOUNT_ID}/statuses`,
{}
);
var postDate = new Date(timeline.data[0].created_at);
2022-11-23 16:34:57 -05:00
let count = 0;
feed.items.every(async (item) => {
2022-11-23 15:30:51 -05:00
let pubDate = new Date(item.pubDate);
if (pubDate > postDate) {
2022-11-23 16:34:57 -05:00
count++;
2022-11-28 09:43:44 -05:00
if (count > maxPostPerScan) return false;
2022-11-23 16:34:57 -05:00
await M.post("statuses", {
2022-11-23 16:02:14 -05:00
status: `${item.title}\n\n#NeoVibe #${process.env.POST_HASHTAG}\n\n${item.link}`,
2022-11-23 15:30:51 -05:00
});
2022-11-23 16:34:57 -05:00
return true;
2022-11-23 15:30:51 -05:00
}
2022-11-23 16:34:57 -05:00
return true;
2022-11-23 15:30:51 -05:00
});
2022-11-23 16:34:57 -05:00
}