diff --git a/src/main/services/download/python-instance.ts b/src/main/services/download/python-instance.ts index 438cb39c..72823edf 100644 --- a/src/main/services/download/python-instance.ts +++ b/src/main/services/download/python-instance.ts @@ -61,9 +61,9 @@ export class PythonInstance { } public static async getSeedingList() { - const response = await this.rpc.get("/status"); + const response = await this.rpc.get("/seed-list"); - return response.data.seeding; + return response.data; } public static async getStatus() { diff --git a/torrent-client/main.py b/torrent-client/main.py index ee976518..c82a658e 100644 --- a/torrent-client/main.py +++ b/torrent-client/main.py @@ -50,6 +50,21 @@ class Handler(BaseHTTPRequestHandler): self.wfile.write(json.dumps(status).encode('utf-8')) + + elif self.path == "/seed-list": + if self.headers.get(self.rpc_password_header) != rpc_password: + self.send_response(401) + self.end_headers() + return + + self.send_response(200) + self.send_header("Content-type", "application/json") + self.end_headers() + + seed_list = torrent_downloader.get_seed_list() + + self.wfile.write(json.dumps(seed_list).encode('utf-8')) + elif self.path == "/healthcheck": self.send_response(200) self.end_headers() diff --git a/torrent-client/torrent_downloader.py b/torrent-client/torrent_downloader.py index 500d1bec..5738efb8 100644 --- a/torrent-client/torrent_downloader.py +++ b/torrent-client/torrent_downloader.py @@ -137,12 +137,39 @@ class TorrentDownloader: self.downloading_game_id = -1 def get_download_status(self): + if self.downloading_game_id == -1: + return None + + torrent_handle = self.torrent_handles.get(self.downloading_game_id) + + status = torrent_handle.status() + info = torrent_handle.get_torrent_info() + response = { - 'downloading': None, - 'seeding': [] + 'folderName': info.name() if info else "", + 'fileSize': info.total_size() if info else 0, + 'gameId': self.downloading_game_id, + 'progress': status.progress, + 'downloadSpeed': status.download_rate, + 'numPeers': status.num_peers, + 'numSeeds': status.num_seeds, + 'status': status.state, + 'bytesDownloaded': status.progress * info.total_size() if info else status.all_time_download, } + if status.progress == 1: + self.session.remove_torrent(torrent_handle) + self.downloading_game_id = -1 + + return response + + def get_seed_list(self): + response = [] + for game_id, torrent_handle in self.torrent_handles.items(): + if game_id == self.downloading_game_id: + return + status = torrent_handle.status() info = torrent_handle.torrent_file() @@ -158,16 +185,9 @@ class TorrentDownloader: 'status': status.state, 'bytesDownloaded': status.progress * info.total_size() if info else status.all_time_download, } - - if game_id == self.downloading_game_id: - response['downloading'] = torrent_info - - if status.progress == 1: - # this probably should stay here - self.downloading_game_id = -1 - elif status.state == 5: - response['seeding'].append(torrent_info) + if status.state == 5: + response.append(torrent_info) # print(response) return response