forked from rb28z2/progress-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss.js
53 lines (46 loc) · 1.32 KB
/
rss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// eslint-disable-next-line no-unused-vars
const Parser = require("rss-parser");
const parser = new Parser();
import config from "./config.js";
import { runCommand } from "./common.js";
import { ircSay } from "./irc.js";
import { discordSay } from "./discord.js";
let lastUpdated = new Date(0);
export function initRSS() {
parser.parseURL(config.rssFeed).then((feed) => {
for (let item of feed.items) {
const pubDate = new Date(item.pubDate);
if (pubDate > lastUpdated) {
lastUpdated = pubDate;
}
}
setInterval(readRSS, config.rssInterval * 1000);
}, (error) => {
console.log("Initializing RSS feeds failed.".red);
console.log(error);
})
}
function getIRCMessage(item) {
return `New post: \u0002${item.title}\u0002 | ${item.link}`;
}
function getDiscordMessage(item) {
return `New post: **${item.title}** | ${item.link}`;
}
function readRSS() {
parser.parseURL(config.rssFeed).then((feed) => {
let tmpLastUpdated = lastUpdated;
for (let item of feed.items) {
const pubDate = new Date(item.pubDate);
if (pubDate > lastUpdated) {
if (pubDate > tmpLastUpdated) {
tmpLastUpdated = pubDate;
}
if (config.enableIrc) ircSay(getIRCMessage(item));
if (config.enableDiscord) discordSay(getDiscordMessage(item));
}
}
lastUpdated = tmpLastUpdated;
}, (error) => {
console.log(error);
});
}