Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
var index = require("./index.js"),
2
DomHandler = index.DomHandler,
3
DomUtils = index.DomUtils;
4
5
//TODO: make this a streamable handler
6
function FeedHandler(callback, options){
7
this.init(callback, options);
8
}
9
10
require("util").inherits(FeedHandler, DomHandler);
11
12
FeedHandler.prototype.init = DomHandler;
13
14
function getElements(what, where){
15
return DomUtils.getElementsByTagName(what, where, true);
16
}
17
function getOneElement(what, where){
18
return DomUtils.getElementsByTagName(what, where, true, 1)[0];
19
}
20
function fetch(what, where, recurse){
21
return DomUtils.getText(
22
DomUtils.getElementsByTagName(what, where, recurse, 1)
23
).trim();
24
}
25
26
function addConditionally(obj, prop, what, where, recurse){
27
var tmp = fetch(what, where, recurse);
28
if(tmp) obj[prop] = tmp;
29
}
30
31
var isValidFeed = function(value){
32
return value === "rss" || value === "feed" || value === "rdf:RDF";
33
};
34
35
FeedHandler.prototype.onend = function(){
36
var feed = {},
37
feedRoot = getOneElement(isValidFeed, this.dom),
38
tmp, childs;
39
40
if(feedRoot){
41
if(feedRoot.name === "feed"){
42
childs = feedRoot.children;
43
44
feed.type = "atom";
45
addConditionally(feed, "id", "id", childs);
46
addConditionally(feed, "title", "title", childs);
47
if((tmp = getOneElement("link", childs)) && (tmp = tmp.attribs) && (tmp = tmp.href)) feed.link = tmp;
48
addConditionally(feed, "description", "subtitle", childs);
49
if((tmp = fetch("updated", childs))) feed.updated = new Date(tmp);
50
addConditionally(feed, "author", "email", childs, true);
51
52
feed.items = getElements("entry", childs).map(function(item){
53
var entry = {}, tmp;
54
55
item = item.children;
56
57
addConditionally(entry, "id", "id", item);
58
addConditionally(entry, "title", "title", item);
59
if((tmp = getOneElement("link", item)) && (tmp = tmp.attribs) && (tmp = tmp.href)) entry.link = tmp;
60
if((tmp = fetch("summary", item) || fetch("content", item))) entry.description = tmp;
61
if((tmp = fetch("updated", item))) entry.pubDate = new Date(tmp);
62
return entry;
63
});
64
} else {
65
childs = getOneElement("channel", feedRoot.children).children;
66
67
feed.type = feedRoot.name.substr(0, 3);
68
feed.id = "";
69
addConditionally(feed, "title", "title", childs);
70
addConditionally(feed, "link", "link", childs);
71
addConditionally(feed, "description", "description", childs);
72
if((tmp = fetch("lastBuildDate", childs))) feed.updated = new Date(tmp);
73
addConditionally(feed, "author", "managingEditor", childs, true);
74
75
feed.items = getElements("item", feedRoot.children).map(function(item){
76
var entry = {}, tmp;
77
78
item = item.children;
79
80
addConditionally(entry, "id", "guid", item);
81
addConditionally(entry, "title", "title", item);
82
addConditionally(entry, "link", "link", item);
83
addConditionally(entry, "description", "description", item);
84
if((tmp = fetch("pubDate", item))) entry.pubDate = new Date(tmp);
85
return entry;
86
});
87
}
88
}
89
this.dom = feed;
90
DomHandler.prototype._handleCallback.call(
91
this, feedRoot ? null : Error("couldn't find root of feed")
92
);
93
};
94
95
module.exports = FeedHandler;
96
97