fix: removing python tick

This commit is contained in:
Chubby Granny Chaser 2024-06-27 18:10:30 +01:00
parent c26315219e
commit 9f9ea6ee88
No known key found for this signature in database
2 changed files with 39 additions and 65 deletions

View file

@ -15,7 +15,6 @@ export const RPC_PORT = "8084";
const commonArgs = [BITTORRENT_PORT, RPC_PORT]; const commonArgs = [BITTORRENT_PORT, RPC_PORT];
export const startTorrentClient = () => { export const startTorrentClient = () => {
console.log("CALLED");
if (app.isPackaged) { if (app.isPackaged) {
const binaryName = binaryNameByPlatform[process.platform]!; const binaryName = binaryNameByPlatform[process.platform]!;
const binaryPath = path.join( const binaryPath = path.join(

View file

@ -8,64 +8,49 @@ import time
torrent_port = sys.argv[1] torrent_port = sys.argv[1]
http_port = sys.argv[2] http_port = sys.argv[2]
print(http_port) class Downloader:
def __init__(self):
session = lt.session({'listen_interfaces': '0.0.0.0:{port}'.format(port=torrent_port)}) self.torrent_handles = {}
self.downloading_game_id = -1
torrent_handles = {} self.session = lt.session({'listen_interfaces': '0.0.0.0:{port}'.format(port=torrent_port)})
downloading_game_id = -1
def start_download(game_id: int, magnet: str, save_path: str):
global torrent_handles
global downloading_game_id
def start_download(self, game_id: int, magnet: str, save_path: str):
params = {'url': magnet, 'save_path': save_path} params = {'url': magnet, 'save_path': save_path}
torrent_handle = session.add_torrent(params) torrent_handle = self.session.add_torrent(params)
torrent_handles[game_id] = torrent_handle self.torrent_handles[game_id] = torrent_handle
torrent_handle.set_flags(lt.torrent_flags.auto_managed) torrent_handle.set_flags(lt.torrent_flags.auto_managed)
torrent_handle.resume() torrent_handle.resume()
downloading_game_id = game_id self.downloading_game_id = game_id
def pause_download(game_id: int): def pause_download(self, game_id: int):
global torrent_handles torrent_handle = self.torrent_handles.get(game_id)
global downloading_game_id
torrent_handle = torrent_handles.get(game_id)
if torrent_handle: if torrent_handle:
torrent_handle.pause() torrent_handle.pause()
torrent_handle.unset_flags(lt.torrent_flags.auto_managed) torrent_handle.unset_flags(lt.torrent_flags.auto_managed)
downloading_game_id = -1 self.downloading_game_id = -1
def cancel_download(game_id: int): def cancel_download(self, game_id: int):
global torrent_handles torrent_handle = self.torrent_handles.get(game_id)
global downloading_game_id
torrent_handle = torrent_handles.get(game_id)
if torrent_handle: if torrent_handle:
torrent_handle.pause() torrent_handle.pause()
session.remove_torrent(torrent_handle) self.session.remove_torrent(torrent_handle)
torrent_handles[game_id] = None self.torrent_handles[game_id] = None
downloading_game_id =-1 self.downloading_game_id = -1
def get_download_updates(): def get_download_status(self):
global torrent_handles if self.downloading_game_id == -1:
global downloading_game_id return None
while True: torrent_handle = self.torrent_handles.get(self.downloading_game_id)
if downloading_game_id == -1:
time.sleep(0.5)
continue
torrent_handle = torrent_handles.get(downloading_game_id)
status = torrent_handle.status() status = torrent_handle.status()
info = torrent_handle.get_torrent_info() info = torrent_handle.get_torrent_info()
Handler.current_status = { return {
'folderName': info.name() if info else "", 'folderName': info.name() if info else "",
'fileSize': info.total_size() if info else 0, 'fileSize': info.total_size() if info else 0,
'gameId': downloading_game_id, 'gameId': self.downloading_game_id,
'progress': status.progress, 'progress': status.progress,
'downloadSpeed': status.download_rate, 'downloadSpeed': status.download_rate,
'numPeers': status.num_peers, 'numPeers': status.num_peers,
@ -74,22 +59,19 @@ def get_download_updates():
'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 status.progress == 1:
cancel_download(downloading_game_id)
time.sleep(0.5)
downloader = Downloader()
class Handler(BaseHTTPRequestHandler): class Handler(BaseHTTPRequestHandler):
current_status = None
def do_GET(self): def do_GET(self):
if self.path == "/status": if self.path == "/status":
self.send_response(200) self.send_response(200)
self.send_header("Content-type", "application/json") self.send_header("Content-type", "application/json")
self.end_headers() self.end_headers()
self.wfile.write(json.dumps(self.current_status).encode('utf-8')) status = downloader.get_download_status()
self.wfile.write(json.dumps(status).encode('utf-8'))
def do_POST(self): def do_POST(self):
if self.path == "/action": if self.path == "/action":
@ -98,23 +80,16 @@ class Handler(BaseHTTPRequestHandler):
data = json.loads(post_data.decode('utf-8')) data = json.loads(post_data.decode('utf-8'))
if data['action'] == 'start': if data['action'] == 'start':
start_download(data['game_id'], data['magnet'], data['save_path']) downloader.start_download(data['game_id'], data['magnet'], data['save_path'])
elif data['action'] == 'pause': elif data['action'] == 'pause':
pause_download(data['game_id']) downloader.pause_download(data['game_id'])
self.current_status = None
elif data['action'] == 'cancel': elif data['action'] == 'cancel':
cancel_download(data['game_id']) downloader.cancel_download(data['game_id'])
self.current_status = None
self.send_response(200) self.send_response(200)
self.end_headers() self.end_headers()
if __name__ == "__main__": if __name__ == "__main__":
p1 = threading.Thread(target=get_download_updates)
httpd = HTTPServer(("", int(http_port)), Handler) httpd = HTTPServer(("", int(http_port)), Handler)
p2 = threading.Thread(target=httpd.serve_forever) httpd.serve_forever()
p1.start()
p2.start()