-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfetchVideoParams.js
95 lines (81 loc) · 3.37 KB
/
fetchVideoParams.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
var url = require('url');
function fetchOpensubtitlesParams(streamingServerURL, mediaURL, behaviorHints) {
var hash = behaviorHints && typeof behaviorHints.videoHash === 'string' ? behaviorHints.videoHash : null;
var size = behaviorHints && isFinite(behaviorHints.videoSize) ? behaviorHints.videoSize : null;
if (typeof hash === 'string' && size !== null && isFinite(size)) {
return Promise.resolve({ hash: hash, size: size });
}
var queryParams = new URLSearchParams([['videoUrl', mediaURL]]);
return fetch(url.resolve(streamingServerURL, '/opensubHash?' + queryParams.toString()))
.then(function(resp) {
if (resp.ok) {
return resp.json();
}
throw new Error(resp.status + ' (' + resp.statusText + ')');
})
.then(function(resp) {
if (resp.error) {
throw new Error(resp.error);
}
return {
hash: typeof hash === 'string' ?
hash
:
resp.result && typeof resp.result.hash === 'string' ?
resp.result.hash
:
null,
size: size !== null && isFinite(size) ?
size
:
resp.result && typeof resp.result.size ?
resp.result.size
:
null
};
});
}
function fetchFilename(streamingServerURL, mediaURL, infoHash, fileIdx, behaviorHints) {
if (behaviorHints && typeof behaviorHints.filename === 'string') {
return Promise.resolve(behaviorHints.filename);
}
if (infoHash) {
return fetch(url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/' + encodeURIComponent(fileIdx) + '/stats.json'))
.then(function(resp) {
if (resp.ok) {
return resp.json();
}
throw new Error(resp.status + ' (' + resp.statusText + ')');
})
.then(function(resp) {
if (!resp || typeof resp.streamName !== 'string') {
throw new Error('Could not retrieve filename from torrent');
}
return resp.streamName;
});
}
return Promise.resolve(decodeURIComponent(mediaURL.split('/').pop()));
}
function fetchVideoParams(streamingServerURL, mediaURL, infoHash, fileIdx, behaviorHints) {
return Promise.allSettled([
fetchOpensubtitlesParams(streamingServerURL, mediaURL, behaviorHints),
fetchFilename(streamingServerURL, mediaURL, infoHash, fileIdx, behaviorHints)
]).then(function(results) {
var result = { hash: null, size: null, filename: null };
if (results[0].status === 'fulfilled') {
result.hash = results[0].value.hash;
result.size = results[0].value.size;
} else if (results[0].reason) {
// eslint-disable-next-line no-console
console.error(results[0].reason);
}
if (results[1].status === 'fulfilled') {
result.filename = results[1].value;
} else if (results[1].reason) {
// eslint-disable-next-line no-console
console.error(results[1].reason);
}
return result;
});
}
module.exports = fetchVideoParams;