use filename from behaviorhints in encoded redirect url

This commit is contained in:
TheBeastLT 2024-05-26 21:23:15 +03:00
parent 0105173c6a
commit 1782e03660
5 changed files with 20 additions and 31 deletions

View file

@ -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}`)

View file

@ -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)
}

View file

@ -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;

View file

@ -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;

View file

@ -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), []))