feat: adding real debrid user auth

This commit is contained in:
Chubby Granny Chaser 2024-05-28 14:01:28 +01:00
parent 86816dc3c3
commit 183b85d66a
No known key found for this signature in database
24 changed files with 234 additions and 137 deletions

View file

@ -30,6 +30,7 @@ import "./user-preferences/auto-launch";
import "./autoupdater/check-for-updates";
import "./autoupdater/restart-and-install-update";
import "./autoupdater/continue-to-main-window";
import "./user-preferences/authenticate-real-debrid";
ipcMain.handle("ping", () => "pong");
ipcMain.handle("getVersion", () => app.getVersion());

View file

@ -0,0 +1,14 @@
import { RealDebridClient } from "@main/services/real-debrid";
import { registerEvent } from "../register-event";
const authenticateRealDebrid = async (
_event: Electron.IpcMainInvokeEvent,
apiToken: string
) => {
RealDebridClient.authorize(apiToken);
const user = await RealDebridClient.getUser();
return user;
};
registerEvent("authenticateRealDebrid", authenticateRealDebrid);

View file

@ -86,7 +86,7 @@ const loadState = async (userPreferences: UserPreferences | null) => {
import("./events");
if (userPreferences?.realDebridApiToken)
await RealDebridClient.authorize(userPreferences?.realDebridApiToken);
RealDebridClient.authorize(userPreferences?.realDebridApiToken);
const game = await gameRepository.findOne({
where: {

View file

@ -92,7 +92,7 @@ export class DownloadManager {
const status = await this.aria2.call("tellStatus", this.gid);
const downloadingMetadata = status.bittorrent && !status.bittorrent?.info;
const isDownloadingMetadata = status.bittorrent && !status.bittorrent?.info;
if (status.followedBy?.length) {
this.gid = status.followedBy[0];
@ -103,7 +103,7 @@ export class DownloadManager {
const progress =
Number(status.completedLength) / Number(status.totalLength);
if (!downloadingMetadata) {
if (!isDownloadingMetadata) {
const update: QueryDeepPartialEntity<Game> = {
bytesDownloaded: Number(status.completedLength),
fileSize: Number(status.totalLength),
@ -127,7 +127,7 @@ export class DownloadManager {
relations: { repack: true },
});
if (progress === 1 && game && !downloadingMetadata) {
if (progress === 1 && game && !isDownloadingMetadata) {
await this.publishNotification();
/*
Only cancel bittorrent downloads to stop seeding
@ -150,7 +150,7 @@ export class DownloadManager {
numSeeds: Number(status.numSeeders ?? 0),
downloadSpeed: Number(status.downloadSpeed),
timeRemaining: this.getETA(status),
downloadingMetadata: !!downloadingMetadata,
isDownloadingMetadata: !!isDownloadingMetadata,
game,
} as DownloadProgress;

View file

@ -1,10 +1,11 @@
import { Game } from "@main/entity";
import axios, { AxiosInstance } from "axios";
import type {
RealDebridAddMagnet,
RealDebridTorrentInfo,
RealDebridUnrestrictLink,
} from "./real-debrid.types";
import axios, { AxiosInstance } from "axios";
RealDebridUser,
} from "@types";
const base = "https://api.real-debrid.com/rest/1.0";
@ -29,6 +30,11 @@ export class RealDebridClient {
return response.data;
}
static async getUser() {
const response = await this.instance.get<RealDebridUser>(`/user`);
return response.data;
}
static async selectAllFiles(id: string) {
const searchParams = new URLSearchParams({ files: "all" });
@ -65,30 +71,29 @@ export class RealDebridClient {
const hash = RealDebridClient.extractSHA1FromMagnet(game!.repack.magnet);
let torrent = torrents.find((t) => t.hash === hash);
// User haven't downloaded this torrent yet
if (!torrent) {
const magnet = await RealDebridClient.addMagnet(game!.repack.magnet);
if (magnet && magnet.id) {
if (magnet) {
await RealDebridClient.selectAllFiles(magnet.id);
torrent = await RealDebridClient.getInfo(magnet.id);
const { links } = torrent;
const { download } = await RealDebridClient.unrestrictLink(links[0]);
if (!download) {
throw new Error("Torrent not cached on Real Debrid");
}
return download;
}
}
if (torrent) {
const { links } = torrent;
const { download } = await RealDebridClient.unrestrictLink(links[0]);
if (!download) {
throw new Error("Torrent not cached on Real Debrid");
}
return download;
}
throw new Error();
}
static async authorize(apiToken: string) {
static authorize(apiToken: string) {
this.instance = axios.create({
baseURL: base,
headers: {

View file

@ -1,51 +0,0 @@
export 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;
}
export interface RealDebridAddMagnet {
id: string;
// URL of the created ressource
uri: string;
}
export 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: string; // 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[];
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
}