-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathopmlparser.js
52 lines (43 loc) · 1.67 KB
/
opmlparser.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
// Adapted from https://github.com/danmactough/node-opmlparser/blob/master/examples/simple.js
const OpmlParser = require("opmlparser");
const request = require("request");
exports.getFeedsFromOPML = () =>
new Promise((resolve, reject) => {
const opmlparser = new OpmlParser()
let feedUrls = [];
var opmlRequest = request(`https://${process.env.PROJECT_DOMAIN}.glitch.me/subscriptions.opml`);
opmlRequest.on("error", function(error) {
// Reject the Promise by returning an error, with the origin
// of the error set to the request.
console.log("opmlRequest error!")
reject({ error: error });
});
opmlRequest.on("response", function (response) {
// If we didn't get a 200 OK, emit an error.
if (response.statusCode != 200) reject(new Error("Bad status code"));
// Otherwise, pipe the request into feedparser.
this.pipe(opmlparser);
})
opmlparser.on("error", function(error) {
// Reject the Promise by returning an error, with the origin
// of the error set to the request.
console.log("opmlparser error!")
reject({ error: error });
});
// If we get a `readable` event, then...
opmlparser.on("readable", function () {
let outline;
// ...so long as there's an outline item
// available to read..
while (outline = this.read()) {
if (outline['#type'] === "feed") {
// ...add its feed URL to the `feedUrls` array.
feedUrls.push(outline.xmlurl);
}
}
});
// When we're done, resolve the Promise and send back the `feedUrls` object.
opmlparser.on("end", function () {
resolve(feedUrls);
});
});