feat: add get-sedding endpoint

This commit is contained in:
Hachi-R 2024-11-06 21:58:55 -03:00
parent e1a5a95ceb
commit 6295637b48
3 changed files with 48 additions and 13 deletions

View file

@ -61,9 +61,9 @@ export class PythonInstance {
} }
public static async getSeedingList() { public static async getSeedingList() {
const response = await this.rpc.get<LibtorrentPayload>("/status"); const response = await this.rpc.get<LibtorrentPayload>("/seed-list");
return response.data.seeding; return response.data;
} }
public static async getStatus() { public static async getStatus() {

View file

@ -50,6 +50,21 @@ class Handler(BaseHTTPRequestHandler):
self.wfile.write(json.dumps(status).encode('utf-8')) 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": elif self.path == "/healthcheck":
self.send_response(200) self.send_response(200)
self.end_headers() self.end_headers()

View file

@ -137,12 +137,39 @@ class TorrentDownloader:
self.downloading_game_id = -1 self.downloading_game_id = -1
def get_download_status(self): 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 = { response = {
'downloading': None, 'folderName': info.name() if info else "",
'seeding': [] '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(): for game_id, torrent_handle in self.torrent_handles.items():
if game_id == self.downloading_game_id:
return
status = torrent_handle.status() status = torrent_handle.status()
info = torrent_handle.torrent_file() info = torrent_handle.torrent_file()
@ -159,15 +186,8 @@ class TorrentDownloader:
'bytesDownloaded': status.progress * info.total_size() if info else status.all_time_download, 'bytesDownloaded': status.progress * info.total_size() if info else status.all_time_download,
} }
if game_id == self.downloading_game_id: if status.state == 5:
response['downloading'] = torrent_info response.append(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)
# print(response) # print(response)
return response return response