-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvrageparser.js
245 lines (225 loc) · 7.85 KB
/
tvrageparser.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Inherits XML default parser from htmlparser to get data from TvRage request results
*/
var inherits = require('util').inherits,
htmlparser = require("htmlparser"),
DomUtils = htmlparser.DomUtils;
/*
* XML Parser for the TvRage XML Response
* @class TvRageParser inherits DefaultHandler from htmlparser
*/
var TvRageParser = function(callback) {
TvRageParser.super_.call(this, callback, {ignoreWhitespace: true, verbose: false, enforceEmptyTags: false});
};
TvRageParser.ROOT_RESULTS_TAG = "Results";
TvRageParser.ROOT_INFO_TAG = "Showinfo";
TvRageParser.ROOT_EPISODE_TAG = "Show";
TvRageParser.ROOT_EPISODE_INFO_TAG = "show";
TvRageParser.SHOW_TAG = "show";
TvRageParser.GENRES_TAG = "genres";
TvRageParser.GENRE_TAG = "genre";
TvRageParser.NETWORK_TAG = "network";
TvRageParser.AKAS_TAG = "akas";
TvRageParser.AKA_TAG = "aka";
TvRageParser.COUNTRY = "country";
TvRageParser.SEASONS_COUNT = "totalseasons";
TvRageParser.EPISODELIST_TAG = "Episodelist";
TvRageParser.SEASONS_TAG = "Season";
TvRageParser.EPISODE_TAG = "episode";
TvRageParser.EPISODEINFO_TAG = "episode";
TvRageParser.PROPERTIES_MAP = {
"showname": "name",
"showlink": "link",
"origin_country": "country"
};
inherits(TvRageParser, htmlparser.DefaultHandler);
/*
* parse generic properties array
* @method _parsePropertiesArray
* @private
* @param {String} tag to search for array
* @param {Object} properties dom
* @param {String} subtag optional
* @return {Array | Null} returns back an parsed array or null
*/
TvRageParser.prototype._parsePropertiesArray = function(tag, properties, subtag) {
var items, attr, value, result = null;
try {
items = DomUtils.getElementsByTagName(tag, properties, false);
} catch(ex) {}
if(items && items.length) {
result = [];
items.forEach(function(item) {
attr = item.attribs;
value = item.children[0].data;
if(attr && subtag && attr[subtag]) {
attr = attr[subtag];
result[attr] = value;
} else {
result.push(value);
}
}.bind(this));
}
return result;
};
/*
* parse generic properties
* @method _parseProperties
* @private
* @param {Object} properties dom
* @param {Boolean} map properties tag optional
* @return {Object} return parsed array or empty object
*/
TvRageParser.prototype._parseProperties = function(properties, map) {
var tag, genres, value, attribs, country, akas,
result = {};
properties.forEach(function(prop){
tag = prop.name;
if(map && TvRageParser.PROPERTIES_MAP[tag]) {
tag = TvRageParser.PROPERTIES_MAP[tag];
}
if(tag === TvRageParser.GENRES_TAG){
genres = this._parsePropertiesArray(TvRageParser.GENRE_TAG, prop.children);
if(genres) {
result[tag]= genres;
}
} else if(tag === TvRageParser.NETWORK_TAG) {
attribs = prop.attribs;
result[tag] = {};
if(attribs && attribs.country)
country = attribs.country;
else
country = "unknown";
result[tag][country] = prop.children[0].data;
} else if(tag === TvRageParser.AKAS_TAG) {
akas = this._parsePropertiesArray(TvRageParser.AKA_TAG, prop.children, TvRageParser.COUNTRY);
if(akas) {
result[tag]= akas;
}
} else if(prop.children && prop.children.length === 1) {
value = prop.children[0].data;
if(value) {
result[tag] = value;
}
}
}.bind(this));
return result;
};
/*
* overwrite default parser method done to recognise result show data
* @method done
*/
TvRageParser.prototype.done = function() {
var feedRoot,
found = DomUtils.getElementsByTagName(function (value) {
return(value === TvRageParser.ROOT_RESULTS_TAG ||
value === TvRageParser.ROOT_INFO_TAG ||
value === TvRageParser.ROOT_EPISODE_TAG ||
value === TvRageParser.ROOT_EPISODE_INFO_TAG);
}, this.dom, false);
if (found.length) {
feedRoot = found[0];
}
this.results = null;
if(feedRoot) {
switch(feedRoot.name) {
case TvRageParser.ROOT_RESULTS_TAG :
feedRoot = feedRoot.children;
this.results = [];
feedRoot.forEach(function(show) {
if(show.name === TvRageParser.SHOW_TAG) {
this._parseShowResult(show);
}
}.bind(this));
break;
case TvRageParser.ROOT_INFO_TAG :
this._parseShowInfo(feedRoot);
break;
case TvRageParser.ROOT_EPISODE_TAG:
this.results = {};
this._parseEpisodesList(feedRoot);
break;
case TvRageParser.ROOT_EPISODE_INFO_TAG:
this.results = {};
this._parseEpisodeInfo(feedRoot);
break;
default:
break;
}
}
this.dom = this.results;
TvRageParser.super_.prototype.done.call(this);
};
/*
* Parse dom object containing show informations from results response
* @method _parseShowResult
* @private
* @param {Object} item passed as show dom object to parse
*/
TvRageParser.prototype._parseShowResult = function(item) {
var properties = item.children,
show = this._parseProperties(properties);
this.results.push(show);
};
/*
* Parse dom object containing show informations from show information response
* @method _parseShowInfo
* @private
* @param {Object} item passed as show dom object to parse
*/
TvRageParser.prototype._parseShowInfo = function(item) {
var properties = item.children,
show = this._parseProperties(properties, true);
this.results = show;
};
/*
* Parse dom object containing episodes list informations
* @method _parseEpisodesList
* @private
* @param {Object} item passed as show dom object to parse
*/
TvRageParser.prototype._parseEpisodesList = function(item) {
var properties = item.children,
count, seasons, list, episode, seasonlist = [], seasonNumber = 0, episodeNumber = 0;
try {
count = DomUtils.getElementsByTagName(TvRageParser.SEASONS_COUNT, properties, false)[0].children[0].data;
} catch(ex) {}
if(count) {
this.results[TvRageParser.SEASONS_COUNT] = count;
}
try{
list = DomUtils.getElementsByTagName(TvRageParser.EPISODELIST_TAG, properties)[0].children;
DomUtils.getElementsByTagName(TvRageParser.SEASONS_TAG, list).forEach(function(seasons){
seasonNumber = parseInt(seasons.attribs.no);
seasonlist[seasonNumber] = [];
DomUtils.getElementsByTagName(TvRageParser.EPISODE_TAG, seasons.children).forEach(function(item){
episode = this._parseProperties(item.children);
episodeNumber = parseInt(episode.epnum);
if(seasonNumber && episodeNumber) {
delete episode.epnum;
seasonlist[seasonNumber][episodeNumber] = episode;
}
}.bind(this));
}.bind(this));
} catch(ex) {}
if(seasonlist.length) {
this.results[TvRageParser.EPISODELIST_TAG] = seasonlist;
}
};
/*
* Parse dom object containing episode informations
* @method _parseEpisodeInfo
* @private
* @param {Object} item passed as show dom object to parse
*/
TvRageParser.prototype._parseEpisodeInfo = function(item) {
var properties = item.children,
episode;
try{
episode = DomUtils.getElementsByTagName(TvRageParser.EPISODEINFO_TAG, properties)[0].children;
} catch(ex) {}
if(episode) {
this.results = this._parseProperties(episode, true);
}
};
module.exports = TvRageParser;