From d69019293e18fa005983ea0dcf51c2e380ef0bc1 Mon Sep 17 00:00:00 2001 From: Alexandru Branza Date: Sat, 16 Apr 2022 01:30:40 +0300 Subject: [PATCH 1/6] Guess File Idx in EngineFS --- src/withStreamingServer/convertStream.js | 27 ++++++++++++--------- src/withStreamingServer/createTorrent.js | 30 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/withStreamingServer/createTorrent.js diff --git a/src/withStreamingServer/convertStream.js b/src/withStreamingServer/convertStream.js index 4a8a210..dfc4624 100644 --- a/src/withStreamingServer/convertStream.js +++ b/src/withStreamingServer/convertStream.js @@ -1,6 +1,6 @@ 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) { @@ -39,17 +39,22 @@ 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); - }); + var guessFileIdx = false; + if (stream.fileIdx === null || !isFinite(stream.fileIdx)) { + guessFileIdx = {}; + if (seriesInfo && (seriesInfo.season || seriesInfo.episode)) { + guessFileIdx.season = seriesInfo.season; + guessFileIdx.episode = seriesInfo.episode; + } } + 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; } diff --git a/src/withStreamingServer/createTorrent.js b/src/withStreamingServer/createTorrent.js new file mode 100644 index 0000000..144be63 --- /dev/null +++ b/src/withStreamingServer/createTorrent.js @@ -0,0 +1,30 @@ +var url = require('url'); + +function createTorrent(streamingServerURL, infoHash, sources, guessFileIdx) { + var body = { + torrent: { + infoHash: infoHash, + } + }; + if ((sources || []).length) { + body.torrent.peerSearch = { + sources: ['dht:' + infoHash].concat(Array.isArray(sources) ? 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; From 31d924fd3abeb8c0d60f3ef984457db88f90a042 Mon Sep 17 00:00:00 2001 From: Alexandru Branza Date: Sat, 16 Apr 2022 01:40:14 +0300 Subject: [PATCH 2/6] Add Support for Magnets --- src/withStreamingServer/convertStream.js | 25 +++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/withStreamingServer/convertStream.js b/src/withStreamingServer/convertStream.js index dfc4624..c6a3bd3 100644 --- a/src/withStreamingServer/convertStream.js +++ b/src/withStreamingServer/convertStream.js @@ -4,6 +4,16 @@ 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 && (seriesInfo.season || seriesInfo.episode)) { + guessFileIdx.season = seriesInfo.season; + guessFileIdx.episode = seriesInfo.episode; + } + } + if (typeof stream.url === 'string') { if (stream.url.indexOf('magnet:') === 0) { var parsedMagnetURI; @@ -24,8 +34,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) { @@ -39,17 +50,9 @@ function convertStream(streamingServerURL, stream, seriesInfo) { } if (typeof stream.infoHash === 'string') { - var guessFileIdx = false; - if (stream.fileIdx === null || !isFinite(stream.fileIdx)) { - guessFileIdx = {}; - if (seriesInfo && (seriesInfo.season || seriesInfo.episode)) { - guessFileIdx.season = seriesInfo.season; - guessFileIdx.episode = seriesInfo.episode; - } - } createTorrent(streamingServerURL, stream.infoHash, stream.announce, guessFileIdx) .then(function(resp) { - var fileIdx = guessFileIdx ? resp.guessedFileIdx : stream.fileIdx + var fileIdx = guessFileIdx ? resp.guessedFileIdx : stream.fileIdx; resolve(url.resolve(streamingServerURL, '/' + encodeURIComponent(stream.infoHash) + '/' + encodeURIComponent(fileIdx))); }) .catch(function(error) { From f6243fa1ddc369cf28dcea3752b1ab3d9a6cc68c Mon Sep 17 00:00:00 2001 From: Alexandru Branza Date: Sat, 16 Apr 2022 01:44:18 +0300 Subject: [PATCH 3/6] Missing Semicolon --- src/withStreamingServer/createTorrent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/withStreamingServer/createTorrent.js b/src/withStreamingServer/createTorrent.js index 144be63..c96e66b 100644 --- a/src/withStreamingServer/createTorrent.js +++ b/src/withStreamingServer/createTorrent.js @@ -24,7 +24,7 @@ function createTorrent(streamingServerURL, infoHash, sources, guessFileIdx) { body: JSON.stringify(body) }).then(function(resp) { return resp.json(); - }) + }); } module.exports = createTorrent; From 51d09f42bb18d20f2e5aedca4cb010171df6ff2e Mon Sep 17 00:00:00 2001 From: nklhrstv Date: Mon, 18 Apr 2022 18:14:04 +0300 Subject: [PATCH 4/6] inferTorrentFileIdx removed --- .../inferTorrentFileIdx.js | 67 ------------------- 1 file changed, 67 deletions(-) delete mode 100644 src/withStreamingServer/inferTorrentFileIdx.js diff --git a/src/withStreamingServer/inferTorrentFileIdx.js b/src/withStreamingServer/inferTorrentFileIdx.js deleted file mode 100644 index 114b50d..0000000 --- a/src/withStreamingServer/inferTorrentFileIdx.js +++ /dev/null @@ -1,67 +0,0 @@ -var url = require('url'); -var parseVideoName = require('video-name-parser'); - -var MEDIA_FILE_EXTENTIONS = /.mkv$|.avi$|.mp4$|.wmv$|.vp8$|.mov$|.mpg$|.ts$|.m3u8$|.webm$|.flac$|.mp3$|.wav$|.wma$|.aac$|.ogg$/i; - -function inferTorrentFileIdx(streamingServerURL, infoHash, sources, seriesInfo) { - return fetch(url.resolve(streamingServerURL, '/' + encodeURIComponent(infoHash) + '/create'), { - method: 'POST', - headers: { - 'content-type': 'application/json' - }, - body: JSON.stringify({ - torrent: { - infoHash: infoHash, - peerSearch: { - sources: ['dht:' + infoHash].concat(Array.isArray(sources) ? sources : []), - min: 40, - max: 150 - } - } - }) - }).then(function(resp) { - if (resp.ok) { - return resp.json(); - } - - throw new Error(resp.status + ' (' + resp.statusText + ')'); - }).then(function(resp) { - if (!resp || !Array.isArray(resp.files) || resp.files.some(function(file) { return !file || typeof file.path !== 'string' || file.length === null || !isFinite(file.length); })) { - throw new Error('No files found in the torrent'); - } - - var mediaFiles = resp.files.filter(function(file) { - return file.path.match(MEDIA_FILE_EXTENTIONS); - }); - if (mediaFiles.length === 0) { - throw new Error('No media files found in the torrent'); - } - - var mediaFilesForEpisode = seriesInfo ? - mediaFiles.filter(function(file) { - try { - var info = parseVideoName(file.path); - return info.season !== null && - isFinite(info.season) && - info.season === seriesInfo.season && - Array.isArray(info.episode) && - info.episode.indexOf(seriesInfo.episode) !== -1; - } catch (e) { - return false; - } - }) - : - []; - var selectedFile = (mediaFilesForEpisode.length > 0 ? mediaFilesForEpisode : mediaFiles) - .reduce(function(result, file) { - if (!result || file.length > result.length) { - return file; - } - - return result; - }, null); - return resp.files.indexOf(selectedFile); - }); -} - -module.exports = inferTorrentFileIdx; From 503e8a171842d6c118ed605a3d31f1d26e1d9990 Mon Sep 17 00:00:00 2001 From: nklhrstv Date: Mon, 18 Apr 2022 18:29:42 +0300 Subject: [PATCH 5/6] improve array checking --- src/withStreamingServer/createTorrent.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/withStreamingServer/createTorrent.js b/src/withStreamingServer/createTorrent.js index c96e66b..0719d83 100644 --- a/src/withStreamingServer/createTorrent.js +++ b/src/withStreamingServer/createTorrent.js @@ -6,9 +6,9 @@ function createTorrent(streamingServerURL, infoHash, sources, guessFileIdx) { infoHash: infoHash, } }; - if ((sources || []).length) { + if (Array.isArray(sources) && sources.length > 0) { body.torrent.peerSearch = { - sources: ['dht:' + infoHash].concat(Array.isArray(sources) ? sources : []), + sources: ['dht:' + infoHash].concat(sources), min: 40, max: 150 }; From 42e498009ce74e94809ec95f9652f2ce17caf5ab Mon Sep 17 00:00:00 2001 From: nklhrstv Date: Mon, 18 Apr 2022 18:33:27 +0300 Subject: [PATCH 6/6] improve season/episode validation --- src/withStreamingServer/convertStream.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/withStreamingServer/convertStream.js b/src/withStreamingServer/convertStream.js index c6a3bd3..ef214d9 100644 --- a/src/withStreamingServer/convertStream.js +++ b/src/withStreamingServer/convertStream.js @@ -4,13 +4,16 @@ 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 && (seriesInfo.season || seriesInfo.episode)) { - guessFileIdx.season = seriesInfo.season; - guessFileIdx.episode = seriesInfo.episode; + if (seriesInfo) { + if (seriesInfo.season !== null && isFinite(seriesInfo.season)) { + guessFileIdx.season = seriesInfo.season; + } + if (seriesInfo.episode !== null && isFinite(seriesInfo.episode)) { + guessFileIdx.episode = seriesInfo.episode; + } } }