@stremio/stremio-video 0.0.17-rc.2 → 0.0.17-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
var url = require('url');
|
|
2
2
|
var magnet = require('magnet-uri');
|
|
3
|
-
var
|
|
3
|
+
var createTorrent = require('./createTorrent');
|
|
4
4
|
|
|
5
5
|
function convertStream(streamingServerURL, stream, seriesInfo) {
|
|
6
6
|
return new Promise(function(resolve, reject) {
|
|
7
|
+
var guessFileIdx = false;
|
|
8
|
+
if (stream.fileIdx === null || !isFinite(stream.fileIdx)) {
|
|
9
|
+
guessFileIdx = {};
|
|
10
|
+
if (seriesInfo) {
|
|
11
|
+
if (seriesInfo.season !== null && isFinite(seriesInfo.season)) {
|
|
12
|
+
guessFileIdx.season = seriesInfo.season;
|
|
13
|
+
}
|
|
14
|
+
if (seriesInfo.episode !== null && isFinite(seriesInfo.episode)) {
|
|
15
|
+
guessFileIdx.episode = seriesInfo.episode;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
7
20
|
if (typeof stream.url === 'string') {
|
|
8
21
|
if (stream.url.indexOf('magnet:') === 0) {
|
|
9
22
|
var parsedMagnetURI;
|
|
@@ -24,8 +37,9 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
|
|
|
24
37
|
})
|
|
25
38
|
:
|
|
26
39
|
[];
|
|
27
|
-
|
|
28
|
-
.then(function(
|
|
40
|
+
createTorrent(streamingServerURL, parsedMagnetURI.infoHash, sources, guessFileIdx)
|
|
41
|
+
.then(function(resp) {
|
|
42
|
+
var fileIdx = guessFileIdx ? resp.guessedFileIdx : stream.fileIdx;
|
|
29
43
|
resolve(url.resolve(streamingServerURL, '/' + encodeURIComponent(parsedMagnetURI.infoHash) + '/' + encodeURIComponent(fileIdx)));
|
|
30
44
|
})
|
|
31
45
|
.catch(function(error) {
|
|
@@ -39,17 +53,14 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
|
|
|
39
53
|
}
|
|
40
54
|
|
|
41
55
|
if (typeof stream.infoHash === 'string') {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
reject(error);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
56
|
+
createTorrent(streamingServerURL, stream.infoHash, stream.announce, guessFileIdx)
|
|
57
|
+
.then(function(resp) {
|
|
58
|
+
var fileIdx = guessFileIdx ? resp.guessedFileIdx : stream.fileIdx;
|
|
59
|
+
resolve(url.resolve(streamingServerURL, '/' + encodeURIComponent(stream.infoHash) + '/' + encodeURIComponent(fileIdx)));
|
|
60
|
+
})
|
|
61
|
+
.catch(function(error) {
|
|
62
|
+
reject(error);
|
|
63
|
+
});
|
|
53
64
|
|
|
54
65
|
return;
|
|
55
66
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var url = require('url');
|
|
2
|
+
|
|
3
|
+
function createTorrent(streamingServerURL, infoHash, sources, guessFileIdx) {
|
|
4
|
+
var body = {
|
|
5
|
+
torrent: {
|
|
6
|
+
infoHash: infoHash,
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
if (Array.isArray(sources) && sources.length > 0) {
|
|
10
|
+
body.torrent.peerSearch = {
|
|
11
|
+
sources: ['dht:' + infoHash].concat(sources),
|
|
12
|
+
min: 40,
|
|
13
|
+
max: 150
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
if (guessFileIdx) {
|
|
17
|
+
body.guessFileIdx = guessFileIdx;
|
|
18
|
+
}
|
|
19
|
+
return fetch(url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/create'), {
|
|
20
|
+
method: 'POST',
|
|
21
|
+
headers: {
|
|
22
|
+
'content-type': 'application/json'
|
|
23
|
+
},
|
|
24
|
+
body: JSON.stringify(body)
|
|
25
|
+
}).then(function(resp) {
|
|
26
|
+
return resp.json();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = createTorrent;
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
var url = require('url');
|
|
2
|
-
var parseVideoName = require('video-name-parser');
|
|
3
|
-
|
|
4
|
-
var MEDIA_FILE_EXTENTIONS = /.mkv$|.avi$|.mp4$|.wmv$|.vp8$|.mov$|.mpg$|.ts$|.m3u8$|.webm$|.flac$|.mp3$|.wav$|.wma$|.aac$|.ogg$/i;
|
|
5
|
-
|
|
6
|
-
function inferTorrentFileIdx(streamingServerURL, infoHash, sources, seriesInfo) {
|
|
7
|
-
return fetch(url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/create'), {
|
|
8
|
-
method: 'POST',
|
|
9
|
-
headers: {
|
|
10
|
-
'content-type': 'application/json'
|
|
11
|
-
},
|
|
12
|
-
body: JSON.stringify({
|
|
13
|
-
torrent: {
|
|
14
|
-
infoHash: infoHash,
|
|
15
|
-
peerSearch: {
|
|
16
|
-
sources: ['dht:' + infoHash].concat(Array.isArray(sources) ? sources : []),
|
|
17
|
-
min: 40,
|
|
18
|
-
max: 150
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
}).then(function(resp) {
|
|
23
|
-
if (resp.ok) {
|
|
24
|
-
return resp.json();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
throw new Error(resp.status + ' (' + resp.statusText + ')');
|
|
28
|
-
}).then(function(resp) {
|
|
29
|
-
if (!resp || !Array.isArray(resp.files) || resp.files.some(function(file) { return !file || typeof file.path !== 'string' || file.length === null || !isFinite(file.length); })) {
|
|
30
|
-
throw new Error('No files found in the torrent');
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
var mediaFiles = resp.files.filter(function(file) {
|
|
34
|
-
return file.path.match(MEDIA_FILE_EXTENTIONS);
|
|
35
|
-
});
|
|
36
|
-
if (mediaFiles.length === 0) {
|
|
37
|
-
throw new Error('No media files found in the torrent');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
var mediaFilesForEpisode = seriesInfo ?
|
|
41
|
-
mediaFiles.filter(function(file) {
|
|
42
|
-
try {
|
|
43
|
-
var info = parseVideoName(file.path);
|
|
44
|
-
return info.season !== null &&
|
|
45
|
-
isFinite(info.season) &&
|
|
46
|
-
info.season === seriesInfo.season &&
|
|
47
|
-
Array.isArray(info.episode) &&
|
|
48
|
-
info.episode.indexOf(seriesInfo.episode) !== -1;
|
|
49
|
-
} catch (e) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
:
|
|
54
|
-
[];
|
|
55
|
-
var selectedFile = (mediaFilesForEpisode.length > 0 ? mediaFilesForEpisode : mediaFiles)
|
|
56
|
-
.reduce(function(result, file) {
|
|
57
|
-
if (!result || file.length > result.length) {
|
|
58
|
-
return file;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return result;
|
|
62
|
-
}, null);
|
|
63
|
-
return resp.files.indexOf(selectedFile);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
module.exports = inferTorrentFileIdx;
|