fix: do not add the same magnet twice to real debrid

This commit is contained in:
lilezek 2024-04-30 21:11:32 +02:00
parent d3f891da16
commit 27bf3c92ec
2 changed files with 30 additions and 4 deletions

View File

@ -69,10 +69,22 @@ export class Downloader {
});
} else {
try {
const torrent = await RealDebridClient.addMagnet(repack.magnet);
if (torrent && torrent.id) {
await RealDebridClient.selectAllFiles(torrent.id);
const { links } = await RealDebridClient.getInfo(torrent.id);
// Lets try first to find the torrent on RealDebrid
const torrents = await RealDebridClient.getAllTorrents();
const hash = RealDebridClient.extractSHA1FromMagnet(repack.magnet);
let torrent = torrents.find((t) => t.hash === hash);
if (!torrent) {
// Torrent is missing, lets add it
const magnet = await RealDebridClient.addMagnet(repack.magnet);
if (magnet && magnet.id) {
await RealDebridClient.selectAllFiles(magnet.id);
torrent = await RealDebridClient.getInfo(magnet.id);
}
}
if (torrent) {
const { links } = torrent;
const { download } = await RealDebridClient.unrestrictLink(links[0]);
this.lastHttpDownloader = new HTTPDownloader();
this.lastHttpDownloader.download(

View File

@ -52,9 +52,23 @@ export class RealDebridClient {
return response.json() as Promise<RealDebridUnrestrictLink>;
}
static async getAllTorrents() {
const response = await fetch(`${base}/torrents`, {
headers: {
Authorization: `Bearer ${await this.getApiToken()}`,
},
});
return response.json() as Promise<RealDebridTorrentInfo[]>;
}
static getApiToken() {
return userPreferencesRepository
.findOne({ where: { id: 1 } })
.then((userPreferences) => userPreferences!.realDebridApiToken);
}
static extractSHA1FromMagnet(magnet: string) {
return magnet.match(/btih:([0-9a-fA-F]*)/)?.[1].toLowerCase();
}
}