feat: added support to add magnets and download from real debrid

This commit is contained in:
lilezek 2024-04-29 20:54:27 +02:00
parent 76a64fca4a
commit 6bb22655e8
2 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,65 @@
interface RealDebridUnrestrictLink {
id: string;
filename: string;
mimeType: string;
filesize: number;
link: string;
host: string;
host_icon: string;
chunks: number;
crc: number;
download: string;
streamable: number;
}
interface RealDebridAddMagnet {
"id": string,
// URL of the created ressource
"uri": string
}
interface RealDebridTorrentInfo {
"id": string,
"filename": string,
"original_filename": string, // Original name of the torrent
"hash": string, // SHA1 Hash of the torrent
"bytes": number, // Size of selected files only
"original_bytes": number, // Total size of the torrent
"host": string, // Host main domain
"split": number, // Split size of links
"progress": number, // Possible values: 0 to 100
"status": "downloaded", // Current status of the torrent: magnet_error, magnet_conversion, waiting_files_selection, queued, downloading, downloaded, error, virus, compressing, uploading, dead
"added": string, // jsonDate
"files": [
{
"id": number,
"path": string, // Path to the file inside the torrent, starting with "/"
"bytes": number,
"selected": number // 0 or 1
},
{
"id": number,
"path": string, // Path to the file inside the torrent, starting with "/"
"bytes": number,
"selected": number // 0 or 1
}
],
"links": [
"string" // Host URL
],
"ended": string, // !! Only present when finished, jsonDate
"speed": number, // !! Only present in "downloading", "compressing", "uploading" status
"seeders": number // !! Only present in "downloading", "magnet_conversion" status
}
declare module 'real-debrid-api' {
interface Torrent {
addMagnet(magnet: string): Promise<RealDebridAddMagnet>;
info(id: string): Promise<RealDebridTorrentInfo[]>;
}
export default class {
constructor(token: string);
torrents: Torrent;
}
}

View File

@ -0,0 +1,55 @@
/// <reference path="./real-debrid.d.ts" />
import { userPreferencesRepository } from "@main/repository";
import fetch from "node-fetch";
const base = "https://api.real-debrid.com/rest/1.0";
export class RealDebridClient {
static async addMagnet(magnet: string) {
const response = await fetch(`${base}/torrents/addMagnet`, {
method: "POST",
headers: {
"Authorization": `Bearer ${await this.getApiToken()}`,
},
body: `magnet=${encodeURIComponent(magnet)}`
});
return response.json() as Promise<RealDebridAddMagnet>;
}
static async getInfo(id: string) {
const response = await fetch(`${base}/torrents/info/${id}`, {
headers: {
"Authorization": `Bearer ${await this.getApiToken()}`
}
});
return response.json() as Promise<RealDebridTorrentInfo>;
}
static async selectAllFiles(id: string) {
const response = await fetch(`${base}/torrents/selectFiles/${id}`, {
method: "POST",
headers: {
"Authorization": `Bearer ${await this.getApiToken()}`,
},
body: "files=all"
});
}
static async unrestrictLink(link: string) {
const response = await fetch(`${base}/unrestrict/link`, {
method: "POST",
headers: {
"Authorization": `Bearer ${await this.getApiToken()}`,
},
body: `link=${link}`
});
return response.json() as Promise<RealDebridUnrestrictLink>;
}
static getApiToken() {
return userPreferencesRepository.findOne({ where: { id: 1 } }).then(userPreferences => userPreferences.realDebridApiToken);
}
}