From 04134d3c0ababea5d4fe29e3c95cf040118d4bea Mon Sep 17 00:00:00 2001 From: bobokun Date: Mon, 10 Apr 2023 20:50:55 -0400 Subject: [PATCH] more refactoring --- modules/core/cross_seed.py | 2 +- modules/core/remove_unregistered.py | 42 ++++++--------------------- modules/qbittorrent.py | 16 ++++------- modules/util.py | 44 +++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/modules/core/cross_seed.py b/modules/core/cross_seed.py index 54194c8..3c5c255 100644 --- a/modules/core/cross_seed.py +++ b/modules/core/cross_seed.py @@ -39,7 +39,7 @@ class CrossSeed: dest = os.path.join(self.qbt.torrentinfo[t_name]["save_path"], "") src = os.path.join(dir_cs, file) dir_cs_out = os.path.join(dir_cs, "qbit_manage_added", file) - category = self.get_category(dest) + category = self.qbt.global_max_ratioget_category(dest) # Only add cross-seed torrent if original torrent is complete if self.qbt.torrentinfo[t_name]["is_complete"]: categories.append(category) diff --git a/modules/core/remove_unregistered.py b/modules/core/remove_unregistered.py index 780a728..aeadad9 100644 --- a/modules/core/remove_unregistered.py +++ b/modules/core/remove_unregistered.py @@ -2,38 +2,12 @@ from qbittorrentapi import NotFound404Error from modules import util from modules.util import list_in_text +from modules.util import TorrentMessages logger = util.logger class RemoveUnregistered: - UNREGISTERED_MSGS = [ - "UNREGISTERED", - "TORRENT NOT FOUND", - "TORRENT IS NOT FOUND", - "NOT REGISTERED", - "NOT EXIST", - "UNKNOWN TORRENT", - "TRUMP", - "RETITLED", - "TRUNCATED", - "TORRENT IS NOT AUTHORIZED FOR USE ON THIS TRACKER", - ] - - IGNORE_MSGS = [ - "YOU HAVE REACHED THE CLIENT LIMIT FOR THIS TORRENT", - "MISSING PASSKEY", - "MISSING INFO_HASH", - "PASSKEY IS INVALID", - "INVALID PASSKEY", - "EXPECTED VALUE (LIST, DICT, INT OR STRING) IN BENCODED STRING", - "COULD NOT PARSE BENCODED DATA", - "STREAM TRUNCATED", - ] - - # Tracker has been contacted, but it is not working (or doesn't send proper replies) - TORRENT_STATUS_NOT_WORKING = 4 - def __init__(self, qbit_manager): self.qbt = qbit_manager self.config = qbit_manager.config @@ -98,20 +72,20 @@ class RemoveUnregistered: msg = trk.msg # Tag any error torrents if self.cfg_tag_error: - if trk.status == self.TORRENT_STATUS_NOT_WORKING and self.tag_error not in check_tags: + if trk.status == TorrentMessages.TORRENT_STATUS_NOT_WORKING and self.tag_error not in check_tags: self.tag_tracker_error() if self.cfg_rem_unregistered: # Tag any error torrents that are not unregistered if ( - not list_in_text(msg_up, self.UNREGISTERED_MSGS) - and trk.status == self.TORRENT_STATUS_NOT_WORKING + not list_in_text(msg_up, TorrentMessages.UNREGISTERED_MSGS) + and trk.status == TorrentMessages.TORRENT_STATUS_NOT_WORKING and self.tag_error not in check_tags ): # Check for unregistered torrents using BHD API if the tracker is BHD if ( "tracker.beyond-hd.me" in tracker["url"] and self.config.beyond_hd is not None - and not list_in_text(msg_up, self.IGNORE_MSGS) + and not list_in_text(msg_up, TorrentMessages.IGNORE_MSGS) ): json = {"info_hash": torrent.hash} response = self.config.beyond_hd.search(json) @@ -120,9 +94,9 @@ class RemoveUnregistered: break self.tag_tracker_error() if ( - list_in_text(msg_up, self.UNREGISTERED_MSGS) - and not list_in_text(msg_up, self.IGNORE_MSGS) - and trk.status == self.TORRENT_STATUS_NOT_WORKING + list_in_text(msg_up, TorrentMessages.UNREGISTERED_MSGS) + and not list_in_text(msg_up, TorrentMessages.IGNORE_MSGS) + and trk.status == TorrentMessages.TORRENT_STATUS_NOT_WORKING ): self.del_unregistered() break diff --git a/modules/qbittorrent.py b/modules/qbittorrent.py index 3effec6..9cce1f5 100755 --- a/modules/qbittorrent.py +++ b/modules/qbittorrent.py @@ -12,6 +12,7 @@ from qbittorrentapi import Version from modules import util from modules.util import Failed from modules.util import list_in_text +from modules.util import TorrentMessages logger = util.logger @@ -157,20 +158,13 @@ class Qbt: if trk.url.startswith("http"): status = trk.status msg = trk.msg.upper() - exception = [ - "DOWN", - "DOWN.", - "IT MAY BE DOWN,", - "UNREACHABLE", - "(UNREACHABLE)", - "BAD GATEWAY", - "TRACKER UNAVAILABLE", - ] - if trk.status == 2: + if trk.status == TorrentMessages.TORRENT_STATUS_WORKING: working_tracker = True break # Add any potential unregistered torrents to a list - if trk.status == 4 and not list_in_text(msg, exception): + if trk.status == TorrentMessages.TORRENT_STATUS_NOT_WORKING and not list_in_text( + msg, TorrentMessages.EXCEPTIONS_MSGS + ): issue["potential"] = True issue["msg"] = msg issue["status"] = status diff --git a/modules/util.py b/modules/util.py index ad42ba0..05e39b3 100755 --- a/modules/util.py +++ b/modules/util.py @@ -33,6 +33,50 @@ def get_list(data, lower=False, split=True, int_list=False): return [d.strip() for d in str(data).split(",")] +class TorrentMessages: + """Contains list of messages to check against a status of a torrent""" + + UNREGISTERED_MSGS = [ + "UNREGISTERED", + "TORRENT NOT FOUND", + "TORRENT IS NOT FOUND", + "NOT REGISTERED", + "NOT EXIST", + "UNKNOWN TORRENT", + "TRUMP", + "RETITLED", + "TRUNCATED", + "TORRENT IS NOT AUTHORIZED FOR USE ON THIS TRACKER", + ] + + IGNORE_MSGS = [ + "YOU HAVE REACHED THE CLIENT LIMIT FOR THIS TORRENT", + "MISSING PASSKEY", + "MISSING INFO_HASH", + "PASSKEY IS INVALID", + "INVALID PASSKEY", + "EXPECTED VALUE (LIST, DICT, INT OR STRING) IN BENCODED STRING", + "COULD NOT PARSE BENCODED DATA", + "STREAM TRUNCATED", + ] + + EXCEPTIONS_MSGS = [ + "DOWN", + "DOWN.", + "IT MAY BE DOWN,", + "UNREACHABLE", + "(UNREACHABLE)", + "BAD GATEWAY", + "TRACKER UNAVAILABLE", + ] + + TORRENT_STATUS_DISABLED = 0 # Tracker is disabled (used for DHT, PeX, and LSD) + TORRENT_STATUS_NOT_CONTACTED = 1 # Tracker has not been contacted yet + TORRENT_STATUS_WORKING = 2 # Tracker has been contacted and is working + TORRENT_STATUS_UPDATING = 3 # Tracker is updating + TORRENT_STATUS_NOT_WORKING = 4 # Tracker has been contacted, but it is not working (or doesn't send proper replies) + + class check: """Check for attributes in config."""