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 { } else {
try { try {
const torrent = await RealDebridClient.addMagnet(repack.magnet); // Lets try first to find the torrent on RealDebrid
if (torrent && torrent.id) { const torrents = await RealDebridClient.getAllTorrents();
await RealDebridClient.selectAllFiles(torrent.id); const hash = RealDebridClient.extractSHA1FromMagnet(repack.magnet);
const { links } = await RealDebridClient.getInfo(torrent.id); 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]); const { download } = await RealDebridClient.unrestrictLink(links[0]);
this.lastHttpDownloader = new HTTPDownloader(); this.lastHttpDownloader = new HTTPDownloader();
this.lastHttpDownloader.download( this.lastHttpDownloader.download(

View File

@ -52,9 +52,23 @@ export class RealDebridClient {
return response.json() as Promise<RealDebridUnrestrictLink>; 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() { static getApiToken() {
return userPreferencesRepository return userPreferencesRepository
.findOne({ where: { id: 1 } }) .findOne({ where: { id: 1 } })
.then((userPreferences) => userPreferences!.realDebridApiToken); .then((userPreferences) => userPreferences!.realDebridApiToken);
} }
static extractSHA1FromMagnet(magnet: string) {
return magnet.match(/btih:([0-9a-fA-F]*)/)?.[1].toLowerCase();
}
} }