hydra/torrent-client/main.py

96 lines
3.2 KiB
Python
Raw Normal View History

2024-06-27 16:51:13 +03:00
import libtorrent as lt
import sys
2024-06-27 19:18:48 +03:00
from http.server import HTTPServer, BaseHTTPRequestHandler
2024-06-27 16:51:13 +03:00
import json
import threading
import time
torrent_port = sys.argv[1]
2024-06-27 19:18:48 +03:00
http_port = sys.argv[2]
2024-06-27 20:10:30 +03:00
class Downloader:
def __init__(self):
self.torrent_handles = {}
self.downloading_game_id = -1
self.session = lt.session({'listen_interfaces': '0.0.0.0:{port}'.format(port=torrent_port)})
def start_download(self, game_id: int, magnet: str, save_path: str):
params = {'url': magnet, 'save_path': save_path}
torrent_handle = self.session.add_torrent(params)
self.torrent_handles[game_id] = torrent_handle
torrent_handle.set_flags(lt.torrent_flags.auto_managed)
torrent_handle.resume()
self.downloading_game_id = game_id
def pause_download(self, game_id: int):
torrent_handle = self.torrent_handles.get(game_id)
if torrent_handle:
torrent_handle.pause()
torrent_handle.unset_flags(lt.torrent_flags.auto_managed)
self.downloading_game_id = -1
def cancel_download(self, game_id: int):
torrent_handle = self.torrent_handles.get(game_id)
if torrent_handle:
torrent_handle.pause()
self.session.remove_torrent(torrent_handle)
self.torrent_handles[game_id] = None
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)
2024-06-27 16:51:13 +03:00
status = torrent_handle.status()
info = torrent_handle.get_torrent_info()
2024-06-27 20:10:30 +03:00
return {
2024-06-27 16:51:13 +03:00
'folderName': info.name() if info else "",
'fileSize': info.total_size() if info else 0,
2024-06-27 20:10:30 +03:00
'gameId': self.downloading_game_id,
2024-06-27 16:51:13 +03:00
'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,
2024-06-27 19:18:48 +03:00
}
2024-06-27 16:51:13 +03:00
2024-06-27 20:10:30 +03:00
downloader = Downloader()
2024-06-27 16:51:13 +03:00
2024-06-27 19:18:48 +03:00
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/status":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
2024-06-27 20:10:30 +03:00
status = downloader.get_download_status()
self.wfile.write(json.dumps(status).encode('utf-8'))
2024-06-27 19:18:48 +03:00
def do_POST(self):
if self.path == "/action":
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = json.loads(post_data.decode('utf-8'))
if data['action'] == 'start':
2024-06-27 20:10:30 +03:00
downloader.start_download(data['game_id'], data['magnet'], data['save_path'])
2024-06-27 19:18:48 +03:00
elif data['action'] == 'pause':
2024-06-27 20:10:30 +03:00
downloader.pause_download(data['game_id'])
2024-06-27 19:18:48 +03:00
elif data['action'] == 'cancel':
2024-06-27 20:10:30 +03:00
downloader.cancel_download(data['game_id'])
2024-06-27 19:18:48 +03:00
self.send_response(200)
self.end_headers()
2024-06-27 16:51:13 +03:00
if __name__ == "__main__":
2024-06-27 19:18:48 +03:00
httpd = HTTPServer(("", int(http_port)), Handler)
2024-06-27 20:10:30 +03:00
httpd.serve_forever()