diff --git a/src/core/network/libtorrent_int.py b/src/core/network/libtorrent_int.py index b5fcb45..801a890 100644 --- a/src/core/network/libtorrent_int.py +++ b/src/core/network/libtorrent_int.py @@ -1,4 +1,5 @@ import time +import os from core.utils.general.wrappers import run_thread from core.network.interface import get_interface_ip import threading @@ -51,8 +52,26 @@ def add_download(magnet_uri, dl_path=state.download_path): init_session() if magnet_uri in state.active_downloads: - consoleLog("Skipping Downloading, download already running...") - return + try: + handle = state.active_downloads[magnet_uri] + status = handle.status() + except RuntimeError as e: + consoleLog(f"Error in LibTorrent Handle: {e}") + + if status.has_metadata: + filepath = os.path.join(status.save_path, status.name) + + if not os.path.exists(filepath): + + consoleLog(f"File Deleted, redownloading: {status.name}") + state.dl_session.remove_torrent(handle) + del state.active_downloads[magnet_uri] + else: + consoleLog("Skipping Downloading, download already running...") + return False + else: + consoleLog("Skipping Downloading, download already running... ") + return False magnetdl = lt.parse_magnet_uri(magnet_uri) magnetdl.save_path = dl_path @@ -62,6 +81,7 @@ def add_download(magnet_uri, dl_path=state.download_path): consoleLog(f"Added {magnet_uri} to downloads") run_thread(threading.Thread(target=dl_status_loop)) + return True diff --git a/src/core/network/libtorrent_misc.py b/src/core/network/libtorrent_misc.py index 98ec561..8a3ccca 100644 --- a/src/core/network/libtorrent_misc.py +++ b/src/core/network/libtorrent_misc.py @@ -3,6 +3,7 @@ from core.utils.logging.logs import update_download_completed_by_hash from core.utils.logging.logs import consoleLog from plyer import notification import libtorrent as lt +import os import time @@ -58,3 +59,24 @@ def update_log(shutdown_event): except Exception: pass time.sleep(5) + +def check_deleted_files(shutdown_event): + while not shutdown_event.is_set(): + try: + for magnet_uri, magnetdl in list(state.active_downloads.items()): + if isinstance(magnetdl, dict): + continue + + status = magnetdl.status() + + if status.state == lt.torrent_status.seeding and status.has_metadata: + file_path = os.path.join(status.save_path, status.name) + + if not os.path.exists(file_path): + consoleLog(f"Registered File Deletion: {status.name}") + state.dl_session.remove_torrent(magnetdl) + del state.active_downloads[magnet_uri] + + except Exception: + pass + time.sleep(5) \ No newline at end of file diff --git a/src/core/utils/network/download.py b/src/core/utils/network/download.py index d5184af..79479d4 100644 --- a/src/core/utils/network/download.py +++ b/src/core/utils/network/download.py @@ -19,8 +19,8 @@ def run_download(item, posts, post_titles): consoleLog(f"Selected URL: {post_url}") magnet_uri = get_magnet_link(post_url) - add_download(magnet_uri) - add_download_log(item, post_url, magnet_uri, False) + if add_download(magnet_uri): + add_download_log(item, post_url, magnet_uri, False) def run_download_direct(magnet_uri): consoleLog(f"Direct download: {magnet_uri[:60]}") diff --git a/src/main.py b/src/main.py index 1743652..72a2ec1 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,7 @@ from core.interface.gui import MainWindow, windowCloseHelper from core.utils.data.state import state from core.utils.logging.logs import consoleLog -from core.network.libtorrent_misc import send_notification, update_log +from core.network.libtorrent_misc import send_notification, update_log, check_deleted_files from core.utils.logging.logs import get_download_logs from core.utils.general.shutdown import closehelper, shutdown_event from core.utils.general.wrappers import run_thread @@ -53,6 +53,8 @@ def main(): consoleLog("Started Thread: update_log") run_thread(threading.Thread(target=check_completed, args=(downloads, state.autoresume))) consoleLog("Started Thread: check_completed") + run_thread(threading.Thread(target=check_deleted_files, args=(shutdown_event,), daemon=True)) + consoleLog("Started Thread: check_deleted_files") check_downloads(downloads) elapsed = time.perf_counter() - start_time consoleLog(f"Initialization completed in {elapsed:.2f}s. Launching GUI")