-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_rss.js
67 lines (54 loc) · 1.65 KB
/
my_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const me = module.exports = {}
const RSS_JSON_FILE = 'rss.json'
const FS = require('fs')
const Emitter = require('events')
const RSSParser = require('rss-parser')
const events = new Emitter()
me.on = events.on.bind(events)
me.reader = async function(aRSS){
if (typeof aRSS === 'string') {
loadRSSFeed(aRSS)
} else if (Array.isArray(aRSS)) {
aRSS.forEach(loadRSSFeed)
}
}
const emitRSSFeedLoadError = events.emit.bind(events, 'error')
const emitRSSFeedItemParsingError = events.emit.bind(events, 'error')
const emitRSSItem = events.emit.bind(events, 'item')
const readRSSFile = function(){
try {
return JSON.parse(FS.readFileSync('./' + RSS_JSON_FILE))
} catch (error) {
return {}
}
}
const writeRSSFile = function(data){
FS.writeFileSync('./' + RSS_JSON_FILE, JSON.stringify(data))
}
let RSSLastUpdate = readRSSFile()
const loadRSSFeed = async function(aLink){
try {
let parser = new RSSParser()
let feed = await parser.parseURL(aLink)
for (let item of feed.items) {
if (RSSLastUpdate[aLink] === item.pubDate) {
break
}
readRSSFeedItem(feed, item)
}
if (feed && feed.items && feed.items[0]) {
RSSLastUpdate[aLink] = feed.items[0].pubDate
writeRSSFile(RSSLastUpdate)
}
} catch (error) {
emitRSSFeedLoadError({ error, link:aLink })
}
}
const readRSSFeedItem = async function(aFeed, aItem){
try {
let { title, content } = aItem
emitRSSItem({ title, content })
} catch (error) {
emitRSSFeedItemParsingError({ error, feed:aFeed, item:aItem })
}
}