-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (50 loc) · 1.67 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
'use strict';
'use strong';
const request = require('request');
const stream = require('stream');
const util = require('util');
function DrVideoStream(urn) {
if (!(this instanceof DrVideoStream)) return new DrVideoStream(urn);
stream.PassThrough.call(this);
const self = this;
this._fetchProgramcard(urn, function (err, programcard) {
if (err) return self.emit('error', err);
if (!programcard.PrimaryAsset) {
return self.emit('error', new Error('could not assert information'));
}
self._fetchManifest(programcard, function (err, manifest) {
if (err) return self.emit('error', err);
const source = self._selectBestSource(manifest);
self._startPipe(source);
});
});
}
util.inherits(DrVideoStream, stream.PassThrough);
DrVideoStream.prototype._fetchProgramcard = function (urn, callback) {
request(`http://www.dr.dk/mu-online/api/1.3/programcard/${urn}`, function (err, res, content) {
if (err) return callback(err);
callback(null, JSON.parse(content));
});
};
DrVideoStream.prototype._fetchManifest = function (programcard, callback) {
request(programcard.PrimaryAsset.Uri, function (err, res, content) {
if (err) return callback(err);
callback(null, JSON.parse(content));
});
};
DrVideoStream.prototype._selectBestSource = function (manifest) {
let bestBitrate = 0;
let bestSource = null;
for (const source of manifest.Links) {
const bitrate = source.Bitrate | 0;
if (bitrate > bestBitrate) {
bestBitrate = bitrate;
bestSource = source;
}
}
return bestSource;
};
DrVideoStream.prototype._startPipe = function (source) {
request(source.Uri).pipe(this);
};
module.exports = DrVideoStream;