forked from lufinkey/node-justwatch-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (163 loc) · 5.62 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
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
"use strict";
const https = require('https');
const QueryString = require('querystring');
const API_DOMAIN = 'apis.justwatch.com';
var JustWatch = {};
JustWatch._options = Object.assign({locale:'en_US'});
this._options = Object.assign({locale:'en_US'});
function 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();
});
}
JustWatch.search = async function search(options={}) {
if(typeof options === 'string') {
options = {query: options};
}
else {
options = Object.assign({}, options);
}
// build default params
const params = {
'age_certifications':null,
'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,
'nationwide_cinema_releases_only':null,
'scoring_filter_types': null,
'cinema_release': null,
'query': null,
'page': null,
'page_size': null,
'timeline_type':null,
'person_id':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 request('POST', '/titles/'+locale+'/popular', params);
}
JustWatch.getProviders = async function getProviders() {
const locale = encodeURIComponent(this._options.locale);
return await request('GET', '/providers/locale/'+locale);
}
JustWatch.getGenres = async function getGenres() {
const locale = encodeURIComponent(this._options.locale);
return await request('GET', '/genres/locale/'+locale);
}
JustWatch.getTitle = async function getTile(title_id, content_type){
const locale = encodeURIComponent(this._options.locale);
return await request('GET', '/titles/' + content_type + '/' + title_id + '/locale/' + locale);
}
JustWatch.searchTitleID = async function searchTitleID(query){
results = [];
for(let result in this.search(query)){
results.push({id: result.id, title: result.title});
}
return results;
}
JustWatch.getSeason = async function getSeason(season_id) {
season_id = encodeURIComponent(season_id);
const locale = encodeURIComponent(this._options.locale);
return await request('GET', '/titles/show_season/' + season_id + '/locale/' + locale);
}
JustWatch.getEpisodes = async function getEpisodes(show_id) {
show_id = encodeURIComponent(show_id);
const locale = encodeURIComponent(this._options.locale);
return await request('GET', '/titles/show/'+show_id+'/locale/'+locale+'/newest_episodes');
}
JustWatch.getTitle = async function getTitle(content_type, title_id) {
title_id = encodeURIComponent(title_id);
content_type = encodeURIComponent(content_type);
const locale = encodeURIComponent(this._options.locale);
return await request('GET', '/titles/'+content_type+'/'+title_id+'/locale/'+locale);
}
JustWatch.getPerson = async function getPerson(person_id) {
person_id = encodeURIComponent(person_id);
const locale = encodeURIComponent(this._options.locale);
return await request('GET', '/titles/person/'+person_id+'/locale/'+locale);
}
JustWatch.getCertification = async function getCertification(content_type, country){
const params = {
'country': country,
'object_type': content_type
}
return await request('GET', '/age_certifications', params);
}
module.exports = JustWatch;