-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (45 loc) · 1.97 KB
/
Copy pathindex.js
File metadata and controls
56 lines (45 loc) · 1.97 KB
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
const Parser = require('rss-parser');
const fs = require('fs');
const helpers = require('./helpers');
const configPath = __dirname + '/config.json';
//TODO - add error handling for RSS feed not being available.
(async () => {
const config = JSON.parse(fs.readFileSync(configPath));
const discordSender = new helpers.DiscordSender(config.discordWebhookUrl);
/**
* Reddit RSS feed is odd... RSS feeds SHOULD be ordered chronologically.
* The timestamp in the feed is actually an UPDATED value rather then the
* time of inception.
* Unless we keep track of posts, we are unable to filter newly updated items
* therefore we could get dupes
* ----
* We will need to collect all timestamps for sorting, and update
* "latestTimestamp" wtih the modst recent time.
* ----
* rss-parser will return items in order of feed; this is not a rss-parser
* issue.
*/
do {
const parser = new Parser();
const feed = await parser.parseURL(config.feedUrl);
const timeStamps = []
for (let i = feed.items.length; i > 0; --i){
const item = feed.items[i-1];
const itemTimestamp = Date.parse(item.isoDate);
if (itemTimestamp > config.latestTimestamp) {
timeStamps.push(itemTimestamp);
await helpers.sleep(2); // sleep between discord posts for rate limits
await discordSender.send(await helpers.makeDiscordMessage(item));
};
};
if (timeStamps.length) {
const latestTimestamp = timeStamps.sort().reverse()[0];
// update config
if (latestTimestamp !== config.latestTimestamp) {
config.latestTimestamp = latestTimestamp;
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
};
};
// console.log(`sleeping for ${config.sleepSeconds} seconds`);
} while(await helpers.sleep(config.sleepSeconds));
})();