Skip to content

Commit

Permalink
Enhanced results handling for Debrid services, Issue #22 fix, Correct…
Browse files Browse the repository at this point in the history
…ly limit max results option (#25)

* Fixed All-Debrid for season files and corrected maximum results implementation

* Implement max results option in the search

* Issue #22 fix

* Fix for #22 fix

* Correctly limit max results option

* Fix for #22 fix

* Enhanced results handling for Debrid services

---------

Co-authored-by: Aran Leite <hyoretsu@gmail.com>
Co-authored-by: aymene <aymene@Macbook-Pro-de-Aymene.local>
  • Loading branch information
3 people authored Jan 25, 2024
1 parent c0afc83 commit a574322
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 163 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
pnpm-lock.yaml
.DS_Store
13 changes: 13 additions & 0 deletions src/helpers/getAvailabilityAD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export async function getAvailabilityAD(magnet, debridApi) {
const url = `https://api.alldebrid.com/v4/magnet/instant?agent=jackett&apikey=${debridApi}&magnets[]=${magnet}`;
const response = await fetch(url);
const json = await response.json();
if (json.status === "error") {
return "error";
}
const instant = json.data.magnets[0].instant;
if (instant === true) {
return true;
}
return false;
}
13 changes: 13 additions & 0 deletions src/helpers/getAvailabilityRD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export async function getAvailabilityRD(hash, debridApi) {
const url = `https://api.real-debrid.com/rest/1.0/torrents/instantAvailability/${hash}`
const headers = {
Authorization: `Bearer ${debridApi}`,
};
const response = await fetch(url, { method: "GET", headers });
const json = await response.json();
const length = json[hash].length;
if (length === 0) {
return false;
}
return true;
}
20 changes: 15 additions & 5 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"
></script>
<title>Stremio-Jackett by Aymene69</title>
<title>Stremio-Jackett</title>
</head>
<body>
<section class="vh-100">
Expand All @@ -50,7 +50,7 @@
height="100"
/>
<h2 class="fw-bold mb-2 text-uppercase">Stremio-Jackett</h2>
<p class="text-white-50 mb-5">1.0.0</p>
<p class="text-white-50 mb-5">1.1.3</p>
<h3 class="text-white mb-5">Configuration page</h3>
<p class="text-white-50 mb-5">
Please fill in the following fields to install Stremio-Jackett
Expand Down Expand Up @@ -112,6 +112,7 @@ <h3 class="text-white mb-5">Configuration page</h3>
class="form-control form-control-lg"
placeholder="Enter maximum results per search"
oninput="updateLink()"
onchange="onChangeMaxResults()"
id="maxResults"
/>
</div>
Expand Down Expand Up @@ -149,6 +150,16 @@ <h3 class="text-white mb-5">Configuration page</h3>
<script>
document.getElementById("realDebridApiDiv").style.display = "none";
document.getElementById("allDebridApiDiv").style.display = "none";

function onChangeMaxResults() {
let addonType = document.getElementById("serviceProvider").value;
let maxResultsInput = document.getElementById("maxResults");

if (addonType !== "torrent" && Number(maxResultsInput.value) > 15) {
maxResultsInput.value = String(15);
}
}

function updateLink() {
let serviceProvider = document.getElementById("serviceProvider").value;
let realDebridApi = document.getElementById("realDebridApi").value;
Expand All @@ -166,7 +177,6 @@ <h3 class="text-white mb-5">Configuration page</h3>
if (serviceProvider === "torrent") {
document.getElementById("realDebridApiDiv").style.display = "none";
document.getElementById("allDebridApiDiv").style.display = "none";
document.getElementById("maxResultDiv").style.display = "block";
let dataJson = {
streamService: serviceProvider,
jackettUrl: jackettUrl,
Expand All @@ -181,12 +191,12 @@ <h3 class="text-white mb-5">Configuration page</h3>
} else if (serviceProvider === "realdebrid") {
document.getElementById("realDebridApiDiv").style.display = "block";
document.getElementById("allDebridApiDiv").style.display = "none";
document.getElementById("maxResultDiv").style.display = "none";
let dataJson = {
streamService: serviceProvider,
jackettUrl: jackettUrl,
jackettApiKey: jackettApi,
debridApiKey: realDebridApi,
maxResults: maxResults,
};
dynamicLink.href = `stremio://${
window.location.host
Expand All @@ -196,12 +206,12 @@ <h3 class="text-white mb-5">Configuration page</h3>
} else if (serviceProvider === "alldebrid") {
document.getElementById("realDebridApiDiv").style.display = "none";
document.getElementById("allDebridApiDiv").style.display = "block";
document.getElementById("maxResultDiv").style.display = "none";
let dataJson = {
streamService: serviceProvider,
jackettUrl: jackettUrl,
jackettApiKey: jackettApi,
debridApiKey: allDebridApi,
maxResults: maxResults,
};
dynamicLink.href = `stremio://${
window.location.host
Expand Down
163 changes: 9 additions & 154 deletions src/jackett/index.js
Original file line number Diff line number Diff line change
@@ -1,159 +1,14 @@
import { getMovieRDLink } from "../helpers/getMovieRDLink.js";
import { getMovieADLink } from "../helpers/getMovieADLink.js";
import { selectBiggestFileSeasonTorrent } from "../helpers/selectBiggestFileSeasonTorrent.js";
import { toHumanReadable } from "../helpers/toHumanReadable.js";
import getTorrentInfo from "./utils/getTorrentInfo.js";
import processXML from "./utils/processXML.js";
import jackettSearch from "./utils/jackettSearch.js";

async function getItemsFromUrl(url) {
const res = await fetch(url);

const items = await processXML(await res.text());

return items;
}

export default async function jackettSearch(debridApi, jackettHost, jackettApiKey, addonType, maxResults, searchQuery) {
try {
const { episode, name, season, type } = searchQuery;
const isSeries = type === "series";
const torrentAddon = addonType === "torrent";

console.log("Searching on Jackett...");
console.log(`Will return ${!torrentAddon ? "Debrid link" : "Torrents"}.`);

let searchUrl = `${jackettHost}/api/v2.0/indexers/all/results/torznab/api?apikey=${jackettApiKey}&cat=${
isSeries ? 5000 : 2000
}&q=${encodeURIComponent(name)}${isSeries ? `+S${season}E${episode}` : ""}`;

const results = [];

let items = await getItemsFromUrl(searchUrl);
for (const [index, item] of items.entries()) {
if (torrentAddon && index >= 5) {
break;
}

console.log("Getting torrent info...");
const torrentInfo = await getTorrentInfo(item.link);
console.log(`Torrent info: ${item.title}`);

if (!torrentAddon) {
if (addonType === "realdebrid") {
console.log("Getting RD link...");

const downloadLink = await getMovieRDLink(torrentInfo.magnetLink, debridApi);
results.push({
name: "Jackett Debrid",
title: `${item.title}\r\n📁${toHumanReadable(item.size)}`,
url: downloadLink,
});

break;
} else if (addonType === "alldebrid") {
console.log("Getting AD link...");

const downloadLink = await getMovieADLink(torrentInfo.magnetLink, debridApi);
if (downloadLink === "blocked") {
console.log("Error: AllDebrid blocked for this IP. Please check your email.");
return [{ name: "AllDebrid blocked", title: "Please check your email", url: "#" }];
}
results.push({
name: "Jackett Debrid",
title: `${item.title}\r\n📁${toHumanReadable(item.size)}`,
url: downloadLink,
});

break;
}
}

torrentInfo.seeders = item.seeders;
torrentInfo.title = `${item.title}\r\n👤${item.seeders} 📁${toHumanReadable(item.size)}`;
if (!isSeries) {
delete torrentInfo.fileIdx;
}

results.push(torrentInfo);
console.log(`Added torrent to results: ${item.title}`);
}

// Try again without episode
if (isSeries && results.length === 0) {
if (torrentAddon) {
console.log("No results found with season/episode. Trying without...");
console.log("Searching on Jackett...");
}

searchUrl = `${jackettHost}/api/v2.0/indexers/all/results/torznab/api?apikey=${jackettApiKey}&cat=5000&q=${encodeURIComponent(
searchQuery.name,
)}+S${searchQuery.season}`;

items = await getItemsFromUrl(searchUrl);
for (const item of items) {
const torrentInfo = await getTorrentInfo(item.link);

if (!torrentAddon) {
if (addonType === "realdebrid") {
const url = await getMovieRDLink(
torrentInfo.magnetLink,
debridApi,
`S${searchQuery.season}E${searchQuery.episode}`,
);

results.push({
name: "Jackett Debrid",
title: `${item.title}\r\n📁${toHumanReadable(item.size)}`,
url,
});

break;

} else if (addonType === "alldebrid") {
const url = await getMovieADLink(
torrentInfo.magnetLink,
debridApi,
`S${searchQuery.season}E${searchQuery.episode}`,
);

results.push({
name: "Jackett Debrid",
title: `${item.title}\r\n📁${toHumanReadable(item.size)}`,
url,
});

break;
}
}

console.log("Getting torrent info...");
console.log(`Torrent info: ${item.title}`);

torrentInfo.seeders = item.seeders;
torrentInfo.title = `${item.title}\r\n👤${item.seeders} 📁${toHumanReadable(item.size)}`;

console.log("Determining episode file...");
torrentInfo.fileIdx = parseInt(
selectBiggestFileSeasonTorrent(torrentInfo.files, `S${searchQuery.season}E${searchQuery.episode}`),
10,
);
console.log("Episode file determined.");

results.push(torrentInfo);
console.log(`Added torrent to results: ${item.title}`);
}
}

export default async function fetchResults(debridApi, jackettHost, jackettApiKey, addonType, maxResults, searchQuery) {
let results = await jackettSearch(debridApi, jackettHost, jackettApiKey, addonType, maxResults, searchQuery);
if (results.length === 0) {
results = await jackettSearch(debridApi, jackettHost, jackettApiKey, addonType, 1, searchQuery);
if (results.length === 0) {
console.log("No results found.");

results.push({ name: "Jackett", title: "No results found", url: "#" });
results = [{ name: "Jackett", title: "No results found", url: "#" }]
}

return results;
} catch (e) {
console.error(e);

return [{ name: "Jackett", title: "No results found", url: "#" }];
}
}

return results;
}
Loading

0 comments on commit a574322

Please sign in to comment.