diff --git a/addon/moch/alldebrid.js b/addon/moch/alldebrid.js index 5301d95..ed96ba1 100644 --- a/addon/moch/alldebrid.js +++ b/addon/moch/alldebrid.js @@ -3,7 +3,7 @@ import { Type } from '../lib/types.js'; import { isVideo, isArchive } from '../lib/extension.js'; import StaticResponse from './static.js'; import { getMagnetLink } from '../lib/magnetHelper.js'; -import { BadTokenError, AccessDeniedError, sameFilename } from './mochHelper.js'; +import { BadTokenError, AccessDeniedError, sameFilename, streamFilename } from './mochHelper.js'; const KEY = 'alldebrid'; const AGENT = 'torrentio'; @@ -23,12 +23,9 @@ export async function getCachedStreams(streams, apiKey, ip) { return available?.data?.magnets && streams .reduce((mochStreams, stream) => { const cachedEntry = available.data.magnets.find(magnet => stream.infoHash === magnet.hash.toLowerCase()); - const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); - const fileName = streamTitleParts[streamTitleParts.length - 1]; - const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; - const encodedFileName = encodeURIComponent(fileName); + const filename = streamFilename(stream); mochStreams[`${stream.infoHash}@${stream.fileIdx}`] = { - url: `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`, + url: `${apiKey}/${stream.infoHash}/${filename}/${stream.fileIdx}`, cached: cachedEntry?.instant } return mochStreams; @@ -144,8 +141,8 @@ async function _unrestrictLink(AD, torrent, encodedFileName, fileIndex) { const targetFileName = decodeURIComponent(encodedFileName); 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[0]; + && videos.find(video => sameFilename(targetFileName, video.filename)) + || 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/mochHelper.js b/addon/moch/mochHelper.js index 410dba9..0e2da69 100644 --- a/addon/moch/mochHelper.js +++ b/addon/moch/mochHelper.js @@ -11,8 +11,8 @@ export function chunkArray(arr, size) { } export function streamFilename(stream) { - const titleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); - const filename = titleParts.pop().split('/').pop(); + const filename = stream?.behaviorHints?.filename + || stream.title.replace(/\n👤.*/s, '').split('\n').pop().split('/').pop(); return encodeURIComponent(filename) } diff --git a/addon/moch/offcloud.js b/addon/moch/offcloud.js index e7c6a53..61757cd 100644 --- a/addon/moch/offcloud.js +++ b/addon/moch/offcloud.js @@ -4,7 +4,7 @@ import { Type } from '../lib/types.js'; import { isVideo } from '../lib/extension.js'; import StaticResponse from './static.js'; import { getMagnetLink } from '../lib/magnetHelper.js'; -import { chunkArray, BadTokenError, sameFilename } from './mochHelper.js'; +import { chunkArray, BadTokenError, sameFilename, streamFilename } from './mochHelper.js'; const KEY = 'offcloud'; @@ -25,11 +25,9 @@ export async function getCachedStreams(streams, apiKey) { return available && streams .reduce((mochStreams, stream) => { const isCached = available.includes(stream.infoHash); - const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); - const fileName = streamTitleParts[streamTitleParts.length - 1]; - const encodedFileName = encodeURIComponent(fileName); + const fileName = streamFilename(stream); mochStreams[`${stream.infoHash}@${stream.fileIdx}`] = { - url: `${apiKey}/${stream.infoHash}/${encodedFileName}/${stream.fileIdx}`, + url: `${apiKey}/${stream.infoHash}/${fileName}/${stream.fileIdx}`, cached: isCached }; return mochStreams; diff --git a/addon/moch/premiumize.js b/addon/moch/premiumize.js index a96ea9c..f26d7c2 100644 --- a/addon/moch/premiumize.js +++ b/addon/moch/premiumize.js @@ -4,7 +4,7 @@ import { Type } from '../lib/types.js'; import { isVideo, isArchive } from '../lib/extension.js'; import StaticResponse from './static.js'; import { getMagnetLink } from '../lib/magnetHelper.js'; -import { BadTokenError, chunkArray, sameFilename } from './mochHelper.js'; +import { BadTokenError, chunkArray, sameFilename, streamFilename } from './mochHelper.js'; const KEY = 'premiumize'; @@ -28,12 +28,9 @@ async function _getCachedStreams(PM, apiKey, streams) { }) .then(available => streams .reduce((mochStreams, stream, index) => { - const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); - const fileName = streamTitleParts[streamTitleParts.length - 1]; - const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; - const encodedFileName = encodeURIComponent(fileName); + const filename = streamFilename(stream); mochStreams[`${stream.infoHash}@${stream.fileIdx}`] = { - url: `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`, + url: `${apiKey}/${stream.infoHash}/${filename}/${stream.fileIdx}`, cached: available?.response[index] }; return mochStreams; @@ -126,8 +123,8 @@ async function _getCachedLink(PM, infoHash, encodedFileName, fileIndex, ip, isBr const targetFileName = decodeURIComponent(encodedFileName); 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[0]; + && videos.find(video => sameFilename(video.path, targetFileName)) + || 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 58eb778..3a00575 100644 --- a/addon/moch/putio.js +++ b/addon/moch/putio.js @@ -5,7 +5,7 @@ import StaticResponse from './static.js'; import { getMagnetLink } from '../lib/magnetHelper.js'; import { Type } from "../lib/types.js"; import { decode } from "magnet-uri"; -import { sameFilename } from "./mochHelper.js"; +import { sameFilename, streamFilename } from "./mochHelper.js"; const PutioAPI = PutioClient.default; const KEY = 'putio'; @@ -13,12 +13,9 @@ const KEY = 'putio'; export async function getCachedStreams(streams, apiKey) { return streams .reduce((mochStreams, stream) => { - const streamTitleParts = stream.title.replace(/\n👤.*/s, '').split('\n'); - const fileName = streamTitleParts[streamTitleParts.length - 1]; - const fileIndex = streamTitleParts.length === 2 ? stream.fileIdx : null; - const encodedFileName = encodeURIComponent(fileName); + const filename = streamFilename(stream); mochStreams[`${stream.infoHash}@${stream.fileIdx}`] = { - url: `${apiKey}/${stream.infoHash}/${encodedFileName}/${fileIndex}`, + url: `${apiKey}/${stream.infoHash}/${filename}/${stream.fileIdx}`, cached: false }; return mochStreams; @@ -172,8 +169,8 @@ async function _getTargetFile(Putio, torrent, encodedFileName, fileIndex) { // 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[0]; + && videos.find(video => sameFilename(targetFileName, video.name)) + || !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), []))