-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnitro.js
243 lines (215 loc) · 7.47 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
var moment = require('moment-timezone');
var sdk = require('bbcparse/nitroSdk.js');
var api = require('bbcparse/nitroApi/api.js');
var common = require('./common.js');
var host = 'programmes.api.bbc.com';
var apikey = process.env.nitrokey || 'key';
function pubDate(item) {
var result = item.updated_time;
if (item.availability && item.availability.version_types &&
item.availability.version_types.version_type) {
result = item.availability.version_types.version_type.start;
}
return result;
}
function cacheKey(payload) {
return payload.domain+'/'+payload.prefix+'/'+payload.feed;
}
function saveNitroProgramme(payload,item) {
if (item.item_type != 'episode') return;
if ((payload.timeStamp - new Date(pubDate(item))) > payload.options.limit) return;
var prefix = (payload.prefix == 'available' ? '' : payload.prefix);
if (prefix == 'upcoming') {
prefix = 'Upcoming: ';
if (item.availability && item.availability.version_types && item.availability.version_types.version_type) {
if (item.availability.version_types.version_type.start) {
var start = new Date(item.availability.version_types.version_type.start);
var offset = 0;
if (item.version && item.version.duration) {
offset = sdk.iso8601durationToSeconds(item.version.duration);
}
if ((offset == 0) && item.available_versions && item.available_versions.version) {
for (var v of item.available_versions.version) {
if (offset == 0) offset = sdk.iso8601durationToSeconds(v.duration);
}
}
if (offset > 0) {
start = new Date(start - (offset * 1000)); // to msec
}
prefix = moment(start).tz('Europe/London').format('ddd MMM Do HH:mm z ');
}
}
}
var ancestors = '';
if (item.ancestor_titles) {
for (var a of item.ancestor_titles) {
ancestors += (ancestors ? ': ' : '') + a.title;
}
}
if (item.presentation_title && item.presentation_title.startsWith('Episode')) {
ancestors += ' ('+item.presentation_title+')';
}
if (ancestors) ancestors += ' ';
var suffix = '';
if (item.genre_groupings && item.genre_groupings.genre_group) {
for (var genre of item.genre_groupings.genre_group) {
if (genre.id == 'C00035') suffix = '#scifi';
if (genre.id == 'C00025') suffix = '#horror';
if (genre.id == 'C00032') suffix = '#psych';
if ((genre.id == 'C00193') || (genre.id == 'C00196')) suffix = '#comedy';
}
}
var p = {};
p.pid = item.pid;
p.actual_start = pubDate(item);
if (item.synopses) {
if (payload.options.short) {
if (p.title.toLowerCase().indexOf('podcast')>=0) return;
p.long_synopsis = item.synopses.short;
}
else {
p.long_synopsis = item.synopses.long ? item.synopses.long :
item.synopses.medium ? item.synopses.medium : item.synopses.short;
}
p.short_synopsis = p.long_synopsis||'';
if ((p.short_synopsis.length > 140) && (item.synopses.medium || item.synopses.short)) {
if (item.synopses.medium) p.short_synopsis = item.synopses.medium;
if ((p.short_synopsis.length > 140) && (item.synopses.short)) {
p.short_synopsis = item.synopses.short;
}
}
if (p.short_synopsis.length > 140) {
p.short_synopsis = p.short_synopsis.substr(0,137)+'...';
}
}
p.title = (prefix + ancestors + (item.title ? item.title : '') + ' ' + suffix + ' - '+p.short_synopsis).trim();
if (p.title.startsWith('@')) p.title = '.'+p.title;
p.image = {};
p.image.pid = item.images.image.href.split('=')[1];
p.media_type = item.media_type;
payload.results.push(p);
}
function processResults(payload,obj) {
if (obj.nitro && obj.nitro.results && obj.nitro.results.items) {
for (var i of obj.nitro.results.items) {
saveNitroProgramme(payload,i);
}
}
if (obj.nitro && obj.nitro.pagination && obj.nitro.pagination.next) {
var newQuery = sdk.queryFrom(obj.nitro.pagination.next.href,true);
sdk.make_request(host,api.nitroProgrammes,apikey,newQuery,{},function(obj){
processResults(payload,obj);
},
function(err){
common.finish(payload);
});
}
else {
if (payload.options.cache) {
var entry = {};
entry.results = payload.results;
entry.timeStamp = payload.timeStamp;
var key = cacheKey(payload);
if (!payload.options.cache[key]) {
console.log('Caching '+entry.results.length+' items for '+key);
}
payload.options.cache[key] = entry;
}
else {
common.finish(payload);
}
}
}
function programmesByCategory(req,res,options) {
//params are:
// service: rss or cache
// domain: radio, tv or both
// mode: genre, format, env or pid
// category:string
//options are:
// mode: string, overrides req.params.mode
// availability: string
if (!options.mode) options.mode = req.params.mode;
if (!options.limit) options.limit = common.MONTH;
var payload = {};
payload.res = res;
payload.finish = common.finish;
payload.domain = req.params.domain; //original not modified
payload.feed = req.params.category; // use from params once /progs goes away
payload.params = req.params;
payload.options = options;
payload.results = [];
payload.inFlight = 0;
payload.prefix = (options.availability == 'available' ? 'available' : 'upcoming');
payload.pidprefix = (options.availability == 'available' ? 'PID:' : 'uPID:');
payload.xmlOffset = 1; // 1 character already sent
payload.timeStamp = new Date();
res.set('Content-Type', 'text/xml');
res.write('<');
var query = options.query || sdk.newQuery();
query.add(api.fProgrammesPageSize,50,true);
query.add(api.mProgrammesAncestorTitles);
query.add(api.mProgrammesAvailableVersions);
query.add(api.fProgrammesAvailability,options.availability);
query.add(api.mProgrammesAvailability);
query.add(api.mProgrammesGenreGroupings);
query.add(api.fProgrammesAvailabilityEntityTypeEpisode);
//query.add(api.xProgrammesEmbargoedInclude);
if (options.mode != 'accessibility') {
var feed = req.params.category;
if (options.mode == 'env') {
feed = process.env[feed] || '';
//console.log(feed);
}
var feeds = feed.split(',');
if ((options.mode == 'pid') || (options.mode == 'env')) {
for (var feed of feeds) {
//console.log(feed);
query.add(api.fProgrammesDescendantsOf,feed);
}
}
else if (options.mode == 'format') {
for (var feed of feeds) {
query.add(api.fProgrammesFormat,feed);
}
}
else {
for (var feed of feeds) {
query.add(api.fProgrammesGenre,feed);
}
}
}
query.add(api.mProgrammesImages);
query.add(api.mProgrammesDuration);
if (req.params.category == 'signed') {
query.add(api.fProgrammesSignedExclusive);
}
if (req.params.category == 'audiodescribed') {
query.add(api.fProgrammesAudioDescribedTrue);
}
if (req.params.domain == 'tv') {
query.add(api.fProgrammesMediaTypeAudioVideo);
}
else if (req.params.domain == 'radio') {
query.add(api.fProgrammesMediaTypeAudio);
}
if (payload.options.cache) {
var key = cacheKey(payload);
if (payload.options.cache[key]) {
payload.results = payload.options.cache[key].results;
}
common.finish(payload);
payload.results = [];
}
sdk.make_request(host,api.nitroProgrammes,apikey,query,{},function(obj){
processResults(payload,obj);
},
function(err){
common.finish(payload);
});
common.updateHitCounter();
return true;
}
module.exports = {
programmesByCategory : programmesByCategory
};