Skip to content

Commit

Permalink
Merge pull request #85 from aymene69/dev
Browse files Browse the repository at this point in the history
Fixed torrent service
  • Loading branch information
aymene69 authored Feb 6, 2024
2 parents 2dcfc97 + 9650534 commit e70ccc0
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "stremio-addon-jackett",
"version": "2.1.0",
"version": "2.1.2",
"description": "Stremio Jackett Addon",
"scripts": {
"build": "node esbuild.js",
Expand Down
19 changes: 13 additions & 6 deletions src/jackett/utils/jackettSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { toHumanReadable } from "../../helpers/toHumanReadable";
import { excludeItem } from "./excludeItems";
import processXML from "./processXML";
import { threadedAvailability } from "./threadedAvailability";
import { threadedTorrent } from "./threadedTorrent";

async function getItemsFromUrl(url) {
const res = await fetch(url);
Expand Down Expand Up @@ -80,8 +81,11 @@ export default async function jackettSearch(
items.items = sortBySize(items.items, sorting.ascOrDesc);
}
const results = [];
if (!torrentAddon && items.cached === false)
if (!torrentAddon && items.cached === false) {
items.items = await threadedAvailability(items.items, debridApi, addonType, maxResults, maxThread);
} else {
items.items = await threadedTorrent(items.items, maxResults, maxThread);
}
for (let index = 0; index < maxResults; index++) {
const item = items.items[index];
if (!item) {
Expand Down Expand Up @@ -177,7 +181,7 @@ export default async function jackettSearch(
});
}
} else {
torrentInfo.title = `${item.title.slice(0, 98)}...\r\n${detectLanguageEmoji(torrentInfo.title)} ${detectQuality(torrentInfo.title)}\r\n📁${toHumanReadable(item.size)}`;
torrentInfo.title = `${item.title}\r\n${detectLanguageEmoji(item.title)} ${detectQuality(item.title)}\r\n📁${toHumanReadable(item.size)}`;
if (!isSeries) {
torrentInfo.fileIdx = undefined;
}
Expand Down Expand Up @@ -206,8 +210,11 @@ export default async function jackettSearch(
items.items = await getItemsFromUrl(searchUrl);
}
}
if (!torrentAddon && items.cached === false)
if (!torrentAddon && items.cached === false) {
items.items = await threadedAvailability(items.items, debridApi, addonType, maxResults, maxThread);
} else {
items.items = await threadedTorrent(items.items, maxResults, maxThread);
}
for (let index = 0; index < maxResults; index++) {
const item = items.items[index];
if (!item) {
Expand All @@ -217,8 +224,7 @@ export default async function jackettSearch(
if (items.cached) {
torrentInfo = JSON.parse(item.torrentInfo);
}
console.log(`Torrent info: ${item.title}`);
torrentInfo = item.torrentInfo;
console.log(`Torrent infoaa: ${item.title}`);
if (!torrentAddon) {
if (addonType === "realdebrid") {
if (maxResults === "1") {
Expand Down Expand Up @@ -325,7 +331,7 @@ export default async function jackettSearch(
} else {
console.log("Getting torrent info...");
console.log(`Torrent info: ${item.title}`);

torrentInfo = item.torrentInfo;
torrentInfo.title = `${item.title.slice(0, 98)}...\r\n${detectLanguageEmoji(item.title)} ${detectQuality(item.title)}\r\n📁${toHumanReadable(item.size)}`;

console.log("Determining episode file...");
Expand All @@ -336,6 +342,7 @@ export default async function jackettSearch(
),
10,
);
console.log(torrentInfo.fileIdx);
console.log("Episode file determined.");

results.push(torrentInfo);
Expand Down
105 changes: 105 additions & 0 deletions src/jackett/utils/threadedTorrent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import PQueue from "p-queue";
import getTorrentInfo from "./getTorrentInfo.js";

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

export async function threadedTorrent(itemList, maxResults, maxThread) {
if (maxThread > 1) {
const queue = new PQueue({ concurrency: parseInt(maxThread) }); // Limite le nombre de workers à 5
if (itemList.length !== 0) {
const items = itemList.slice(0, parseInt(maxResults));
const filteredItems = await Promise.all(
items.map(async elem => {
return await queue.add(async () => {
let torrentInfo;
console.log("Fetching torrent info for " + elem.title);
torrentInfo = await getTorrentInfo(elem.link);
if (torrentInfo === undefined) {
console.log("Error fetching torrent info for " + elem.title);
console.log("Retrying...");
let tries = 0;
while (true) {
if (tries > 5) {
console.log("Failed to fetch torrent info for " + elem.title);
break;
}
await wait(5000);
console.log("Retrying...");
try {
torrentInfo = await getTorrentInfo(elem.link);
} catch (error) {
torrentInfo = undefined;
}
if (torrentInfo !== undefined) {
console.log("Success");
break;
}
tries += 1;
}
}
if (torrentInfo !== undefined) {
const { infoHash, magnetLink } = torrentInfo;
elem.torrentInfo = torrentInfo;
elem.magnetLink = magnetLink;
return { ...elem, infoHash }; // Ajoute l'availability à l'élément
}
return undefined;
});
}),
);
// Filtre les éléments en fonction de l'availability
const finalFilteredItems = filteredItems.filter(elem => elem !== undefined);

return finalFilteredItems;
}

return [];
} else {
if (itemList.length !== 0) {
const items = itemList.slice(0, parseInt(maxResults));
const filteredItems = await Promise.all(
items.map(async elem => {
let torrentInfo;
console.log("Fetching torrent info for " + elem.title);
torrentInfo = await getTorrentInfo(elem.link);
if (torrentInfo === undefined) {
console.log("Error fetching torrent info for " + elem.title);
console.log("Retrying...");
let tries = 0;
while (true) {
if (tries > 5) {
console.log("Failed to fetch torrent info for " + elem.title);
break;
}
await wait(5000);
console.log("Retrying...");
try {
torrentInfo = await getTorrentInfo(elem.link);
} catch (error) {
torrentInfo = undefined;
}
if (torrentInfo !== undefined) {
console.log("Success");
break;
}
tries += 1;
}
}
if (torrentInfo !== undefined) {
const { infoHash, magnetLink } = torrentInfo;
elem.torrentInfo = torrentInfo;
elem.magnetLink = magnetLink;
return { ...elem, infoHash }; // Ajoute l'availability à l'élément
}
return undefined;
}),
);
// Filtre les éléments en fonction de l'availability
const finalFilteredItems = filteredItems.filter(elem => elem !== undefined);

return finalFilteredItems;
}

return [];
}
}

0 comments on commit e70ccc0

Please sign in to comment.