-
Notifications
You must be signed in to change notification settings - Fork 3
/
liveTv.js
137 lines (121 loc) · 3.47 KB
/
liveTv.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
var sdk = require('./nitroSdk.js');
var api = require('./nitroApi/api.js');
// https://ess.api.bbci.co.uk/services
// http://ess.api.bbci.co.uk/schedules?serviceId=bbc_one_london
// http://ess.api.bbci.co.uk/schedules?versionId={vpid}
// http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/vpid/bbc_one_hd/mediaset/stb-hd-h264
var fs = require('fs');
var refs = {};
var dash = {};
var channels = require('./ess/services.json');
channels.items = channels.items.sort(function(a,b){
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
function clone(obj){
return JSON.parse(JSON.stringify(obj));
}
function uniqueOnly(value, index, self) {
return self.indexOf(value) === index;
}
function cmp(a,b){
var widthA = Number(a.split('x')[0]);
var widthB = Number(b.split('x')[0]);
if (widthA > widthB) return -1;
if (widthA < widthB) return 1;
return 0;
}
function getHref(refs,key){
for (var href in refs) {
if (refs[href].key == key) return refs[href];
}
return null;
}
var infile = process.argv[2];
if (infile) {
var query = sdk.newQuery();
var key = '';
var options = {};
options.headers = {};
options.headers.Accept = 'application/json';
var lines = fs.readFileSync(infile,'utf8').split('\n');
for (var line of lines) {
var fields = line.split(' ');
var vpid = fields[0];
var desc = fields[1];
var mediaset = fields[2];
if (mediaset) {
console.log('http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/vpid/bbc_one_hd/mediaset/'+mediaset);
var path = '/mediaselector/5/select/version/2.0/vpid/bbc_one_hd/mediaset/'+mediaset+'/format/json';
options.payload = {};
options.payload.mediaset = mediaset;
sdk.make_request('open.live.bbc.co.uk',path,key,query,options,function(obj,payload){
//console.log(JSON.stringify(obj,null,2));
if (obj.media) {
for (var media of obj.media) {
var cm = clone(media);
delete cm.connection;
for (var conn of media.connection) {
conn.media = cm;
conn.key = cm.width+'x'+cm.height+'@'+cm.bitrate+'/'+conn.supplier.substr(0,2);
conn.mediaset = payload.mediaset;
if ((conn.href.indexOf('m3u8')>=0) && (conn.href.indexOf('https:')<0)) {
refs[conn.href] = conn;
}
if ((conn.href.indexOf('.mpd')>0) && (!dash[conn.href])) {
dash[conn.href] = conn;
}
}
}
}
},
function(statusCode,data,payload){
console.log('Err '+payload.mediaset);
});
}
}
}
function output(refs) {
console.log(JSON.stringify(refs,null,2));
var keys = [];
for (var href in refs) {
keys.push(refs[href].key);
}
keys = keys.sort(cmp);
keys = keys.filter(uniqueOnly);
var s = '';
s += '|Channel|';
for (var key of keys) {
s += key + '|';
}
s += '\n';
s += '|---|';
for (var key of keys) {
s += '---|';
}
s += '\n';
for (var channel of channels.items) {
if (((channel.type == 'Simulcast') || (channel.type == 'TV') ||
(channel.type == 'Webcast')) && (channel.mediaType == 'Video')) {
s += channel.name + '|';
for (var key of keys) {
var href = getHref(refs,key);
if (href) {
var url = href.href.replace('bbc_one_hd',channel.id);
if (channel.type == 'Webcast') {
url = url.replace('simulcast','webcast');
}
s += '['+href.mediaset+']('+url+')|';
}
else s += '|';
}
s += '\n';
}
}
return s;
}
process.on('exit',function(code){
fs.writeFileSync('./markdown/liveTv.md',output(refs),'utf8');
fs.writeFileSync('./markdown/liveDash.md',output(dash),'utf8');
});