From 9c92b2100feb0292d0b1544bd0189aba94d3adb4 Mon Sep 17 00:00:00 2001 From: TheBeastLT Date: Fri, 15 Mar 2024 20:27:16 +0200 Subject: [PATCH] sort debrid videos depending on filename matching --- addon/moch/alldebrid.js | 4 ++-- addon/moch/premiumize.js | 4 ++-- addon/moch/putio.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/addon/moch/alldebrid.js b/addon/moch/alldebrid.js index 5740aea..1df0ac0 100644 --- a/addon/moch/alldebrid.js +++ b/addon/moch/alldebrid.js @@ -142,10 +142,10 @@ async function _createTorrent(AD, infoHash) { async function _unrestrictLink(AD, torrent, encodedFileName, fileIndex) { const targetFileName = decodeURIComponent(encodedFileName); - const videos = torrent.links.filter(link => isVideo(link.filename)); + const videos = torrent.links.filter(link => isVideo(link.filename)).sort((a, b) => b.size - a.size); const targetVideo = Number.isInteger(fileIndex) ? videos.find(video => sameFilename(targetFileName, video.filename)) - : videos.sort((a, b) => b.size - a.size)[0]; + : videos[0]; if (!targetVideo && torrent.links.every(link => isArchive(link.filename))) { console.log(`Only AllDebrid archive is available for [${torrent.hash}] ${encodedFileName}`) diff --git a/addon/moch/premiumize.js b/addon/moch/premiumize.js index d1edafd..a96ea9c 100644 --- a/addon/moch/premiumize.js +++ b/addon/moch/premiumize.js @@ -124,10 +124,10 @@ async function _getCachedLink(PM, infoHash, encodedFileName, fileIndex, ip, isBr const cachedTorrent = await PM.transfer.directDownload(magnet.encode({ infoHash }), ip); if (cachedTorrent?.content?.length) { const targetFileName = decodeURIComponent(encodedFileName); - const videos = cachedTorrent.content.filter(file => isVideo(file.path)); + const videos = cachedTorrent.content.filter(file => isVideo(file.path)).sort((a, b) => b.size - a.size); const targetVideo = Number.isInteger(fileIndex) ? videos.find(video => sameFilename(video.path, targetFileName)) - : videos.sort((a, b) => b.size - a.size)[0]; + : videos[0]; if (!targetVideo && videos.every(video => isArchive(video.path))) { console.log(`Only Premiumize archive is available for [${infoHash}] ${fileIndex}`) return StaticResponse.FAILED_RAR; diff --git a/addon/moch/putio.js b/addon/moch/putio.js index 2957f82..58eb778 100644 --- a/addon/moch/putio.js +++ b/addon/moch/putio.js @@ -168,12 +168,12 @@ async function _getTargetFile(Putio, torrent, encodedFileName, fileIndex) { while (!targetFile && files.length) { const folders = files.filter(file => file.file_type === 'FOLDER'); - videos = videos.concat(files.filter(file => isVideo(file.name))); + videos = videos.concat(files.filter(file => isVideo(file.name))).sort((a, b) => b.size - a.size); // when specific file index is defined search by filename // when it's not defined find all videos and take the largest one targetFile = Number.isInteger(fileIndex) ? videos.find(video => sameFilename(targetFileName, video.name)) - : !folders.length && videos.sort((a, b) => b.size - a.size)[0]; + : !folders.length && videos[0]; files = !targetFile ? await Promise.all(folders.map(folder => _getFiles(Putio, folder.id))) .then(results => results.reduce((a, b) => a.concat(b), []))