-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
159 lines (142 loc) · 3.91 KB
/
index.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
const https = require('https');
const QueryString = require('querystring');
const API_DOMAIN = 'apis.justwatch.com';
class JustWatch {
constructor(options) {
this._options = Object.assign({locale:'en_US'}, options);
}
request(method, endpoint, params) {
return new Promise((resolve, reject) => {
params = Object.assign({}, params);
// build request data
const reqData = {
protocol: 'https:',
hostname: API_DOMAIN,
path: '/content' + endpoint,
method: method,
headers: {}
};
let body = null;
// add query string if necessary
if(method==='GET') {
if(Object.keys(params) > 0) {
reqData.path = reqData.path+'?'+QueryString.stringify(params);
}
}
else {
body = JSON.stringify(params);
reqData.headers['Content-Type'] = 'application/json';
}
// send request
const req = https.request(reqData, (res) => {
// build response
let buffers = [];
res.on('data', (chunk) => {
buffers.push(chunk);
});
res.on('end', () => {
// check if response
let output = null;
try {
output = Buffer.concat(buffers);
output = output.toString();
output = JSON.parse(output);
}
catch(error) {
if(res.statusCode !== 200) {
reject(new Error("request failed with status "+res.statusCode+": "+res.statusMessage));
}
else {
reject(error);
}
return;
}
if(output.error) {
reject(new Error(output.error));
}
else {
resolve(output);
}
});
});
// handle error
req.on('error', (error) => {
reject(error);
});
// send
if(method !== 'GET' && body) {
req.write(body);
}
req.end();
});
}
async search(options={}) {
if(typeof options === 'string') {
options = {query: options};
}
else {
options = Object.assign({}, options);
}
// build default params
const params = {
'content_types': null,
'presentation_types': null,
'providers': null,
'genres': null,
'languages': null,
'release_year_from': null,
'release_year_until': null,
'monetization_types': null,
'min_price': null,
'max_price': null,
'scoring_filter_types': null,
'cinema_release': null,
'query': null,
'page': null,
'page_size': null
};
const paramKeys = Object.keys(params);
// validate options
for(const key in options) {
if(paramKeys.indexOf(key) === -1) {
throw new Error("invalid option '"+key+"'");
}
else {
params[key] = options[key];
}
}
// send request
const locale = encodeURIComponent(this._options.locale);
return await this.request('POST', '/titles/'+locale+'/popular', params);
}
async getProviders() {
const locale = encodeURIComponent(this._options.locale);
return await this.request('GET', '/providers/locale/'+locale);
}
async getGenres() {
const locale = encodeURIComponent(this._options.locale);
return await this.request('GET', '/genres/locale/'+locale);
}
async getSeason(season_id) {
season_id = encodeURIComponent(season_id);
const locale = encodeURIComponent(this._options.locale);
return await this.request('GET', '/titles/show_season/' + season_id + '/locale/' + locale);
}
async getEpisodes(show_id) {
show_id = encodeURIComponent(show_id);
const locale = encodeURIComponent(this._options.locale);
return await this.request('GET', '/titles/show/'+show_id+'/locale/'+locale+'/newest_episodes');
}
async getTitle(content_type, title_id) {
title_id = encodeURIComponent(title_id);
content_type = encodeURIComponent(content_type);
const locale = encodeURIComponent(this._options.locale);
return await this.request('GET', '/titles/'+content_type+'/'+title_id+'/locale/'+locale);
}
async getPerson(person_id) {
person_id = encodeURIComponent(person_id);
const locale = encodeURIComponent(this._options.locale);
return await this.request('GET', '/titles/person/'+person_id+'/locale/'+locale);
}
}
module.exports = JustWatch;