-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfeender.js
More file actions
67 lines (63 loc) · 2.14 KB
/
feender.js
File metadata and controls
67 lines (63 loc) · 2.14 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
57
58
59
60
61
62
63
64
65
66
67
var request = require('request');
var urlparser = require('url');
var htmlparser = require('htmlparser');
var select = require('soupselect').select;
module.exports = function(url, done) {
request.get(url, {gzip: true}, function (error, response, body) {
var feeds = [];
var handler = new htmlparser.DefaultHandler(function (error, dom) {
if (error) {
done(error, null);
}
else {
select(dom, "head link").forEach(function(link) {
if ((link.attribs.type === "application/atom+xml" || link.attribs.type === "application/rss+xml") && link.attribs.href) {
var feedUrl = urlparser.parse(link.attribs.href);
var feed = {rel: "alternate", type: link.attribs.type};
if(feedUrl.hostname) {
feed.href = link.attribs.href;
}
else {
feed.href = urlparser.format(urlparser.resolve(url, link.attribs.href));
}
if(link.attribs.title) {
feed.title = link.attribs.title;
}
else {
feed.title = select(dom, "title")[0].children[0].raw;
}
feeds.push(feed)
}
});
var title = '';
if(select(dom, "title")[0] && select(dom, "title")[0].children && select(dom, "title")[0].children[0])
title = select(dom, "title")[0].children[0].raw;
if(feeds.length === 0) {
var atom = select(dom, "feed");
if(atom.length > 0 && atom[0].attribs.xmlns.toLowerCase() === 'http://www.w3.org/2005/Atom'.toLowerCase()) {
feeds.push({
rel: "self",
type: "application/atom+xml",
href: url,
title: title
});
}
}
if(feeds.length === 0) {
var rss = select(dom, "rss")[0];
if(rss) {
feeds.push({
rel: "self",
type: "application/rss+xml",
href: url,
title: title
});
}
}
done(null, feeds);
}
});
var parser = new htmlparser.Parser(handler);
parser.parseComplete(body);
});
}