Skip to content

Commit

Permalink
Merge pull request #40 from Stremio/add-filename-to-video-params
Browse files Browse the repository at this point in the history
Add Filename to Video Params
  • Loading branch information
jaruba authored May 23, 2023
2 parents 9150139 + 73c6444 commit 2453870
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/withStreamingServer/convertStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
:
[];
createTorrent(streamingServerURL, parsedMagnetURI.infoHash, null, sources, seriesInfo)
.then(function(url) {
resolve(url);
.then(function(torrent) {
resolve({ url: torrent.url, infoHash: torrent.infoHash, fileIdx: torrent.fileIdx });
})
.catch(function(error) {
reject(error);
});
} else {
resolve(stream.url);
resolve({ url: stream.url });
}

return;
}

if (typeof stream.infoHash === 'string') {
createTorrent(streamingServerURL, stream.infoHash, stream.fileIdx, stream.announce, seriesInfo)
.then(function(url) {
resolve(url);
.then(function(torrent) {
resolve({ url: torrent.url, infoHash: torrent.infoHash, fileIdx: torrent.fileIdx });
})
.catch(function(error) {
reject(error);
Expand Down
13 changes: 9 additions & 4 deletions src/withStreamingServer/createTorrent.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
var url = require('url');

function buildTorrentUrl(streamingServerURL, infoHash, fileIdx, sources) {
function buildTorrent(streamingServerURL, infoHash, fileIdx, sources) {
var query = Array.isArray(sources) && sources.length > 0 ?
'?' + new URLSearchParams(sources.map(function(source) {
return ['tr', source];
}))
:
'';
return url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/' + encodeURIComponent(fileIdx)) + query;
return {
url: url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/' + encodeURIComponent(fileIdx)) + query,
infoHash: infoHash,
fileIdx: fileIdx,
sources: sources
};
}

function createTorrent(streamingServerURL, infoHash, fileIdx, sources, seriesInfo) {
if ((!Array.isArray(sources) || sources.length === 0) && (fileIdx !== null && isFinite(fileIdx))) {
return Promise.resolve(buildTorrentUrl(streamingServerURL, infoHash, fileIdx, sources));
return Promise.resolve(buildTorrent(streamingServerURL, infoHash, fileIdx, sources));
}

var body = {
Expand Down Expand Up @@ -58,7 +63,7 @@ function createTorrent(streamingServerURL, infoHash, fileIdx, sources, seriesInf

throw new Error(resp.status + ' (' + resp.statusText + ')');
}).then(function(resp) {
return buildTorrentUrl(streamingServerURL, infoHash, body.guessFileIdx ? resp.guessedFileIdx : fileIdx, body.peerSearch ? body.peerSearch.sources : []);
return buildTorrent(streamingServerURL, infoHash, body.guessFileIdx ? resp.guessedFileIdx : fileIdx, body.peerSearch ? body.peerSearch.sources : []);
});
}

Expand Down
77 changes: 75 additions & 2 deletions src/withStreamingServer/fetchVideoParams.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
var url = require('url');

function fetchVideoParams(streamingServerURL, mediaURL) {
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) {
Expand All @@ -15,8 +21,75 @@ function fetchVideoParams(streamingServerURL, mediaURL) {
throw new Error(resp.error);
}

return resp.result;
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;
13 changes: 10 additions & 3 deletions src/withStreamingServer/withStreamingServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ function withStreamingServer(Video) {
loadArgs = commandArgs;
onPropChanged('stream');
convertStream(commandArgs.streamingServerURL, commandArgs.stream, commandArgs.seriesInfo)
.then(function(mediaURL) {
.then(function(result) {
var mediaURL = result.url;
var infoHash = result.infoHash;
var fileIdx = result.fileIdx;
var formats = Array.isArray(commandArgs.formats) ?
commandArgs.formats
:
Expand Down Expand Up @@ -135,6 +138,8 @@ function withStreamingServer(Video) {
if (canPlay) {
return {
mediaURL: mediaURL,
infoHash: infoHash,
fileIdx: fileIdx,
stream: {
url: mediaURL
}
Expand All @@ -159,6 +164,8 @@ function withStreamingServer(Video) {

return {
mediaURL: mediaURL,
infoHash: infoHash,
fileIdx: fileIdx,
stream: {
url: url.resolve(commandArgs.streamingServerURL, '/hlsv2/' + id + '/master.m3u8?' + queryParams.toString()),
subtitles: Array.isArray(commandArgs.stream.subtitles) ?
Expand Down Expand Up @@ -195,7 +202,7 @@ function withStreamingServer(Video) {
});
loaded = true;
flushActionsQueue();
fetchVideoParams(commandArgs.streamingServerURL, result.mediaURL)
fetchVideoParams(commandArgs.streamingServerURL, result.mediaURL, result.infoHash, result.fileIdx, commandArgs.stream.behaviorHints)
.then(function(result) {
if (commandArgs !== loadArgs) {
return;
Expand All @@ -211,7 +218,7 @@ function withStreamingServer(Video) {

// eslint-disable-next-line no-console
console.error(error);
videoParams = { hash: null, size: null };
videoParams = { hash: null, size: null, filename: null };
onPropChanged('videoParams');
});
})
Expand Down
5 changes: 4 additions & 1 deletion src/withVideoParams/withVideoParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ function withVideoParams(Video) {
return null;
}

return { hash: null, size: null };
var hash = stream.behaviorHints && typeof stream.behaviorHints.videoHash === 'string' ? stream.behaviorHints.videoHash : null;
var size = stream.behaviorHints && stream.behaviorHints.videoSize !== null && isFinite(stream.behaviorHints.videoSize) ? stream.behaviorHints.videoSize : null;
var filename = stream.behaviorHints && typeof stream.behaviorHints.filename === 'string' ? stream.behaviorHints.filename : null;
return { hash: hash, size: size, filename: filename };
}
default: {
return videoPropValue;
Expand Down

0 comments on commit 2453870

Please sign in to comment.