From 9ba11bfa78296ba3dfab9e42e9f07e4fafa7c28a Mon Sep 17 00:00:00 2001 From: Vxrtrauter <101264710+Vxrtrauter@users.noreply.github.com> Date: Sun, 8 Mar 2026 13:32:30 +0100 Subject: [PATCH] feat: enhance error handling and logging in various modules; improve timeout settings for network requests / fix basic issues --- src/core/data/scrapers/uztracker.py | 4 +++- src/core/interface/dialogs/settings.py | 2 -- src/core/interface/gui.py | 15 +++++---------- src/core/interface/utils/searchhelper.py | 2 +- src/core/network/libtorrent_int.py | 22 ++++++++++++++-------- src/core/utils/data/tracker.py | 2 +- src/core/utils/logging/loghandler.py | 5 ++++- src/core/utils/logging/logs.py | 2 +- src/core/utils/network/update_checker.py | 4 ++-- src/main.py | 3 ++- 10 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/core/data/scrapers/uztracker.py b/src/core/data/scrapers/uztracker.py index 956629f..ac2aa23 100644 --- a/src/core/data/scrapers/uztracker.py +++ b/src/core/data/scrapers/uztracker.py @@ -16,7 +16,9 @@ def scrape_uztracker(query): links = soup.find_all('tr', class_="tCenter hl-tr", id=lambda x: x and x.startswith('tor_')) for link in links: - theme_link = link.find('a', class_="genmed tLink", href=lambda x: x and x.startswith('./viewtopic')) + theme_link = link.find('a', class_="genmed tLink", href=lambda x: x and x.startswith('./viewtopic')) + if not theme_link or not theme_link.b: + continue url = urljoin(base_url, theme_link['href']) title = theme_link.b.text author_link = link.find('a', class_="med") diff --git a/src/core/interface/dialogs/settings.py b/src/core/interface/dialogs/settings.py index d9b016e..550df37 100644 --- a/src/core/interface/dialogs/settings.py +++ b/src/core/interface/dialogs/settings.py @@ -224,8 +224,6 @@ def settings_dialog(self): else: interface_select.setCurrentIndex(0) - interface_select.setFixedWidth(180) - interface_select.setFixedHeight(30) interface_select.setFixedWidth(180) interface_select.setFixedHeight(30) interface_layout.addWidget(interface_select) diff --git a/src/core/interface/gui.py b/src/core/interface/gui.py index 8634e4e..bf82784 100644 --- a/src/core/interface/gui.py +++ b/src/core/interface/gui.py @@ -30,7 +30,6 @@ import libtorrent as lt import sys import json import base64 -import winreg from core.utils.logging.logs import consoleLog, remove_download_log, flush_log_buffer from core.utils.general.wrappers import run_thread from core.utils.data.state import state @@ -157,17 +156,13 @@ def download_update(latest_version): response = r.get(url, allow_redirects=True, stream=True) total = int(response.headers.get("content-length", 0)) downloaded = 0 - chunks = [] - for chunk in response.iter_content(chunk_size=65536): - chunks.append(chunk) - downloaded += len(chunk) - if total > 0: - progress.setValue(int(downloaded * 100 / total)) - QtWidgets.QApplication.processEvents() - with open(installer_path, "wb") as f: - for chunk in chunks: + for chunk in response.iter_content(chunk_size=65536): f.write(chunk) + downloaded += len(chunk) + if total > 0: + progress.setValue(int(downloaded * 100 / total)) + QtWidgets.QApplication.processEvents() if not os.path.exists(installer_path): progress.close() diff --git a/src/core/interface/utils/searchhelper.py b/src/core/interface/utils/searchhelper.py index 9dae1a5..eda868a 100644 --- a/src/core/interface/utils/searchhelper.py +++ b/src/core/interface/utils/searchhelper.py @@ -50,7 +50,7 @@ def return_pressed(self): elif state.tracker is not None: state.posts = scrapers[state.tracker](search_text) - if state.posts == []: + if not state.posts: consoleLog(f"No Results for {search_text}") state.tracker_list[state.tracker].clear() self.show_empty_results(True) diff --git a/src/core/network/libtorrent_int.py b/src/core/network/libtorrent_int.py index d19e4fb..9160b66 100644 --- a/src/core/network/libtorrent_int.py +++ b/src/core/network/libtorrent_int.py @@ -74,10 +74,13 @@ def add_download(magnet_uri, dl_path=state.download_path): consoleLog("Skipping Downloading, download already running... ") return False - magnetdl = lt.parse_magnet_uri(magnet_uri) - magnetdl.save_path = dl_path - - download = state.dl_session.add_torrent(magnetdl) + try: + magnetdl = lt.parse_magnet_uri(magnet_uri) + magnetdl.save_path = dl_path + download = state.dl_session.add_torrent(magnetdl) + except Exception as e: + consoleLog(f"Failed to add torrent: {e}") + return False state.active_downloads[magnet_uri] = download consoleLog(f"Added {magnet_uri} to downloads") @@ -95,10 +98,13 @@ def add_seed(magnet_uri, file_path): consoleLog("Already seeding this torrent") return False - magnetdl = lt.parse_magnet_uri(magnet_uri) - magnetdl.save_path = os.path.dirname(file_path) - - handle = state.dl_session.add_torrent(magnetdl) + try: + magnetdl = lt.parse_magnet_uri(magnet_uri) + magnetdl.save_path = os.path.dirname(file_path) + handle = state.dl_session.add_torrent(magnetdl) + except Exception as e: + consoleLog(f"Failed to add seed: {e}") + return False state.active_downloads[magnet_uri] = handle state.seeded_magnets.add(magnet_uri) return True diff --git a/src/core/utils/data/tracker.py b/src/core/utils/data/tracker.py index 962fa38..79ed98f 100644 --- a/src/core/utils/data/tracker.py +++ b/src/core/utils/data/tracker.py @@ -30,7 +30,7 @@ def get_item_url(item, posts, post_titles): # softwarelist currentitem, post lis def get_magnet_link(post_url): try: - response = requests.get(post_url) # eventually impl. cloudscraper + response = requests.get(post_url, timeout=15) # eventually impl. cloudscraper consoleLog("Sent Request to retrieve Magnet Link...") response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') diff --git a/src/core/utils/logging/loghandler.py b/src/core/utils/logging/loghandler.py index b48c81d..5d48d10 100644 --- a/src/core/utils/logging/loghandler.py +++ b/src/core/utils/logging/loghandler.py @@ -22,7 +22,10 @@ def check_downloads(downloads): for download in downloads: if os.path.exists(download.path): consoleLog(f"Existing Download: {download.title}") - seed_magnet(download.magnet_uri, download.path) + try: + seed_magnet(download.magnet_uri, download.path) + except Exception as e: + consoleLog(f"Failed to seed {download.title}: {e}") else: consoleLog(f"Inexistent Download: {download.title}") remove_download_log(download.magnet_uri) diff --git a/src/core/utils/logging/logs.py b/src/core/utils/logging/logs.py index b8186d2..c78fd7c 100644 --- a/src/core/utils/logging/logs.py +++ b/src/core/utils/logging/logs.py @@ -89,7 +89,7 @@ def _remove_download_log_inner(magnet_uri) -> DownloadList: magnet_link = (magnet_uri or "").strip() - title = next((getattr(d, 'title', 'Unknown') for d in downloads if (getattr(d, 'magnet_uri', None) or '').strip() == magnet_link or (getattr(d, 'url', None) or '').strip() == magnet_link)) + title = next((getattr(d, 'title', 'Unknown') for d in downloads if (getattr(d, 'magnet_uri', None) or '').strip() == magnet_link or (getattr(d, 'url', None) or '').strip() == magnet_link), 'Unknown') downloads = [d for d in downloads if (getattr(d, 'magnet_uri', None) or "").strip() != magnet_link and (getattr(d, 'url', None) or "").strip() != magnet_link] consoleLog(f"Removed {title} from Log File") diff --git a/src/core/utils/network/update_checker.py b/src/core/utils/network/update_checker.py index 31955a9..a250884 100644 --- a/src/core/utils/network/update_checker.py +++ b/src/core/utils/network/update_checker.py @@ -4,11 +4,11 @@ from core.utils.logging.logs import consoleLog def check_for_updates(): url = f"https://api.github.com/repos/KeksPirates/SoftwareManager/releases" - response = requests.get(url) + response = requests.get(url, timeout=15) if response.status_code != 200: consoleLog(f"Failed to fetch releases: {response.status_code}") - exit(1) + return None, None releases = response.json() releases.sort(key=lambda r: r["published_at"], reverse=True) diff --git a/src/main.py b/src/main.py index 097f67d..8101f87 100644 --- a/src/main.py +++ b/src/main.py @@ -56,7 +56,8 @@ def main(): 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) + run_thread(threading.Thread(target=check_downloads, args=(downloads,))) + consoleLog("Started Thread: check_downloads") elapsed = time.perf_counter() - start_time consoleLog(f"Initialization completed in {elapsed:.2f}s. Launching GUI") run_gui()