-
Notifications
You must be signed in to change notification settings - Fork 3
/
nitro.js
831 lines (741 loc) · 23 KB
/
nitro.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/*
List programmes by aggregation (category, format, or search)
*/
'use strict';
var fs = require('fs');
var util = require('util');
var url = require('url');
var getopt = require('node-getopt');
var j2x = require('jgexml/json2xml');
var giUtils = require('./giUtils');
var nitro = require('./nitroSdk');
var api = require('./nitroApi/api');
const colours = process.stdout.isTTY;
if (process.env.NODE_DISABLE_COLORS) colours = false;
var red = colours ? '\x1b[31m' : '';
var green = colours ? '\x1b[32m' : '';
var normal = colours ? '\x1b[0m' : '';
var programme_cache = [];
var download_history = [];
var showAll = false;
var dumpMediaSets = false;
var upcoming = false;
var pidList = [];
var indexBase = 10000;
var channel = '';
var partner_pid = '';
var sid = '';
var search = '';
var children = false;
var embargoed = '';
var tag = '';
// bbc seem to use int(ernal),test,stage and live
// http://programmes.api.bbc.com
// http://nitro.api.bbci.co.uk - deprecated since 24/12/2015
// http://nitro.stage.api.bbci.co.uk/nitro/api/
// http://d.bbc.co.uk/nitro/api/ - deprecated since 24/12/2015
// http://d.bbc.co.uk/stage/nitro/api/
// http://data.bbc.co.uk/nitro/api/ - deprecated since 24/12/2015
// https://api.live.bbc.co.uk/nitro/api/
// https://api.test.bbc.co.uk/nitro/api/
// http://nitro-e2e.api.bbci.co.uk/nitro-e2e/api/ - locked down?
// http://nitro-slave-1.cloud.bbc.co.uk/nitro/api
var host = 'programmes.api.bbc.com';
var domain = '/nitro/api';
var feed = '/programmes';
var mediaSet = 'pc';
var payment_type = 'free';
var api_key = '';
var service = 'radio';
var media_type = 'audio';
const pageSize = 30;
var debuglog = util.debuglog('bbc');
//----------------------------------------------------------------------------
var add_programme = function(obj,parent) {
var seen = false;
for (var i in programme_cache) {
var check = programme_cache[i];
if (check.pid == obj.pid) {
seen = true;
if (check.x_parent && !check.x_parent.pid && parent) check.x_parent = parent;
break;
}
}
if (!seen) {
if (parent) {
obj.x_parent = parent;
}
programme_cache.push(obj);
}
};
//-----------------------------------------------------------------------------
function pad(str, padding, padRight) {
if (typeof str === 'undefined')
return padding;
if (padRight) {
return (str + padding).substring(0, padding.length);
} else {
return (padding + str).slice(-padding.length);
}
}
//-----------------------------------------------------------------------------
function toArray(obj) {
if (Array.isArray(obj)) return obj;
var a = [];
a.push(obj);
return a;
}
//-----------------------------------------------------------------------------
function pc_export() {
var hidden = 0;
console.log('\n* Programme Cache:');
var index = indexBase;
for (var i in programme_cache) {
index++;
var p = programme_cache[i];
var present = giUtils.binarySearch(download_history,p.pid)>=0;
var title = p.title;
var subtitle = (p.display_titles && p.display_titles.subtitle ? p.display_titles.subtitle : p.presentation_title);
var available = ((p.available_versions) && (p.available_versions.available > 0));
var position = p.episode_of ? p.episode_of.position : 1;
var totaleps = 1;
var series = 1;
var len = (p.version && p.version.duration) ? nitro.iso8601durationToSeconds(p.version.duration) : '0';
var thumb = p.image.template_url.replace('$recipe','150x84');
//#index|type|name|pid|available|episode|seriesnum|episodenum|versions|duration|desc|channel|categories|thumbnail|timeadded|guidance|web
if (i==programme_cache.length-1) {
debuglog(JSON.stringify(p,null,2));
}
console.log(index+'|'+(p.media_type == 'Video' ? 'tv' : 'radio')+'|'+title+'|'+p.pid+'|'+
(available ? 'Available' : 'Unavailable')+'|'+subtitle+'|'+'|'+series+'|'+position+'|'+'default'+'|'+
len+'||'+(p.master_brand ? p.master_brand.mid : 'unknown')+'|'+category[0]+'|'+thumb+'|'+Math.floor(Date.now()/1000)+
'||'+'http://bbc.co.uk/programmes/'+p.pid+'|');
}
}
//-----------------------------------------------------------------------------
function pc_dump() {
var hidden = 0;
console.log('\n* Programme Cache:');
for (var i in programme_cache) {
var p = programme_cache[i];
var present = giUtils.binarySearch(download_history,p.pid)>=0;
var title = (p.title ? p.title : p.presentation_title);
var parents = '';
var ancestor_titles = '';
for (var at in p.ancestor_titles) {
present = present || (download_history.indexOf(p.ancestor_titles[at].pid)>=0);
var t = '';
if (p.ancestor_titles[at].title) {
t = p.ancestor_titles[at].title;
}
else if (p.ancestor_titles[at].presentation_title) {
t = p.ancestor_titles[at].presentation_title;
}
if (p.ancestor_titles[at].ancestor_type != 'episode') {
ancestor_titles += t + ' : ';
}
else if (!title) {
title = t;
}
parents += ' ' + p.ancestor_titles[at].pid + ' ('+t+') ';
}
title = ancestor_titles + title;
if (p.version && p.version.pid) {
parents += ' ' + p.version.pid + ' (vPID)';
}
var available = ((p.available_versions) && (p.available_versions.available > 0));
if ((!present && available) || (pidList.indexOf(p.pid)>=0) || showAll) {
if (programme_cache.length==1) {
debuglog(JSON.stringify(p,null,2));
}
var position = p.episode_of ? p.episode_of.position : 1;
var totaleps = p.x_parent && p.x_parent.expected_child_count ? p.x_parent.expected_child_count : 1;
var series = 1;
console.log(p.pid+' '+pad(p.item_type,' ',true)+' '+
(p.media_type ? p.media_type : 'Audio')+' '+(available ? 'Available' : 'Unavailable')+' '+green+title+normal);
var len = (p.version && p.version.duration) ? p.version.duration : '0s';
if (p.available_versions) {
for (var v in p.available_versions.version) {
var version = p.available_versions.version[v];
if (len == '0s') {
len = version.duration;
}
parents += ' ' + version.pid + ' (vPID '+version.types.type[0]+')';
for (var va in p.available_versions.version[v].availabilities) {
var a = p.available_versions.version[v].availabilities[va];
// dump mediasets
if (dumpMediaSets) {
for (var vaa in a) {
var vaaa = a[vaa];
for (var ms in vaaa.media_sets.media_set) {
console.log(p.available_versions.version[v].pid+' '+vaaa.status+' '+vaaa.media_sets.media_set[ms].name);
}
}
}
}
}
}
if (!len) len = '';
len = len.replace('PT','').toLocaleLowerCase(); // ISO 8601 duration
console.log(' '+len+' S'+pad(series,'00')+'E'+pad(position,'00')+
'/'+pad(totaleps,'00')+' '+(p.synopses && p.synopses.short ? p.synopses.short : 'No description'));
if (parents) console.log(parents);
if (p.master_brand) {
console.log(' '+p.master_brand.mid+' @ '+(p.release_date ? p.release_date :
(p.release_year ? p.release_year : p.updated_time)));
}
if (p.contributions) {
console.log();
var contribution = toArray(p.contributions.contribution);
for (var cont of contribution) {
console.log((cont.character_name ? cont.character_name : (cont.credit_role ? cont.credit_role.$ : 'Unknown'))+' - '+
(cont.contributor && cont.contributor.name ? (cont.contributor.name.given ? cont.contributor.name.given+' '+cont.contributor.name.family :
cont.contributor.name.presentation) : 'Unknown'));
}
}
//if (p.x_parent) {
// console.log(JSON.stringify(p.x_parent,null,2));
//}
}
else {
hidden++;
}
}
if (programme_cache.length == 1) {
var item = programme_cache[0];
if (item.synopses && (item.synopses.long || item.synopses.medium)) {
console.log();
console.log(item.synopses.long ? item.synopses.long : item.synopses.medium);
}
}
console.log();
console.log('Cache has '+programme_cache.length+' entries, '+hidden+' hidden');
}
//_____________________________________________________________________________
var processResponse = function(obj,payload) {
var nextHref = '';
if ((obj.nitro.pagination) && (obj.nitro.pagination.next)) {
nextHref = obj.nitro.pagination.next.href;
//console.log(nextHref);
}
var pageNo = obj.nitro.results.page;
var top = obj.nitro.results.total;
if (!top) {
top = obj.nitro.results.more_than+1;
}
var last = Math.ceil(top/obj.nitro.results.page_size);
process.stdout.write('.');
var length = 0;
if (obj.nitro.results.items) {
length = obj.nitro.results.items.length;
}
if (length==0) {
//console.log('No results returned');
}
else {
for (var i in obj.nitro.results.items) {
var p = obj.nitro.results.items[i];
//debuglog(JSON.stringify(p,null,2));
if ((p.item_type == 'episode') || (p.item_type == 'clip') || (p.item_type == episode_type)) {
add_programme(p,payload);
}
else if ((p.item_type == 'series') || (p.item_type == 'brand')) {
var path = domain+feed;
var query = nitro.newQuery(api.fProgrammesDescendantsOf,p.pid,true)
.add(api.fProgrammesAvailabilityAvailable)
.add(api.fProgrammesAvailabilityEntityTypeEpisode)
.add(api.fProgrammesAvailabilityTypeOndemand)
.add(api.fProgrammesPaymentType,payment_type)
.add(api.fProgrammesMediaSet,mediaSet)
.add(api.mProgrammesDuration)
.add(api.mProgrammesAncestorTitles)
.add(api.mProgrammesAvailability)
.add(api.mProgrammesAvailableVersions)
.add(api.fProgrammesEntityTypeEpisode)
.add(api.fProgrammesPageSize,pageSize);
if (media_type) {
query.add(api.fProgrammesMediaType,media_type);
}
process.stdout.write('>');
var settings = {};
settings.payload = p;
nitro.make_request(host,path,api_key,query,settings,processResponse);
}
else {
console.log('Unhandled type: '+p.type);
console.log(p);
}
}
}
var dest = {};
if (pageNo<last) {
dest.path = domain+feed;
dest.query = nitro.queryFrom(nextHref,true); // TODO
dest.callback = processResponse;
}
// if we need to go somewhere else, e.g. after all pages received set callback and/or path
nitro.setReturn(dest);
return true;
}
//_____________________________________________________________________________
function dispatch(obj,payload) {
if (obj.nitro) {
processResponse(obj,payload);
}
else if (obj.fault) {
nitro.logFault(obj);
}
else if (obj.errors) {
nitro.logError(obj);
}
else {
console.log(obj);
}
return {};
}
//_____________________________________________________________________________
function processPid(host,path,api_key,pid) {
var query = nitro.newQuery();
query.add(api.fProgrammesPageSize,pageSize,true)
.add(api.mProgrammesContributions)
.add(api.mProgrammesDuration)
.add(api.mProgrammesAncestorTitles)
.add(api.mProgrammesAvailableVersions)
.add(api.mProgrammesGenreGroupings)
if (upcoming || children) {
query.add(api.fProgrammesDescendantsOf,pid)
.add(api.fProgrammesAvailabilityPending);
}
else {
query.add(api.fProgrammesPid,pid)
}
if (partner_pid != '') {
query.add(api.fProgrammesPartnerPid,partner_pid);
}
else {
if (pending) {
query.add(api.fProgrammesAvailability,'P30D');
}
else {
query.add(api.fProgrammesAvailabilityAvailable);
}
query.add(api.mProgrammesAvailability); // has a dependency on 'availability'
}
if (episode_type == 'clip') {
query.add(api.fProgrammesAvailabilityEntityTypeClip);
}
else if ((episode_type == 'brand') || (episode_type == 'series')) {
query.add(api.fProgrammesEntityType,episode_type);
}
else {
query.add(api.fProgrammesAvailabilityEntityTypeEpisode);
}
if (embargoed) {
query.add(api.xProgrammesEmbargoed,embargoed);
}
if (payment_type) {
query.add(api.fProgrammesPaymentType,payment_type);
}
nitro.make_request(host,path,api_key,query,{},function(obj){
return dispatch(obj);
});
}
//_____________________________________________________________________________
function processVpid(host,path,api_key,vpid) {
var query = nitro.newQuery();
query.add(api.fVersionsPid,vpid,true);
if (partner_pid != '') {
query.add(api.fVersionsPartnerPid,partner_pid);
}
else {
if (pending) {
query.add(api.fVersionsAvailability,'P30D');
}
else {
query.add(api.fVersionsAvailabilityAvailable);
}
}
if (embargoed) {
query.add(api.xProgrammesEmbargoed,embargoed);
}
nitro.make_request(host,api.nitroVersions,api_key,query,{},function(obj){
for (var i in obj.nitro.results.items) {
var item = obj.nitro.results.items[i];
var pid = item.version_of.pid;
processPid(host,path,api_key,pid)
}
});
}
//_____________________________________________________________________________
var scheduleResponse = function(obj) {
var nextHref = '';
if ((obj.nitro.pagination) && (obj.nitro.pagination.next)) {
nextHref = obj.nitro.pagination.next.href;
//console.log(nextHref);
}
var pageNo = obj.nitro.results.page;
var top = obj.nitro.results.total;
if (!top) {
top = obj.nitro.results.more_than+1;
}
var last = Math.ceil(top/obj.nitro.results.page_size);
//console.log('page '+pageNo+' of '+last);
process.stdout.write('.');
var length = 0;
if (obj.nitro.results.items) {
length = obj.nitro.results.items.length;
}
if (length==0) {
console.log('No results returned');
}
else {
for (var i in obj.nitro.results.items) {
var item = obj.nitro.results.items[i];
debuglog(item);
// minimally convert a broadcast into a programme for display
var p = {};
p.ancestor_titles = item.ancestor_titles;
p.available_versions = {};
p.available_versions.available = 1;
p.version = {};
p.version.duration = (item.published_time ? item.published_time.duration : '');
p.synopses = {};
p.media_type = (item.service.sid.indexOf('radio')>=0 ? 'Audio' : 'Video');
p.image = item.image;
p.master_brand = {};
p.master_brand.mid = item.service.sid; //!
p.release_date = item.published_time.start;
for (var b in item.broadcast_of) {
var bof = item.broadcast_of[b];
if ((bof.result_type == 'episode') || (bof.result_type == 'clip')) {
p.item_type = bof.result_type;
p.pid = bof.pid;
}
else if (bof.result_type == 'version') {
p.version.pid = bof.pid;
}
}
for (var a in item.ancestor_titles) {
var at = item.ancestor_titles[a];
if ((at.ancestor_type == 'episode') || (at.ancestor_type == 'clip')) {
p.title = at.title;
}
}
// item.ids.id is an array of submissions(?) to broadcast schedule services
var present = false;
for (var pc in programme_cache) {
var pci = programme_cache[pc];
if (pci.pid == p.pid) present = true;
}
if (!present) programme_cache.push(p);
}
}
var dest = {};
if (pageNo<last) {
dest.path = '/nitro/api/schedules';
dest.query = nitro.queryFrom(nextHref,true); // TODO
dest.callback = scheduleResponse;
}
// if we need to go somewhere else, e.g. after all pages received set callback and/or path
nitro.setReturn(dest);
return true;
}
//_____________________________________________________________________________
function processSchedule(host,api_key,category,format,mode,pid) {
var path = '/nitro/api/schedules';
var today = new Date();
var todayStr = today.toISOString();
var query = nitro.newQuery();
query.add(api.fSchedulesStartFrom,todayStr,true);
if (mode == 'genre') {
for (var c in category) {
query.add(api.fSchedulesGenre,category[c]);
}
}
for (var f in format) {
query.add(api.fSchedulesFormat,format[f]);
}
if (search != '') {
query.add(api.fSchedulesQ,search);
}
if (mode == 'pid') {
query.add(api.fSchedulesDescendantsOf,pid);
}
if (channel != '') {
query.add(api.fSchedulesServiceMasterBrand,channel);
}
if (partner_pid) {
query.add(api.fSchedulesPartnerPid,partner_pid);
}
if (sid) {
query.add(api.fSchedulesSid,sid);
}
query.add(api.mSchedulesAncestorTitles)
.add(api.fSchedulesPageSize,pageSize);
nitro.make_request(host,path,api_key,query,{},function(obj){
var result = scheduleResponse(obj);
var dest = nitro.getReturn();
if (dest.callback) {
// call the callback's next required destination
// e.g. second and subsequent pages
if (dest.path) {
nitro.make_request(host,dest.path,api_key,dest.query,{},dest.callback);
}
else {
dest.callback();
}
}
});
}
//------------------------------------------------------------------------[main]
// https://developer.bbc.co.uk/nitropubliclicence
// https://admin.live.bbc.co.uk/nitro/admin/servicestatus
// https://api.live.bbc.co.uk/nitro/api/schema
// https://confluence.dev.bbc.co.uk/display/nitro/Nitro+run+book
// http://www.bbc.co.uk/academy/technology/article/art20141013145843465
var defcat = 'all';
try {
var config = require('./config.json');
host = config.nitro.host;
api_key = config.nitro.api_key;
mediaSet = config.nitro.mediaset;
defcat = config.nitro.category;
}
catch (e) {
console.log('Please rename config.json.example to config.json and edit for your setup');
process.exit(2);
}
var category = [];
var format = [];
var query = nitro.newQuery();
var pid = '';
var mode = '';
var pending = false;
var episode_type = '';
var duration = '';
var path = domain+feed;
var options = getopt.create([
['h','help','display this help'],
['b','index_base','get_iplayer index base, defaults to 10000'],
['c','channel=ARG','Filter by channel (masterbrand) id'],
['d','domain=ARG','Set domain = radio,tv or both'],
['f','format=ARG+','Filter by format id'],
['g','genre=ARG+','Filter by genre id. all to reset'],
['l','linear=ARG','Set linear service id, works with -u only'],
['r','run=ARG','run-length, short,medium or long'],
['s','search=ARG','Search metadata. Can use title: or synopsis: prefix'],
['w','with=ARG','Filter with tag'],
['p','pid=ARG+','Query by individual pid(s), ignores options above'],
['v','version=ARG+','Query by individual version pid(s), ignores options above'],
['a','all','Show programme regardless of presence in download_history'],
['e','episode=ARG','Set programme type to episode*,clip,brand or series'],
['i','imminent','Set availability to future (default is available)'],
['k','children','Include children of given pid'],
['m','mediaset','Dump mediaset information, most useful with -p'],
['o','output','output in get_iplayer cache format'],
['t','payment_type=ARG','Set payment_type to free*,bbcstore,uscansvod'],
['u','upcoming','Show programme schedule information not history'],
['x','partner_pid=ARG','Set partner pid, defaults to s0000001'],
['z','embargoed=ARG','Set embargoed to include, exclude* or only']
]);
var o = options.bindHelp();
query.add(api.fProgrammesMediaSet,mediaSet,true);
options.on('all',function(){
showAll = true;
});
options.on('imminent',function(){
pending = true;
});
options.on('children',function(){
children = true;
});
options.on('domain',function(argv,options){
service = options.domain;
if (service == 'tv') {
media_type = 'audio_video';
}
else if (service == 'both') {
media_type = '';
}
});
options.on('version',function(argv,options){
mode = 'version';
});
options.on('upcoming',function(){
feed = 'schedules';
upcoming = true;
});
options.on('channel',function(argv,options){
channel = options.channel;
query.add(api.fProgrammesMasterBrand,channel).add(api.sProgrammesTitleAscending);
});
options.on('search',function(argv,options){
search = options.search;
query.add(api.fProgrammesQ,search).add(api.sProgrammesTitleAscending);
});
options.on('format',function(argv,options){
format = options.format;
for (var f in format) {
query.add(api.fProgrammesFormat,format[f]);
}
});
options.on('genre',function(argv,options){
mode = 'genre';
for (var g in options.genre) {
var genre = options.genre[g];
if (genre == 'all') {
category = [];
}
else {
category.push(genre);
query.add(api.fProgrammesGenre,genre);
}
}
});
options.on('mediaset',function(){
dumpMediaSets = true;
});
options.on('payment_type',function(argv,options){
payment_type = options.payment_type;
});
options.on('pid',function(argv,options){
mode = 'pid';
});
options.on('partner_pid',function(argv,options){
partner_pid = options.partner_pid;
});
options.on('linear',function(argv,options){
sid = options.linear;
});
options.on('embargoed',function(argv,options){
embargoed = options.embargoed;
});
options.on('with',function(argv,options){
tag = options.with;
});
options.on('episode',function(argv,options){
episode_type = options.episode;
if ((episode_type == 'brand') || (episode_type == 'series')) {
showAll = true;
}
});
options.on('run',function(argv,options){
duration = options.run;
});
var o = options.parseSystem();
if (!showAll && config.download_history) {
download_history = giUtils.downloadHistory(config.download_history);
}
if (partner_pid) {
query.add(api.fProgrammesPartnerPid,partner_pid)
.add(api.mProgrammesAvailability)
.add(api.mProgrammesAvailableVersions)
.add(api.fProgrammesAvailabilityAvailable);
}
else {
if (pending) {
query.add(api.fProgrammesAvailability,'P30D');
}
else {
query.add(api.fProgrammesAvailabilityAvailable);
}
query.add(api.mProgrammesAvailability)
.add(api.mProgrammesAvailableVersions)
.add(api.fProgrammesPaymentType,payment_type)
.add(api.fProgrammesAvailabilityTypeOndemand);
}
if (episode_type == 'clip') {
query.add(api.fProgrammesAvailabilityEntityTypeClip);
}
else if ((episode_type == 'brand') || (episode_type == 'series')) {
query.add(api.fProgrammesEntityType,episode_type);
}
else {
query.add(api.fProgrammesAvailabilityEntityTypeEpisode);
}
if (media_type) {
query.add(api.fProgrammesMediaType,media_type);
}
if (duration) {
query.add(api.fProgrammesDuration,duration);
}
if (embargoed) {
query.add(api.xProgrammesEmbargoed,embargoed);
}
if (tag) {
query.add(api.fProgrammesTagName,tag);
}
query.add(api.mProgrammesDuration)
.add(api.mProgrammesAncestorTitles)
.add(api.fProgrammesPageSize,pageSize);
if (mode=='') {
mode = 'genre';
category.push(defcat);
query.add(api.fProgrammesGenre,defcat);
}
if (mode == 'version') {
for (var p in o.options.version) {
pid = o.options.version[p];
if (pid.indexOf('@')==0) {
pid = pid.substr(1);
var s = fs.readFileSync(pid,'utf8');
pidList = pidList.concat(s.split('\n'));
}
else {
pidList.push(pid);
}
}
for (var p in pidList) {
processVpid(host,path,api_key,pidList[p]);
}
}
else if (mode == 'pid') {
for (var p in o.options.pid) {
pid = o.options.pid[p];
if (pid.indexOf('@')==0) {
pid = pid.substr(1);
var s = fs.readFileSync(pid,'utf8');
pidList = pidList.concat(s.split('\n'));
}
else {
pidList.push(pid);
}
}
for (var p in pidList) {
if (upcoming) {
processSchedule(host,api_key,category,format,mode,pidList[p]);
}
else {
processPid(host,path,api_key,pidList[p]);
}
}
}
else if (feed == 'schedules') {
processSchedule(host,api_key,category,format,mode);
}
else {
if (mode=='genre') {
// parallelize the queries by 36 times
var letters = '0123456789abcdefghijklmnopqrstuvwxyz';
for (var l in letters) {
if (letters.hasOwnProperty(l)) {
var lQuery = query.clone();
lQuery.add(api.fProgrammesInitialLetter,letters[l]);
nitro.make_request(host,path,api_key,lQuery,{},function(obj,payload){
return dispatch(obj,payload);
});
}
}
}
}
process.on('exit', function(code) {
if (o.options.output) {
pc_export();
}
else {
pc_dump();
if (nitro.getRateLimitEvents()>0) {
console.log('Got '+nitro.getRateLimitEvents()+' rate-limit events');
}
}
});