This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (67 loc) · 2.49 KB
/
index.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
const fs = require('fs');
const RSSparser = require('rss-parser');
const request = require('request-promise-native');
const parser = new RSSparser();
const {log, err, avatar} = require('./util');
const {config, comics} = require('./config/config');
const last = require('./config/last.json');
let lastFlag = false;
const poll = () => {
comics.forEach((comic, i) => {
setTimeout(comic => {
log(comic, 'Polling')
if(comic.info.feed) {
parser.parseURL(comic.info.feed)
.then(feed => check(comic, feed.items[0]))
.catch(err(comic))
} else {
comic.getItem()
.then(item => check(comic, item))
.catch(err(comic))
}
}, config.delay*i*1000, comic)
})
setTimeout(() => {
if(lastFlag) {
lastFlag = false;
log(null, "Flushing last known comic data to disk")
fs.writeFile(`${__dirname}/config/last.json`, JSON.stringify(last), 'utf8', e => {
e ? err()(e) : log(null, "Last known comic data saved")
})
} else {
log(null, "No new last known comic data to flush")
}
}, config.delay*comics.length*1000)
}
const check = async function(comic, item) {
const pubDate = new Date(item.pubDate)
if(!last[comic.info.id] || pubDate > new Date(last[comic.info.id])) {
log(comic, `New comic published at ${pubDate.toJSON()}`)
const parsed = await comic.parse(item)
const avatar_url = await avatar(comic.info.image)
const data = {
'username': comic.info.name,
avatar_url,
'embeds': [{
'title': parsed.title,
'description': parsed.desc,
'url': parsed.url,
'timestamp': pubDate.toJSON(),
'footer': {'text': parsed.footer},
'image': {'url': parsed.image},
'thumbnail': {'url': parsed.thumbnail}
}]
}
log(comic, `Posting new comic, ${parsed.title}`)
await request({url: config.webhook, method: 'POST', json: data})
log(comic,`Posted ${parsed.title}`)
last[comic.info.id] = pubDate
lastFlag = true
} else {
log(comic, 'No new comic')
}
}
if((config.delay * comics.length) > (config.interval / 1.5))
throw new Error("Interval is too short for configured delay and comic amount")
poll()
setInterval(poll, config.interval*1000)