diff --git a/src/main/services/donwloaders/real-debrid.d.ts b/src/main/services/donwloaders/real-debrid.d.ts new file mode 100644 index 00000000..4ee5228a --- /dev/null +++ b/src/main/services/donwloaders/real-debrid.d.ts @@ -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; + info(id: string): Promise; + } + + export default class { + constructor(token: string); + torrents: Torrent; + } +} \ No newline at end of file diff --git a/src/main/services/donwloaders/real-debrid.ts b/src/main/services/donwloaders/real-debrid.ts new file mode 100644 index 00000000..0ab290d0 --- /dev/null +++ b/src/main/services/donwloaders/real-debrid.ts @@ -0,0 +1,55 @@ +/// +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; + } + + static async getInfo(id: string) { + const response = await fetch(`${base}/torrents/info/${id}`, { + headers: { + "Authorization": `Bearer ${await this.getApiToken()}` + } + }); + + return response.json() as Promise; + } + + 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; + } + + static getApiToken() { + return userPreferencesRepository.findOne({ where: { id: 1 } }).then(userPreferences => userPreferences.realDebridApiToken); + } +} \ No newline at end of file