Skip to content

Commit

Permalink
Merge pull request #20 from Stremio/guess-file-idx-enginefs
Browse files Browse the repository at this point in the history
Guess file idx enginefs
  • Loading branch information
nklhtv authored Apr 18, 2022
2 parents 48c8813 + 42e4980 commit 4bb605b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 81 deletions.
39 changes: 25 additions & 14 deletions src/withStreamingServer/convertStream.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
var url = require('url');
var magnet = require('magnet-uri');
var inferTorrentFileIdx = require('./inferTorrentFileIdx');
var createTorrent = require('./createTorrent');

function convertStream(streamingServerURL, stream, seriesInfo) {
return new Promise(function(resolve, reject) {
var guessFileIdx = false;
if (stream.fileIdx === null || !isFinite(stream.fileIdx)) {
guessFileIdx = {};
if (seriesInfo) {
if (seriesInfo.season !== null && isFinite(seriesInfo.season)) {
guessFileIdx.season = seriesInfo.season;
}
if (seriesInfo.episode !== null && isFinite(seriesInfo.episode)) {
guessFileIdx.episode = seriesInfo.episode;
}
}
}

if (typeof stream.url === 'string') {
if (stream.url.indexOf('magnet:') === 0) {
var parsedMagnetURI;
Expand All @@ -24,8 +37,9 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
})
:
[];
inferTorrentFileIdx(streamingServerURL, parsedMagnetURI.infoHash, sources, seriesInfo)
.then(function(fileIdx) {
createTorrent(streamingServerURL, parsedMagnetURI.infoHash, sources, guessFileIdx)
.then(function(resp) {
var fileIdx = guessFileIdx ? resp.guessedFileIdx : stream.fileIdx;
resolve(url.resolve(streamingServerURL, '/' + encodeURIComponent(parsedMagnetURI.infoHash) + '/' + encodeURIComponent(fileIdx)));
})
.catch(function(error) {
Expand All @@ -39,17 +53,14 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
}

if (typeof stream.infoHash === 'string') {
if (stream.fileIdx !== null && isFinite(stream.fileIdx)) {
resolve(url.resolve(streamingServerURL, '/' + encodeURIComponent(stream.infoHash) + '/' + encodeURIComponent(stream.fileIdx)));
} else {
inferTorrentFileIdx(streamingServerURL, stream.infoHash, stream.announce, seriesInfo)
.then(function(fileIdx) {
resolve(url.resolve(streamingServerURL, '/' + encodeURIComponent(stream.infoHash) + '/' + encodeURIComponent(fileIdx)));
})
.catch(function(error) {
reject(error);
});
}
createTorrent(streamingServerURL, stream.infoHash, stream.announce, guessFileIdx)
.then(function(resp) {
var fileIdx = guessFileIdx ? resp.guessedFileIdx : stream.fileIdx;
resolve(url.resolve(streamingServerURL, '/' + encodeURIComponent(stream.infoHash) + '/' + encodeURIComponent(fileIdx)));
})
.catch(function(error) {
reject(error);
});

return;
}
Expand Down
30 changes: 30 additions & 0 deletions src/withStreamingServer/createTorrent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var url = require('url');

function createTorrent(streamingServerURL, infoHash, sources, guessFileIdx) {
var body = {
torrent: {
infoHash: infoHash,
}
};
if (Array.isArray(sources) && sources.length > 0) {
body.torrent.peerSearch = {
sources: ['dht:' + infoHash].concat(sources),
min: 40,
max: 150
};
}
if (guessFileIdx) {
body.guessFileIdx = guessFileIdx;
}
return fetch(url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/create'), {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(body)
}).then(function(resp) {
return resp.json();
});
}

module.exports = createTorrent;
67 changes: 0 additions & 67 deletions src/withStreamingServer/inferTorrentFileIdx.js

This file was deleted.

0 comments on commit 4bb605b

Please sign in to comment.