-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtvrage.js
More file actions
150 lines (133 loc) · 4.18 KB
/
tvrage.js
File metadata and controls
150 lines (133 loc) · 4.18 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
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
/*
* Nodejs module to access tvrage web services.
* @module tvrage
*/
/*
* @class TvRage
*/
var TvRage = function() {
this.http = require('http'),
this.querystring = require('querystring'),
this.tvRageParser = require('./tvrageparser');
this.busy = false;
};
TvRage.VERSION = "0.1";
TvRage.PATH_PREFIX = '/feeds/';
TvRage.SEARCH_URI = "search.php?";
TvRage.INFO_URI = "showinfo.php?";
TvRage.EPISODE_LIST_URI = "episode_list.php?";
TvRage.EPISODE_INFO_URI = "episodeinfo.php?";
TvRage.HTTP_OPTIONS = {
host: 'services.tvrage.com',
port: 80,
method: 'GET'
};
/*
* Handle response from TvRage
* @method _parseResponse
* @private
* @param {Function} callback passed by request
* @param {Object} res Http response object
*/
TvRage.prototype._parseResponse = function(callback, res) {
var buf = [],
idx = 0,
handler, parser;
if(res.statusCode !== 200) {
this._handleError(new Error("wrong http status code"));
return;
}
res.on('data', function(chunk) {
buf[idx++] = chunk;
});
res.on('end', function() {
this.parser = this.parser || require("htmlparser");
handler = new this.tvRageParser();
parser = new this.parser.Parser(handler);
parser.parseComplete(buf.join());
process.nextTick(function() { callback(handler.dom);});
}.bind(this));
};
/*
* Handle if http client response had an error
* @method _handleError
* @private
* @param {Exception} e Exception object
*/
TvRage.prototype._handleError = function(e) {
throw new Error(e);
};
/*
* Get show information by id
* @method _request
* @private
* @param {Object} options for the tvrage request
* @param {Function} callback
*/
TvRage.prototype._request = function(options, callback) {
this.http.get(options, function(res) {
this._parseResponse(callback, res);
}.bind(this)).on('error', function(e) {
this._handleError(e);
}.bind(this));
};
/*
* Search tvshow id by name
* @method search
* @param {String} name of the tv show for the search
* @param {Function} callback that should be called after search is done
*/
TvRage.prototype.search = function(name, callback) {
var options = TvRage.HTTP_OPTIONS,
query = this.querystring.stringify({show: name});
options.path = TvRage.PATH_PREFIX + TvRage.SEARCH_URI + query;
this._request(options, callback);
};
/*
* Get show information by id
* @method showInfo
* @param {Int} id of the tv show to get info
* @param {Function} callback that should be called after response was received
*/
TvRage.prototype.showInfo = function(id, callback) {
var options = TvRage.HTTP_OPTIONS,
query = this.querystring.stringify({sid: id});
options.path = TvRage.PATH_PREFIX + TvRage.INFO_URI + query;
this._request(options, callback);
};
/*
* Get episode list for the show by id
* @method episodeList
* @param {Int} id of the tv show to get info
* @param {Function} callback that should be called after response was received
*/
TvRage.prototype.episodeList = function(id, callback) {
var options = TvRage.HTTP_OPTIONS,
query = this.querystring.stringify({sid: id});
options.path = TvRage.PATH_PREFIX + TvRage.EPISODE_LIST_URI + query;
this._request(options, callback);
};
/*
* Get episode info for the show by id and episode number
* @method episodeList
* @param {Int} id of the tv show to get episode info
* @param {Int} season of the tv show to get episode info
* @param {Int} episode of the tv show to get episode info
* @param {Function} callback that should be called after response was received
*/
TvRage.prototype.episodeInfo = function(id, season, episode, callback) {
var options = TvRage.HTTP_OPTIONS,
episodeNum = season + 'x' + episode,
query = this.querystring.stringify({sid: id, ep: episodeNum});
options.path = TvRage.PATH_PREFIX + TvRage.EPISODE_INFO_URI + query;
this._request(options, callback);
};
/*
* Returns string to identify module for debuging
* @method toString
* @return {String} The text contains module name and version.
*/
TvRage.prototype.toString = function() {
return "TvRage API Client v."+TvRage.VERSION;
};
module.exports = TvRage;