feat: fixing real debrid download

This commit is contained in:
Chubby Granny Chaser
2024-08-15 20:46:21 +01:00
parent fffea84ef7
commit 68b361e605
28 changed files with 241 additions and 185 deletions

View File

@@ -102,8 +102,6 @@ export class DownloadManager {
const token = await GofileApi.authorize();
const downloadLink = await GofileApi.getDownloadLink(id!);
console.log(downloadLink, token, "<<<");
GenericHTTPDownloader.startDownload(game, downloadLink, {
Cookie: `accountToken=${token}`,
});

View File

@@ -2,7 +2,7 @@ import { Game } from "@main/entity";
import { gameRepository } from "@main/repository";
import { calculateETA } from "./helpers";
import { DownloadProgress } from "@types";
import { HTTPDownload } from "./http-download";
import { HttpDownload } from "./http-download";
export class GenericHTTPDownloader {
private static downloads = new Map<number, string>();
@@ -11,7 +11,7 @@ export class GenericHTTPDownloader {
public static async getStatus() {
if (this.downloadingGame) {
const gid = this.downloads.get(this.downloadingGame.id)!;
const status = await HTTPDownload.getStatus(gid);
const status = HttpDownload.getStatus(gid);
if (status) {
const progress =
@@ -60,7 +60,7 @@ export class GenericHTTPDownloader {
const gid = this.downloads.get(this.downloadingGame!.id!);
if (gid) {
await HTTPDownload.pauseDownload(gid);
await HttpDownload.pauseDownload(gid);
}
this.downloadingGame = null;
@@ -76,26 +76,23 @@ export class GenericHTTPDownloader {
if (this.downloads.has(game.id)) {
await this.resumeDownload(game.id!);
return;
}
if (downloadUrl) {
const gid = await HTTPDownload.startDownload(
game.downloadPath!,
downloadUrl,
headers
);
const gid = await HttpDownload.startDownload(
game.downloadPath!,
downloadUrl,
headers
);
this.downloads.set(game.id!, gid);
}
this.downloads.set(game.id!, gid);
}
static async cancelDownload(gameId: number) {
const gid = this.downloads.get(gameId);
if (gid) {
await HTTPDownload.cancelDownload(gid);
await HttpDownload.cancelDownload(gid);
this.downloads.delete(gameId);
}
}
@@ -104,7 +101,7 @@ export class GenericHTTPDownloader {
const gid = this.downloads.get(gameId);
if (gid) {
await HTTPDownload.resumeDownload(gid);
await HttpDownload.resumeDownload(gid);
}
}
}

View File

@@ -2,7 +2,7 @@ import { DownloadItem } from "electron";
import { WindowManager } from "../window-manager";
import path from "node:path";
export class HTTPDownload {
export class HttpDownload {
private static id = 0;
private static downloads: Record<string, DownloadItem> = {};

View File

@@ -3,7 +3,7 @@ import { RealDebridClient } from "../real-debrid";
import { gameRepository } from "@main/repository";
import { calculateETA } from "./helpers";
import { DownloadProgress } from "@types";
import { HTTPDownload } from "./http-download";
import { HttpDownload } from "./http-download";
export class RealDebridDownloader {
private static downloads = new Map<number, string>();
@@ -13,19 +13,23 @@ export class RealDebridDownloader {
private static async getRealDebridDownloadUrl() {
if (this.realDebridTorrentId) {
const torrentInfo = await RealDebridClient.getTorrentInfo(
let torrentInfo = await RealDebridClient.getTorrentInfo(
this.realDebridTorrentId
);
const { status, links } = torrentInfo;
if (status === "waiting_files_selection") {
if (torrentInfo.status === "waiting_files_selection") {
await RealDebridClient.selectAllFiles(this.realDebridTorrentId);
return null;
torrentInfo = await RealDebridClient.getTorrentInfo(
this.realDebridTorrentId
);
}
const { links, status } = torrentInfo;
if (status === "downloaded") {
const [link] = links;
const { download } = await RealDebridClient.unrestrictLink(link);
return decodeURIComponent(download);
}
@@ -38,8 +42,6 @@ export class RealDebridDownloader {
this.downloadingGame?.uri
);
console.log("download>>", download);
return decodeURIComponent(download);
}
@@ -49,7 +51,7 @@ export class RealDebridDownloader {
public static async getStatus() {
if (this.downloadingGame) {
const gid = this.downloads.get(this.downloadingGame.id)!;
const status = await HTTPDownload.getStatus(gid);
const status = HttpDownload.getStatus(gid);
if (status) {
const progress =
@@ -91,33 +93,6 @@ export class RealDebridDownloader {
}
}
if (this.realDebridTorrentId && this.downloadingGame) {
const torrentInfo = await RealDebridClient.getTorrentInfo(
this.realDebridTorrentId
);
const { status } = torrentInfo;
if (status === "downloaded") {
this.startDownload(this.downloadingGame);
}
const progress = torrentInfo.progress / 100;
const totalDownloaded = progress * torrentInfo.bytes;
return {
numPeers: 0,
numSeeds: torrentInfo.seeders,
downloadSpeed: torrentInfo.speed,
timeRemaining: calculateETA(
torrentInfo.bytes,
totalDownloaded,
torrentInfo.speed
),
isDownloadingMetadata: status === "magnet_conversion",
} as DownloadProgress;
}
return null;
}
@@ -125,7 +100,7 @@ export class RealDebridDownloader {
if (this.downloadingGame) {
const gid = this.downloads.get(this.downloadingGame.id);
if (gid) {
await HTTPDownload.pauseDownload(gid);
await HttpDownload.pauseDownload(gid);
}
}
@@ -146,18 +121,19 @@ export class RealDebridDownloader {
);
}
this.downloadingGame = game;
const downloadUrl = await this.getRealDebridDownloadUrl();
if (downloadUrl) {
this.realDebridTorrentId = null;
const gid = await HTTPDownload.startDownload(
const gid = await HttpDownload.startDownload(
game.downloadPath!,
downloadUrl
);
this.downloads.set(game.id!, gid);
this.downloadingGame = game;
}
}
@@ -165,7 +141,7 @@ export class RealDebridDownloader {
const gid = this.downloads.get(gameId);
if (gid) {
await HTTPDownload.cancelDownload(gid);
await HttpDownload.cancelDownload(gid);
this.downloads.delete(gameId);
}
@@ -177,7 +153,7 @@ export class RealDebridDownloader {
const gid = this.downloads.get(gameId);
if (gid) {
await HTTPDownload.resumeDownload(gid);
await HttpDownload.resumeDownload(gid);
}
}
}

View File

@@ -46,7 +46,7 @@ export class RealDebridClient {
static async selectAllFiles(id: string) {
const searchParams = new URLSearchParams({ files: "all" });
await this.instance.post(
return this.instance.post(
`/torrents/selectFiles/${id}`,
searchParams.toString()
);