diff --git a/core/network/libtorrent_int.py b/core/network/libtorrent_int.py index 47e37c6..ee6d84d 100644 --- a/core/network/libtorrent_int.py +++ b/core/network/libtorrent_int.py @@ -1,9 +1,13 @@ -import subprocess +import time +from core.utils.general.wrappers import run_thread +import threading import libtorrent as lt from core.utils.data.state import state from core.utils.general.logs import consoleLog +global loop_running +loop_running = False def init_session(): if state.dl_session is not None: @@ -23,11 +27,18 @@ def init_session(): "active_downloads": 10 } + + state.dl_session.apply_settings(settings) + consoleLog("Initialized Session") + def add_download(magnet_uri, dl_path=state.download_path): + if state.active_downloads is None: + state.active_downloads = {} + init_session() if magnet_uri in state.active_downloads: @@ -39,5 +50,51 @@ def add_download(magnet_uri, dl_path=state.download_path): magnetdl = lt.parse_magnet_uri(magnet_uri) magnetdl.save_path = dl_path + consoleLog(f"Download Path: {dl_path}") + download = state.dl_session.add_torrent(magnetdl) - state.active_downloads[magnet_uri] = magnetdl + state.active_downloads[magnet_uri] = download + consoleLog("added download") + + run_thread(threading.Thread(target=dl_status_loop)) + + + +def dl_status_loop(): + global loop_running + + if loop_running == True: + return + + loop_running = True + completed = [] + + if not state.active_downloads: + consoleLog("No active downloads") + return + + while state.active_downloads: + completed.clear() + + for magnet_uri, magnetdl in list(state.active_downloads.items()): + status = magnetdl.status() + + if status.state == lt.torrent_status.seeding: + completed.append(magnet_uri) + consoleLog(f"Download completed: {status.name}") + continue + + for magnet_uri in completed: + magnetdl = state.active_downloads[magnet_uri] + state.dl_session.remove_torrent(magnetdl) + del state.active_downloads[magnet_uri] + + if not state.active_downloads: + loop_running = False + break + + time.sleep(1) + + + + diff --git a/core/network/libtorrent_wrapper.py b/core/network/libtorrent_wrapper.py new file mode 100644 index 0000000..4a24292 --- /dev/null +++ b/core/network/libtorrent_wrapper.py @@ -0,0 +1,12 @@ +from core.utils.general.logs import consoleLog +from core.network.libtorrent_int import add_download, dl_status_loop + +def add_magnet(uri): + if uri is not None and uri.startswith("magnet:?"): + add_download(uri) + consoleLog("Magnet URI added to LibTorrent") + else: + consoleLog(f"Invalid Magnet Link: {uri}") + + + diff --git a/core/utils/network/download.py b/core/utils/network/download.py index 6351f8f..4863f34 100644 --- a/core/utils/network/download.py +++ b/core/utils/network/download.py @@ -2,8 +2,7 @@ from core.utils.data.state import state from core.utils.general.logs import consoleLog from core.utils.data.tracker import get_item_url from core.utils.data.tracker import get_magnet_link -from core.network.aria2_wrapper import start_client -from core.network.aria2_wrapper import add_magnet +from core.network.libtorrent_wrapper import add_download from core.utils.general.wrappers import run_thread from core.utils.general.logs import add_download_log @@ -21,13 +20,12 @@ def run_download(item, posts, post_titles): post_url = get_item_url(item, posts, post_titles) consoleLog(f"Selected URL: {post_url}") magnet_uri = get_magnet_link(post_url) - start_client() + add_download_log(item, post_url, magnet_uri, False) - add_magnet(magnet_uri) + add_download(magnet_uri) def run_download_direct(magnet_uri): - start_client() - add_magnet(magnet_uri) + add_download(magnet_uri)