-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeed.js
71 lines (67 loc) · 1.57 KB
/
feed.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
const FeedParser = require('feedparser-promised');
const { toHumanName, hostName, findFavicon } = require('../../utils');
/**
* An RSS or ATOM feed.
*
* @class Feed
*/
class Feed {
/**
* Creates an instance of Feed.
* @param {string} uri - Link to the site's RSS feed.
* @param {string} type - Dashed name of the site the feed came from.
* @param {string} baseHost - In case the feed differes
* @memberof Feed
*/
constructor(uri, type, baseHost) {
const host = baseHost || hostName(uri);
this.options = {
uri,
timeout: 3000,
}
this.host = host;
this.name = toHumanName(type);
this.type = type;
this.faviconURL = findFavicon(host);
}
/**
* Returns the results of a feed given in options
*
* @returns {Promise}
* @memberof Feed
*/
parseFeed() {
return FeedParser.parse(this.options);
}
/**
* Normalizes the entry data for the feed
*
* @param {Array} data - Items from the fetched rss
* @returns {Array} Normalized items
* @memberof Feed
*/
normalize(data) {
const toSchema = ({ title, date, link }) => ({
title,
publishedAt: new Date(date),
link,
type: this.type,
});
return data.map(toSchema);
}
/**
* Returns normalized, JSON-formatted representation of the feed.
*
* @readonly
* @memberof Feed
*/
get listing() {
return this.parseFeed()
.then(data => this.normalize(data))
.catch(e => {
console.error(e);
throw new Error(`Failed to fetch ${this.type} feed!`)
});
}
}
module.exports = Feed;