1 |
|
2 |
|
3 |
|
4 | var searchRoot = function(tagName){
|
5 | if(tagName === "rss" || tagName === "rdf:RDF" || tagName === "feed"){
|
6 | if(tagName === "rdf:RDF") this.feed.type = "rdf";
|
7 | else this.feed.type = tagName;
|
8 | this._map = RssFeedMap;
|
9 | this.onopentag = getChannelElement;
|
10 | }
|
11 | else if(tagName === "feed"){
|
12 | this.feed.type = "atom";
|
13 | this._map = AtomFeedMap;
|
14 | this.onclosetag = getFeedElements;
|
15 | this.ontext = writeText;
|
16 | this.onopentag = getOpenTag;
|
17 | }
|
18 | }
|
19 |
|
20 | var getChannelElement = function(tagName){
|
21 | if(tagName === "channel"){
|
22 | this.onopentag = getOpenTag;
|
23 | this.onclosetag = getFeedElements;
|
24 | this.ontext = writeText;
|
25 | }
|
26 | }
|
27 |
|
28 | var getOpenTag = function(tagName, attribs){
|
29 | this._level += 1;
|
30 | if(tagName === this._childName){
|
31 | if(this._feed.type === "atom"){
|
32 | }
|
33 | else{
|
34 |
|
35 | }
|
36 | } else if(tagName === "link" && this._level === 1
|
37 | && this._feed.type === "atom" && attribs.href){
|
38 | this.feed.link = attribs.href;
|
39 | }
|
40 | };
|
41 |
|
42 |
|
43 | var writeText = function(text){
|
44 | if(this._stack[this._level]){
|
45 | this._stack[this._level] += text;
|
46 | } else this._stack[this._level] = text;
|
47 | };
|
48 |
|
49 |
|
50 | var getFeedElements = function(tagName){
|
51 | var text = this._stack.pop();
|
52 | if(this._level-- === 1){
|
53 | var elemName = this._map[tagName];
|
54 | if(elemName){
|
55 | if(elemName === "updated") text = Date(text);
|
56 | this._feed[elemName] = text;
|
57 | }
|
58 | }
|
59 | };
|
60 |
|
61 |
|
62 | var RssFeedMap = {
|
63 | title: "title",
|
64 | link: "link",
|
65 | description: "description",
|
66 | lastBuildDate: "updated",
|
67 | managingEditor: "author" |
68 |
|
69 | };
|
70 |
|
71 | var AtomFeedMap = {
|
72 | id: "id",
|
73 | title: "title",
|
74 | subtitle: "description",
|
75 | updated: "updated",
|
76 | email: "author" |
77 |
|
78 | };
|
79 |
|
80 |
|
81 | function FeedHandler(callback, onitem){
|
82 | this.onopentag = searchRoot;
|
83 | this.feed = {
|
84 | type: null,
|
85 | id: "",
|
86 | title: null,
|
87 | link: null,
|
88 | description: null,
|
89 | updated: null,
|
90 | author: null,
|
91 | items: []
|
92 | };
|
93 | this._level = 0;
|
94 | this._stack = [];
|
95 | this._map = null;
|
96 | this.onend = callback;
|
97 | this.onitem = onitem;
|
98 | }
|
99 |
|
100 | module.exports = FeedHandler; |
\ | No newline at end of file |