use custom fn to detect same filename in debrids

This commit is contained in:
TheBeastLT
2023-12-16 19:04:53 +02:00
parent 2c4da0679f
commit afa1458144
5 changed files with 13 additions and 11 deletions

View File

@@ -3,7 +3,7 @@ import { Type } from '../lib/types.js';
import { isVideo, isArchive } from '../lib/extension.js'; import { isVideo, isArchive } from '../lib/extension.js';
import StaticResponse from './static.js'; import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js'; import { getMagnetLink } from '../lib/magnetHelper.js';
import { BadTokenError, AccessDeniedError } from './mochHelper.js'; import { BadTokenError, AccessDeniedError, sameFilename } from './mochHelper.js';
const KEY = 'alldebrid'; const KEY = 'alldebrid';
const AGENT = 'torrentio'; const AGENT = 'torrentio';
@@ -144,7 +144,7 @@ async function _unrestrictLink(AD, torrent, encodedFileName, fileIndex) {
const targetFileName = decodeURIComponent(encodedFileName); const targetFileName = decodeURIComponent(encodedFileName);
const videos = torrent.links.filter(link => isVideo(link.filename)); const videos = torrent.links.filter(link => isVideo(link.filename));
const targetVideo = Number.isInteger(fileIndex) const targetVideo = Number.isInteger(fileIndex)
? videos.find(video => targetFileName.includes(video.filename)) ? videos.find(video => sameFilename(targetFileName, video.filename))
: videos.sort((a, b) => b.size - a.size)[0]; : videos.sort((a, b) => b.size - a.size)[0];
if (!targetVideo && torrent.links.every(link => isArchive(link.filename))) { if (!targetVideo && torrent.links.every(link => isArchive(link.filename))) {

View File

@@ -48,7 +48,7 @@ export async function enrichMeta(itemMeta) {
return itemMeta return itemMeta
} }
function sameFilename(filename, expectedFilename) { export function sameFilename(filename, expectedFilename) {
const offset = filename.length - expectedFilename.length; const offset = filename.length - expectedFilename.length;
for (let i = 0; i < expectedFilename.length; i++) { for (let i = 0; i < expectedFilename.length; i++) {
if (filename[offset + i] !== expectedFilename[i] && expectedFilename[i] !== '<27>') { if (filename[offset + i] !== expectedFilename[i] && expectedFilename[i] !== '<27>') {

View File

@@ -4,7 +4,7 @@ import { Type } from '../lib/types.js';
import { isVideo } from '../lib/extension.js'; import { isVideo } from '../lib/extension.js';
import StaticResponse from './static.js'; import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js'; import { getMagnetLink } from '../lib/magnetHelper.js';
import { chunkArray, BadTokenError } from './mochHelper.js'; import { chunkArray, BadTokenError, sameFilename } from './mochHelper.js';
const KEY = 'offcloud'; const KEY = 'offcloud';
@@ -125,10 +125,11 @@ async function _createTorrent(OC, infoHash) {
} }
async function _unrestrictLink(OC, infoHash, torrent, cachedEntryInfo, fileIndex) { async function _unrestrictLink(OC, infoHash, torrent, cachedEntryInfo, fileIndex) {
const targetFileName = decodeURIComponent(cachedEntryInfo);
const files = await _getFileUrls(OC, torrent) const files = await _getFileUrls(OC, torrent)
const targetFile = Number.isInteger(fileIndex) const targetFile = Number.isInteger(fileIndex)
? files.find(file => file.includes(`/${torrent.requestId}/${fileIndex}/`)) ? files.find(file => sameFilename(targetFileName, file.split('/').pop()))
: files.find(file => isVideo(file)); : files.find(file => isVideo(file)) || files.pop();
if (!targetFile) { if (!targetFile) {
return Promise.reject(`No Offcloud links found for index ${fileIndex} in: ${JSON.stringify(torrent)}`); return Promise.reject(`No Offcloud links found for index ${fileIndex} in: ${JSON.stringify(torrent)}`);

View File

@@ -4,7 +4,7 @@ import { Type } from '../lib/types.js';
import { isVideo, isArchive } from '../lib/extension.js'; import { isVideo, isArchive } from '../lib/extension.js';
import StaticResponse from './static.js'; import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js'; import { getMagnetLink } from '../lib/magnetHelper.js';
import { BadTokenError, chunkArray } from './mochHelper.js'; import { BadTokenError, chunkArray, sameFilename } from './mochHelper.js';
const KEY = 'premiumize'; const KEY = 'premiumize';
@@ -126,7 +126,7 @@ async function _getCachedLink(PM, infoHash, encodedFileName, fileIndex, ip, isBr
const targetFileName = decodeURIComponent(encodedFileName); const targetFileName = decodeURIComponent(encodedFileName);
const videos = cachedTorrent.content.filter(file => isVideo(file.path)); const videos = cachedTorrent.content.filter(file => isVideo(file.path));
const targetVideo = Number.isInteger(fileIndex) const targetVideo = Number.isInteger(fileIndex)
? videos.find(video => video.path.includes(targetFileName)) ? videos.find(video => sameFilename(video.path, targetFileName))
: videos.sort((a, b) => b.size - a.size)[0]; : videos.sort((a, b) => b.size - a.size)[0];
if (!targetVideo && videos.every(video => isArchive(video.path))) { if (!targetVideo && videos.every(video => isArchive(video.path))) {
console.log(`Only Premiumize archive is available for [${infoHash}] ${fileIndex}`) console.log(`Only Premiumize archive is available for [${infoHash}] ${fileIndex}`)

View File

@@ -5,6 +5,7 @@ import StaticResponse from './static.js';
import { getMagnetLink } from '../lib/magnetHelper.js'; import { getMagnetLink } from '../lib/magnetHelper.js';
import { Type } from "../lib/types.js"; import { Type } from "../lib/types.js";
import { decode } from "magnet-uri"; import { decode } from "magnet-uri";
import { sameFilename } from "./mochHelper.js";
const PutioAPI = PutioClient.default; const PutioAPI = PutioClient.default;
const KEY = 'putio'; const KEY = 'putio';
@@ -172,14 +173,14 @@ async function _getTargetFile(Putio, torrent, encodedFileName, fileIndex) {
// when specific file index is defined search by filename // when specific file index is defined search by filename
// when it's not defined find all videos and take the largest one // when it's not defined find all videos and take the largest one
targetFile = Number.isInteger(fileIndex) targetFile = Number.isInteger(fileIndex)
? videos.find(video => targetFileName.includes(video.name)) ? videos.find(video => sameFilename(targetFileName, video.name))
: !folders.length && videos.sort((a, b) => b.size - a.size)[0]; : !folders.length && videos.toSorted((a, b) => b.size - a.size)[0];
files = !targetFile files = !targetFile
? await Promise.all(folders.map(folder => _getFiles(Putio, folder.id))) ? await Promise.all(folders.map(folder => _getFiles(Putio, folder.id)))
.then(results => results.reduce((a, b) => a.concat(b), [])) .then(results => results.reduce((a, b) => a.concat(b), []))
: []; : [];
} }
return targetFile ? targetFile : Promise.reject(`No target file found for Putio [${torrent.hash}] ${targetFileName}`); return targetFile || Promise.reject(`No target file found for Putio [${torrent.hash}] ${targetFileName}`);
} }
async function _getFiles(Putio, fileId) { async function _getFiles(Putio, fileId) {