-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathfooduse-monitor.js
126 lines (98 loc) · 3.66 KB
/
fooduse-monitor.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const MiniDb = require('./minidb');
const FeedParser = require('feedparser');
const request = require('request');
const config = require('./config');
const DiscordChannelSync = require("./discord-channel-sync");
class FooduseMonitor {
static start() {
if (!config.discord_fooduse_channel) {
// Fooduse integration is disabled (no channel configured)
return;
}
// Db init
this.db = new MiniDb('fooduse-monitor');
// Db read
this.lastAnnouncedVideoGuid = null;
try {
const dbRecord = this.db.get("fooduse");
if (dbRecord) {
this.lastAnnouncedVideoGuid = dbRecord.lastAnnouncedVideoGuid;
}
} catch (e) { }
// Loop
this.interval = setInterval(() => {
this.doCheck();
}, FooduseMonitor.YT_CHECK_INTERVAL_SECS * 1000);
this.doCheck();
}
static doCheck() {
let fpReq = request(this.feedUrl);
let fp = new FeedParser();
fpReq.on('error', (err) => {
console.error('[Fooduse]', 'Could not check Youtube feed (request error).', err);
});
fpReq.on('response', function (res) {
let stream = this;
if (res.statusCode === 200) {
stream.pipe(fp);
}
});
fp.on('error', (err) => {
console.error('[Fooduse]', 'Could not check Youtube feed (parser error).', err);
});
let highestDate = null;
let highestItem = null;
fp.on('readable', function() {
let stream = this;
let meta = this.meta;
let item;
while (item = stream.read()) {
let itemDate = Date.parse(item.pubdate);
if (highestDate == null || itemDate > highestItem) {
highestItem = item;
highestDate = itemDate;
}
}
});
fp.on('end', function() {
if (highestItem) {
FooduseMonitor.handleHighestItem(highestItem);
}
});
}
static handleHighestItem(rssItem) {
let lastGuid = this.lastAnnouncedVideoGuid;
let thisGuid = rssItem.guid;
if (lastGuid && lastGuid === thisGuid) {
return;
}
console.debug('[Fooduse]', `Found new video to announce: ${rssItem.title} [${rssItem.guid}]`);
this.db.put("fooduse", { lastAnnouncedVideoGuid: thisGuid });
this.lastAnnouncedVideoGuid = thisGuid;
this.doAnnounce(rssItem);
}
static doAnnounce(rssItem) {
this.targetChannels = DiscordChannelSync.getChannelList(global.discordJsClient,
config.discord_fooduse_channel, false);
let emojiTxtBob = global.getServerEmoji("BOB_USE", true);
let formattedMessage = `${rssItem.link} ${emojiTxtBob}`;
for (let i = 0; i < this.targetChannels.length; i++) {
let targetChannel = this.targetChannels[i];
if (targetChannel) {
try {
targetChannel.send(formattedMessage);
} catch (err) {
console.error('[Fooduse]', 'Announce error:', err);
}
}
}
}
static get feedUrl() {
let cacheBuster = Date.now();
return `${FooduseMonitor.YT_FEED_BASE_URL}?channel_id=${FooduseMonitor.YT_CHANNEL_ID}&orderby=published&_cacheBust=${cacheBuster}`;
}
}
FooduseMonitor.YT_CHANNEL_ID = "UCCuIpl5564hhP8Qpucvu7RA";
FooduseMonitor.YT_FEED_BASE_URL = "https://www.youtube.com/feeds/videos.xml";
FooduseMonitor.YT_CHECK_INTERVAL_SECS = 10 * 60;
module.exports = FooduseMonitor;