Skip to content

Commit

Permalink
fix: possible fix for pixel drain and torbox cancel download
Browse files Browse the repository at this point in the history
  • Loading branch information
zamitto committed Dec 26, 2024
1 parent c9ae543 commit 6ea1f90
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 25 deletions.
5 changes: 3 additions & 2 deletions python_rpc/http_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ def __init__(self):
)
)

def start_download(self, url: str, save_path: str, header: str):
def start_download(self, url: str, save_path: str, header: str, out: str = None):
if self.download:
self.aria2.resume([self.download])
else:
downloads = self.aria2.add(url, options={"header": header, "dir": save_path})
downloads = self.aria2.add(url, options={"header": header, "dir": save_path, "out": out})

self.download = downloads[0]

def pause_download(self):
Expand Down
16 changes: 8 additions & 8 deletions python_rpc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
torrent_downloader = TorrentDownloader(torrent_session)
downloads[initial_download['game_id']] = torrent_downloader
try:
torrent_downloader.start_download(initial_download['url'], initial_download['save_path'], "")
torrent_downloader.start_download(initial_download['url'], initial_download['save_path'])
except Exception as e:
print("Error starting torrent download", e)
else:
http_downloader = HttpDownloader()
downloads[initial_download['game_id']] = http_downloader
try:
http_downloader.start_download(initial_download['url'], initial_download['save_path'], initial_download.get('header'))
http_downloader.start_download(initial_download['url'], initial_download['save_path'], initial_download.get('header'), initial_download.get("out"))
except Exception as e:
print("Error starting http download", e)

Expand All @@ -45,7 +45,7 @@
torrent_downloader = TorrentDownloader(torrent_session, lt.torrent_flags.upload_mode)
downloads[seed['game_id']] = torrent_downloader
try:
torrent_downloader.start_download(seed['url'], seed['save_path'], "")
torrent_downloader.start_download(seed['url'], seed['save_path'])
except Exception as e:
print("Error starting seeding", e)

Expand Down Expand Up @@ -140,18 +140,18 @@ def action():

if url.startswith('magnet'):
if existing_downloader and isinstance(existing_downloader, TorrentDownloader):
existing_downloader.start_download(url, data['save_path'], "")
existing_downloader.start_download(url, data['save_path'])
else:
torrent_downloader = TorrentDownloader(torrent_session)
downloads[game_id] = torrent_downloader
torrent_downloader.start_download(url, data['save_path'], "")
torrent_downloader.start_download(url, data['save_path'])
else:
if existing_downloader and isinstance(existing_downloader, HttpDownloader):
existing_downloader.start_download(url, data['save_path'], data.get('header'))
existing_downloader.start_download(url, data['save_path'], data.get('header'), data.get('out'))
else:
http_downloader = HttpDownloader()
downloads[game_id] = http_downloader
http_downloader.start_download(url, data['save_path'], data.get('header'))
http_downloader.start_download(url, data['save_path'], data.get('header'), data.get('out'))

downloading_game_id = game_id

Expand All @@ -167,7 +167,7 @@ def action():
elif action == 'resume_seeding':
torrent_downloader = TorrentDownloader(torrent_session, lt.torrent_flags.upload_mode)
downloads[game_id] = torrent_downloader
torrent_downloader.start_download(data['url'], data['save_path'], "")
torrent_downloader.start_download(data['url'], data['save_path'])
elif action == 'pause_seeding':
downloader = downloads.get(game_id)
if downloader:
Expand Down
2 changes: 1 addition & 1 deletion python_rpc/torrent_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init__(self, torrent_session, flags = lt.torrent_flags.auto_managed):
"http://bvarf.tracker.sh:2086/announce",
]

def start_download(self, magnet: str, save_path: str, header: str):
def start_download(self, magnet: str, save_path: str):
params = {'url': magnet, 'save_path': save_path, 'trackers': self.trackers, 'flags': self.flags}
self.torrent_handle = self.session.add_torrent(params)
self.torrent_handle.resume()
Expand Down
4 changes: 0 additions & 4 deletions src/main/events/auth/sign-out.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { registerEvent } from "../register-event";
import { DownloadManager, HydraApi, gamesPlaytime } from "@main/services";
import { dataSource } from "@main/data-source";
import { DownloadQueue, Game, UserAuth, UserSubscription } from "@main/entity";
import { PythonRPC } from "@main/services/python-rpc";

const signOut = async (_event: Electron.IpcMainInvokeEvent) => {
const databaseOperations = dataSource
Expand All @@ -27,9 +26,6 @@ const signOut = async (_event: Electron.IpcMainInvokeEvent) => {
/* Cancels any ongoing downloads */
DownloadManager.cancelDownload();

/* Disconnects libtorrent */
PythonRPC.kill();

HydraApi.handleSignOut();

await Promise.all([
Expand Down
15 changes: 11 additions & 4 deletions src/main/services/download/download-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { RealDebridClient } from "./real-debrid";
import path from "path";
import { logger } from "../logger";
import { TorBoxClient } from "./torbox";
import axios from "axios";

export class DownloadManager {
private static downloadingGameId: number | null = null;
Expand Down Expand Up @@ -262,11 +263,16 @@ export class DownloadManager {
case Downloader.PixelDrain: {
const id = game!.uri!.split("/").pop();

const name = await axios
.get(`https://pixeldrain.com/api/file/${id}/info`)
.then((res) => res.data.name as string);

return {
action: "start",
game_id: game.id,
url: `https://pixeldrain.com/api/file/${id}?download`,
save_path: game.downloadPath!,
out: name,
};
}
case Downloader.Qiwi: {
Expand Down Expand Up @@ -297,15 +303,16 @@ export class DownloadManager {
};
}
case Downloader.TorBox: {
const downloadUrl = await TorBoxClient.getDownloadUrl(game.uri!);
console.log(downloadUrl);
const { name, url } = await TorBoxClient.getDownloadInfo(game.uri!);
console.log(url, name);

if (!downloadUrl) return;
if (!url) return;
return {
action: "start",
game_id: game.id,
url: downloadUrl,
url,
save_path: game.downloadPath!,
out: name,
};
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/services/download/torbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,23 @@ export class TorBoxClient {
return response.data.data;
}

static async getTorrentId(magnetUri: string) {
static async getTorrentIdAndName(magnetUri: string) {
const userTorrents = await this.getAllTorrentsFromUser();

const { infoHash } = await parseTorrent(magnetUri);
const userTorrent = userTorrents.find(
(userTorrent) => userTorrent.hash === infoHash
);

if (userTorrent) return userTorrent.id;
if (userTorrent) return { id: userTorrent.id, name: userTorrent.name };

const torrent = await this.addMagnet(magnetUri);
return torrent.torrent_id;
return { id: torrent.torrent_id, name: torrent.name };
}

static async getDownloadUrl(uri: string) {
const id = await this.getTorrentId(uri);
return this.requestLink(id);
static async getDownloadInfo(uri: string) {
const { id, name } = await this.getTorrentIdAndName(uri);
const url = await this.requestLink(id);
return { url, name: `${name}.zip` };
}
}
1 change: 1 addition & 0 deletions src/types/torbox.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface TorBoxAddTorrentRequest {
torrent_id: number;
name: string;
hash: string;
size: number;
};
}

Expand Down

0 comments on commit 6ea1f90

Please sign in to comment.