forked from aessam/ikhbr-SmartArabicNewsAggregator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSSFeedV2Parser.js
71 lines (59 loc) · 2.37 KB
/
RSSFeedV2Parser.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
/**
* Created by aessam on 4/13/14.
*/
var allowedItemsElements = {
"title" : "title", "link" : "link", "a10:author": "author", "media:content": "description",
"description" : "description", "author" : "author","a10:name" : "author", "category" : "category", "comments" : "comments",
"enclosure" : "media", "guid" : "id", "pubdate" : "date", "source" : "source", "date" : "date","body":"description",
"content:encoded": "encoded", "dc:creator":"author", "image" : "media", "dc:identifier" : "id", "id":"id",
"published":"date", "rights":"rights", "summary":"description", "media:conent":"description","media:thumbnail":"mediaSmall",
"img" :"media"};
var ptype = RSSFeedV2Parser.prototype;
function RSSFeedV2Parser() {
this.tree = [];
this.lastOpenedTag = "";
this.lastCreatedItem = [];
this.itemCompeletionCallBack;
}
ptype.setItemCompeletionCallBack = function (clbk){
this.itemCompeletionCallBack = clbk;
}
ptype.getParsedFeed = function() {
return this.tree;
};
ptype.getLastCreatedItem = function() {
return this.lastCreatedItem;
};
ptype.tagOpened = function(node) {
if(node.name.toLowerCase() == "item"){
this.lastCreatedItem = {};
this.tree.push(this.lastCreatedItem);
}
if(allowedItemsElements[node.name.toLowerCase()] != undefined){
this.lastOpenedTag = node.name.toLowerCase();
}
};
ptype.tagClosed = function(node) {
if(node.toLowerCase() == "item"){
if(this.itemCompeletionCallBack){
this.itemCompeletionCallBack(this.lastCreatedItem);
}
this.lastCreatedItem = [];
this.lastOpenedTag = "";
}
if(allowedItemsElements[this.lastOpenedTag] != undefined &&
this.lastOpenedTag==node.toLowerCase()){
this.lastOpenedTag = "";
}
}
ptype.addTextToLastOpenedTag = function(text) {
if(!this.lastCreatedItem[allowedItemsElements[this.lastOpenedTag]])
this.lastCreatedItem[allowedItemsElements[this.lastOpenedTag]] = "";
this.lastCreatedItem[allowedItemsElements[this.lastOpenedTag]] = this.lastCreatedItem[allowedItemsElements[this.lastOpenedTag]] + text;
};
RSSFeedV2Parser.validateAndCreate = function (initNode){
if (initNode.name.toLowerCase() == "rss" && initNode.attributes.VERSION && initNode.attributes.VERSION.toLowerCase() == "2.0"){
return new RSSFeedV2Parser();
}
}
module.exports = RSSFeedV2Parser;