Spaces:
Paused
Paused
File size: 6,062 Bytes
762fa11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
import { searchTorrents, } from "../torrent/search.js";
import { getTorrentInfo } from "../torrent/webtorrent.js";
import { getReadableSize, isSubtitleFile, isVideoFile } from "../utils/file.js";
import { getTitles } from "../utils/imdb.js";
import { guessLanguage } from "../utils/language.js";
import { guessQuality } from "../utils/quality.js";
import { isFileNameMatch, isTorrentNameMatch } from "../utils/shows.js";
export const streamHandler = async ({ type, id, config, req }) => {
let torrents = [];
const categories = [];
if (type === "movie")
categories.push("movie");
if (type === "series")
categories.push("show");
const sources = [];
if (config.enableJackett === "on")
sources.push("jackett");
if (config.enableNcore === "on")
sources.push("ncore");
if (config.enableInsane === "on")
sources.push("insane");
if (config.enableItorrent === "on")
sources.push("itorrent");
if (config.enableYts === "on")
sources.push("yts");
if (config.enableEztv === "on")
sources.push("eztv");
const [imdbId, season, episode] = id.split(":");
const queries = [imdbId];
if (config.searchByTitle === "on")
queries.push(...(await getTitles(imdbId)));
torrents = (await Promise.all(queries.map((query) => searchTorrents(query, {
categories,
sources,
jackett: {
url: config.jackettUrl,
apiKey: config.jackettKey,
},
ncore: {
user: config.nCoreUser,
password: config.nCorePassword,
},
insane: {
user: config.insaneUser,
password: config.insanePassword,
},
})))).flat();
torrents = dedupeTorrents(torrents);
torrents = torrents.filter((torrent) => {
if (!torrent.seeds)
return false;
if (torrent.category?.includes("DVD"))
return false;
if (!isAllowedFormat(config, torrent.name))
return false;
if (!isAllowedQuality(config, guessQuality(torrent.name).quality))
return false;
if (season &&
episode &&
!isTorrentNameMatch(torrent.name, Number(season), Number(episode)))
return false;
return true;
});
let streams = (await Promise.all(torrents.map((torrent) => getStreamsFromTorrent(req, torrent, season, episode)))).flat();
streams = streams.filter((stream) => {
if (!isAllowedFormat(config, stream.fileName))
return false;
if (!isAllowedQuality(config, stream.quality))
return false;
return true;
});
streams.sort((a, b) => b.score - a.score);
return { streams: streams.map((stream) => stream.stream) };
};
const dedupeTorrents = (torrents) => {
const map = new Map(torrents.map((torrent) => [`${torrent.tracker}:${torrent.name}`, torrent]));
return [...map.values()];
};
export const getStreamsFromTorrent = async (req, torrent, season, episode) => {
const uri = torrent.torrent || torrent.magnet;
if (!uri)
return [];
const torrentInfo = await getTorrentInfo(uri);
if (!torrentInfo)
return [];
let videos = torrentInfo.files.filter((file) => isVideoFile(file.name));
if (season && episode) {
videos = videos.filter((file) => isFileNameMatch(file.name, Number(season), Number(episode)));
}
const videosSize = videos.reduce((acc, file) => acc + file.size, 0);
videos = videos.filter((file) => file.size > videosSize / (videos.length + 1));
const subs = torrentInfo.files.filter((file) => isSubtitleFile(file.name));
const torrentQuality = guessQuality(torrent.name);
const language = guessLanguage(torrent.name, torrent.category);
// @ts-ignore
return videos.map((file) => {
const fileQuality = guessQuality(file.name);
const { quality, score } = fileQuality.score > torrentQuality.score ? fileQuality : torrentQuality;
const description = [
...(season && episode ? [torrent.name, file.name] : [torrent.name]),
[
`💾 ${getReadableSize(file.size)}`,
`⬆️ ${torrent.seeds}`,
`⬇️ ${torrent.peers}`,
].join(" "),
[`🔊 ${language}`, `⚙️ ${torrent.tracker}`].join(" "),
].join("\n");
const streamEndpoint = `${req.protocol}://${req.get("host")}/stream`;
const url = [
streamEndpoint,
encodeURIComponent(uri),
encodeURIComponent(file.path),
].join("/");
const subtitles = subs.map((sub, index) => ({
id: index.toString(),
url: [
streamEndpoint,
encodeURIComponent(uri),
encodeURIComponent(sub.path),
].join("/"),
lang: sub.name,
}));
return {
stream: {
name: quality,
description,
url,
subtitles,
behaviorHints: {
bingeGroup: torrent.name,
},
},
torrentName: torrent.name,
fileName: file.name,
quality,
score,
};
});
};
const isAllowedQuality = (config, quality) => {
if (config?.disable4k === "on" && quality.includes("4K"))
return false;
if (config?.disableCam === "on" && quality.includes("CAM"))
return false;
if (config?.disableHdr === "on" &&
(quality.includes("HDR") || quality.includes("Dolby Vision")))
return false;
if (config?.disable3d === "on" && quality.includes("3D"))
return false;
return true;
};
const isAllowedFormat = (config, name) => {
if (config?.disableHevc === "on") {
const str = name.replace(/\W/g, "").toLowerCase();
if (str.includes("x265") || str.includes("h265") || str.includes("hevc"))
return false;
}
return true;
};
//# sourceMappingURL=streams.js.map |