-
Notifications
You must be signed in to change notification settings - Fork 3
/
nitroSdk.js
372 lines (320 loc) · 9.18 KB
/
nitroSdk.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
Nitro SDK (can also be used with other recent BBC APIs)
*/
var http = require('http');
var https = require('https');
var url = require('url');
var util = require('util');
var debuglog = util.debuglog('bbc');
var api = require('./nitroApi/api.js');
String.prototype.toCamelCase = function camelize() {
return this.toLowerCase().replace(/[-_ \/\.](.)/g, function(match, group1) {
return group1.toUpperCase();
});
}
var httpAgent;
var httpsAgent;
var dest = {};
var rateLimitEvents = 0;
var inFlight = 0;
var stacked = 0;
function query() {
this.querystring = '';
this.add = function(param,value,previous) {
this.querystring += (this.querystring || previous===true ? '&' : '?')+param;
if (value) this.querystring += '=' + encodeURIComponent(value);
return this;
};
this.reset = function() {
this.querystring = '';
return this;
};
this.clone = function() {
var newQ = new query();
newQ.querystring = this.querystring;
return newQ;
}
this.toString = function() {
return this.querystring;
}
this.fromString = function(s,previous) {
this.reset();
s = s.split('?').pop();
var e = s.split('&');
for (var p of e) {
var c = p.split('=');
this.add(c[0],decodeURIComponent(c[1]),previous);
previous = false;
}
return this;
}
}
function nitroRawEpisode(pid, version) {
if (!version) version = 1;
return '/nitro/api/v'+version+'/episodes/'+pid;
}
function nitroRawEpisodeGenreGroups(pid, version) {
if (!version) version = 1;
return '/nitro/api/v'+version+'/episodes/'+pid+'/genre_groups';
}
function nitroRawEpisodeFormats(pid, version) {
if (!version) version = 1;
return '/nitro/api/v'+version+'/episodes/'+pid+'/formats';
}
function nitroRawEpisodeAncestors(pid, version) {
if (!version) version = 1;
return '/nitro/api/v'+version+'/episodes/'+pid+'/ancestors';
}
function nitroRawMasterBrand(mbid, version) {
if (!version) version = 1;
return '/nitro/api/v'+version+'/master_brands/'+mbid;
}
function nitroRawBrand(pid, version) {
if (!version) version = 1;
return '/nitro/api/v'+version+'/brands/'+pid;
}
function nitroRawBrandFranchises(pid, version) {
if (!version) version = 1;
return '/nitro/api/v'+version+'/brands/'+pid+'/franchises';
}
function nitroRawPromotion(pid, version) {
if (!version) version = 1;
return '/nitro/api/v'+version+'/promotions/'+pid;
}
function hasHeader(header, headers) {
// snaffled from request module
var headers = Object.keys(headers || this.headers),
lheaders = headers.map(function (h) {return h.toLowerCase();});
header = header.toLowerCase();
for (var i=0;i<lheaders.length;i++) {
if (lheaders[i] === header) return headers[i];
}
return false;
}
function makeRequest(host,path,key,query,settings,callback,err){
inFlight++;
debuglog(host+path+(key ? '?K' : '')+query.querystring);
var defaults = {
headers: {
Accept: 'application/json',
User_Agent: 'BBCiPlayerRadio/1.6.1.1522345 (SM-N900; Android 4.4.2)',
},
api_key_name: 'api_key',
proto: 'http',
port: null,
backoff : 31000,
timeout : 120000,
payload: {}
}
settings = Object.assign({},defaults,settings); // merge/extend
var qs = query.querystring;
if (key) {
qs = '?' + settings.api_key_name + '=' + encodeURIComponent(key) + qs;
}
var options = {
hostname: host,
port: settings.port ? settings.port : (settings.proto == 'http' ? 80 : 443),
path: path+qs,
method: 'GET',
headers: settings.headers
};
var proto = (settings.proto == 'http' ? http : https);
if (settings.proto == 'http') {
if (!httpAgent) {
httpAgent = new http.Agent({ keepAlive: true });
}
options.agent = httpAgent;
}
else {
if (!httpsAgent) {
httpsAgent = new https.Agent({ keepAlive: true });
}
options.agent = httpsAgent;
}
var list = '';
var obj;
var json = (settings.headers.Accept == 'application/json');
var req = proto.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (data) {
list += data;
});
res.on('end', function() {
inFlight--;
if (res.statusCode >= 300 && res.statusCode < 400 && hasHeader('location', res.headers)) {
// handle redirects, as per request module
var location = res.headers[hasHeader('location', res.headers)];
var locUrl = url.parse(location);
host = locUrl.host;
path = locUrl.pathname;
makeRequest(host,path,key,query,settings,callback,err);
}
else if (res.statusCode >= 400 && res.statusCode < 600) {
if (list) {
try {
if (json) {
obj = JSON.parse(list);
}
else {
obj = list;
}
if (typeof err !== 'undefined') err(res.statusCode,obj,settings.payload);
if (json && obj.fault) {
// process Apigee rate-limiting
if (obj.fault.detail && obj.fault.detail.errorcode) {
if (obj.fault.detail.errorcode == 'policies.ratelimit.QuotaViolation') {
rateLimitEvents++;
// rate limiting, back off by configured amount
stacked++;
setTimeout(function(){
stacked--;
makeRequest(host,path,key,query,settings,callback)
},settings.backoff);
}
else {
log_fault(obj,res,query); // rate limits leak keys in error messages
}
}
}
else if (json && obj.errors) {
log_error(obj,res,query);
}
else {
log_error(null,res,query);
}
}
catch (e) {
if (typeof err !== 'undefined') err(res.statusCode,list,settings.payload);
console.warn(e);
console.warn('Invalid JSON received:');
console.warn(list);
}
}
else {
if (typeof err !== 'undefined') err(res.statusCode,null,settings.payload);
log_error(null,res,query);
}
}
else try {
if (json) {
obj = JSON.parse(list);
}
else {
obj = list;
}
var result = callback(obj,settings.payload);
if (dest.callback) {
// call the callback's next required destination
// e.g. second and subsequent pages
if (dest.path) {
makeRequest(host,dest.path,key,dest.query,settings,dest.callback,dest.err);
}
else {
dest.callback({},settings.payload);
}
}
}
catch (e) {
console.warn('Something went wrong parsing the response JSON');
console.warn(e);
console.warn(res.statusCode+' '+res.statusMessage);
console.warn(res.headers);
debuglog('** '+list);
}
});
}).setTimeout(settings.timeout);
req.on('error', function(e) {
if (typeof err !== 'undefined') err(500,e.message + ' ' +list,settings.payload)
else console.warn(host+path,e.message);
});
req.end();
}
function log_fault(fault,res,query) {
/*
{ "fault": {
"faultstring": "Rate limit quota violation. Quota limit : 0 exceeded by 1. Total violation count : 1. Identifier : YOUR-API-KEY-HERE",
"detail":
{"errorcode": “policies.ratelimit.QuotaViolation"}
}
}
*/
if (res) console.warn('Fault: '+res.statusCode+' '+res.statusMessage);
if (query && query.toString()) console.warn(query.querystring);
if (fault) console.warn(fault.fault.detail.errorcode+': '+fault.fault.faultstring);
}
function log_error(error,res,query) {
/*
{"errors":{"error":{"code":"XDMP-EXTIME","message":"Time limit exceeded"}}}
*/
if (res) console.warn('Error: '+res.statusCode+' '+res.statusMessage);
if (query && query.toString()) console.warn(query.querystring);
if (error) console.warn(error.errors.error.code+': '+error.errors.error.message);
}
module.exports = {
setReturn : function(destination) {
dest = destination;
},
getReturn : function(){
return dest;
},
logFault : function(fault,res,query) {
log_fault(fault,res,query);
},
logError : function(error,res,query) {
log_error(error,res,query);
},
hasHeader : hasHeader,
make_request : function(host,path,key,query,settings,callback,err) {
makeRequest(host,path,key,query,settings,callback,err);
},
ping : function(host,key,settings,callback) {
var q = new query();
q.add(api.fMasterbrandsPageSize,1,true);
makeRequest(host,api.nitroMasterbrands,key,q,settings,callback);
},
getRateLimitEvents : function() {
return rateLimitEvents;
},
resetRateLimitEvents : function() {
rateLimitEvents = 0;
},
getRequests : function() {
return inFlight+stacked;
},
newQuery : function(param,value,previous) {
q = new query();
if (param) q.add(param,value,previous);
return q;
},
queryFrom : function(url,previous) {
q = new query();
if (url.indexOf('?')>=0) {
url = url.split('?')[1];
}
else if (url.indexOf('&')>=0) {
url = url.split('&')[1];
}
q.querystring = ((previous ? '&' : '?')+url);
return q;
},
iso8601durationToSeconds : function(input) {
var reptms = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/;
var hours = 0, minutes = 0, seconds = 0, totalseconds;
if (reptms.test(input)) {
var matches = reptms.exec(input);
if (matches[1]) hours = Number(matches[1]);
if (matches[2]) minutes = Number(matches[2]);
if (matches[3]) seconds = Number(matches[3]);
totalseconds = hours * 3600 + minutes * 60 + seconds;
}
return totalseconds;
},
nitroRawEpisode : nitroRawEpisode,
nitroRawEpisodeGenreGroups : nitroRawEpisodeGenreGroups,
nitroRawEpisodeFormats : nitroRawEpisodeFormats,
nitroRawEpisodeAncestors : nitroRawEpisodeAncestors,
nitroRawMasterBrand : nitroRawMasterBrand,
nitroRawBrand : nitroRawBrand,
nitroRawBrandFranchises : nitroRawBrandFranchises,
nitroRawPromotion : nitroRawPromotion
}