-
Notifications
You must be signed in to change notification settings - Fork 15
/
tvmaze.js
63 lines (56 loc) · 2.12 KB
/
tvmaze.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
var util = require("util"),
// xml2js = require("xml2js"),
request = require("request"),
// parser = new xml2js.Parser({explicitArray: false}),
// All the URLs we will be making use of.
baseUrl = "http://api.tvmaze.com/",
showInfoUrl = "lookup/shows",
fullShowInfoUrl = "lookup/shows",
seachUrl = "singlesearch/shows",
showInforTvMazeUrl = "shows/";
// Responsible for sending a request down to the url that has
// been passed as an argument.
_request = function(url, callback) {
request({uri: url}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var outJson= JSON.parse (body);
//parser.parseString(body, function(err, result) {
var output = {'Showinfo' :
{showname : outJson['name']}
};
//body['Showinfo']['showname'] = body.name;
callback(err,output);
//});
} else {
_httpError(err, response, callback, url);
}
});
};
// Responsible for raising an error with the appropriate
// status code.
_httpError = function(error, response, callback,url) {
var status = (response && response.statusCode) ? (response.statusCode) : (error.code);
var err = new Error(util.format("TvMaze API responded with status code %s url : %s ",status,url));
err.http_code = status;
callback(err);
};
exports.showInfoSearchName = function(showName, callback){
var url = util.format("%s%s?q=%s",baseUrl,searchUrl,showName);
_request(url,callback);
};
exports.showInfoTvMaze = function(tvMazeId, callback){
var url = util.format("%s%s%s",baseUrl, showInforTvMazeUrl,tvMazeId);
_request(url,callback);
};
// Show info based on a show id that can be acquired via search
// or fullSearch.
exports.showInfoTvRage = function(showId, callback) {
var url = util.format("%s%s?tvrage=%s", baseUrl, showInfoUrl, showId);
_request(url, callback);
};
// Full show info based on a show id that can be acquired via search
// or fullSearch.
exports.fullShowInfoTvRage = function(showId, callback) {
var url = util.format("%s%s?tvrage=%s", baseUrl, fullShowInfoUrl, showId);
_request(url, callback);
};