From bcf4e3e0be339530100102c834c59021faee7d65 Mon Sep 17 00:00:00 2001 From: Maxim Myalin Date: Sat, 21 Sep 2024 18:06:24 +0300 Subject: [PATCH 1/4] Update arss.py Signed-off-by: Maxim Myalin --- qBitrr/arss.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/qBitrr/arss.py b/qBitrr/arss.py index 25a8c129..92aea869 100755 --- a/qBitrr/arss.py +++ b/qBitrr/arss.py @@ -4460,9 +4460,10 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - .first() ) if episode.EpisodeFileId != 0: - cfunmet = customFormat < episode.CustomFormatScore - if self.force_minimum_custom_format: - cfunmet = cfunmet & customFormat < episode.MinCustomFormatScore + cfunmet = customFormat < episode.CustomFormatScore + if self.force_minimum_custom_format: + cfunmet = cfunmet & customFormat < episode.MinCustomFormatScore + if cfunmet: return True else: @@ -4523,8 +4524,8 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - ) if movie.MovieFileId != 0: cfunmet = customFormat < movie.CustomFormatScore - if self.force_minimum_custom_format: - cfunmet = cfunmet & customFormat < movie.MinCustomFormatScore + if self.force_minimum_custom_format: + cfunmet = cfunmet & customFormat < movie.MinCustomFormatScore if cfunmet: return True else: From 0ce82357898fb57b765002b0da90211d7359c747 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 21 Sep 2024 15:08:30 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- qBitrr/arss.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qBitrr/arss.py b/qBitrr/arss.py index 92aea869..e0085b2d 100755 --- a/qBitrr/arss.py +++ b/qBitrr/arss.py @@ -4460,10 +4460,10 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - .first() ) if episode.EpisodeFileId != 0: - cfunmet = customFormat < episode.CustomFormatScore + cfunmet = customFormat < episode.CustomFormatScore if self.force_minimum_custom_format: cfunmet = cfunmet & customFormat < episode.MinCustomFormatScore - + if cfunmet: return True else: From c117b2d063947941ed85a8f686135f746df4e916 Mon Sep 17 00:00:00 2001 From: Maxim Myalin Date: Sat, 21 Sep 2024 15:30:49 +0000 Subject: [PATCH 3/4] better --- qBitrr/arss.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/qBitrr/arss.py b/qBitrr/arss.py index e0085b2d..f7ea2c17 100755 --- a/qBitrr/arss.py +++ b/qBitrr/arss.py @@ -4435,6 +4435,9 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - completed = True if len(queue["records"]) > 0: if self.type == "sonarr": + # assuming by default it's not unmet to prevent early reseraching and deleeting + cfunmet = False + if not self.series_search: entry = next( ( @@ -4444,8 +4447,10 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - ), None, ) + if not entry: return False + customFormat = next( ( record["customFormatScore"] @@ -4454,20 +4459,19 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - ), None, ) + episode = ( self.model_file.select() .where(self.model_file.EntryId == entry) .first() ) + if episode.EpisodeFileId != 0: cfunmet = customFormat < episode.CustomFormatScore if self.force_minimum_custom_format: cfunmet = cfunmet & customFormat < episode.MinCustomFormatScore - if cfunmet: - return True - else: - return False + return cfunmet else: entry = next( ( @@ -4477,8 +4481,10 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - ), None, ) + if not entry: return False + customFormat = next( ( record["customFormatScore"] @@ -4487,19 +4493,17 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - ), None, ) + series = ( self.model_file.select() .where(self.model_file.EntryId == entry) .first() ) + if self.force_minimum_custom_format: cfunmet = customFormat < series.MinCustomFormatScore - else: - return True - if cfunmet: - return True - else: - return False + + return cfunmet elif self.type == "radarr": entry = next( ( @@ -4509,8 +4513,10 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - ), None, ) + if not entry: return False + customFormat = next( ( record["customFormatScore"] @@ -4522,14 +4528,16 @@ def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) - movie = ( self.model_file.select().where(self.model_file.EntryId == entry).first() ) + + # assuming by default it's not unmet to prevent early reseraching and deleeting + cfunmet = False + if movie.MovieFileId != 0: cfunmet = customFormat < movie.CustomFormatScore if self.force_minimum_custom_format: cfunmet = cfunmet & customFormat < movie.MinCustomFormatScore - if cfunmet: - return True - else: - return False + + return cfunmet except KeyError: pass From 23362a88b4e79fb500574b85956434c14ecef0fa Mon Sep 17 00:00:00 2001 From: Maxim Myalin Date: Sat, 21 Sep 2024 15:32:02 +0000 Subject: [PATCH 4/4] better --- qBitrr/arss.py | 152 +++++++++++++++++-------------------------------- 1 file changed, 51 insertions(+), 101 deletions(-) diff --git a/qBitrr/arss.py b/qBitrr/arss.py index f7ea2c17..3596d9f7 100755 --- a/qBitrr/arss.py +++ b/qBitrr/arss.py @@ -4421,125 +4421,75 @@ def _process_single_torrent(self, torrent: qbittorrentapi.TorrentDictionary): def custom_format_unmet_check(self, torrent: qbittorrentapi.TorrentDictionary) -> bool: try: - completed = True - while completed: - completed = False + # Retry getting the queue until it succeeds + while True: try: queue = self.client.get_queue() + break except ( requests.exceptions.ChunkedEncodingError, requests.exceptions.ContentDecodingError, requests.exceptions.ConnectionError, JSONDecodeError, - ) as e: - completed = True - if len(queue["records"]) > 0: - if self.type == "sonarr": - # assuming by default it's not unmet to prevent early reseraching and deleeting - cfunmet = False - - if not self.series_search: - entry = next( - ( - record["episodeId"] - for record in queue["records"] - if record["downloadId"] == torrent.hash.upper() - ), - None, - ) - - if not entry: - return False - - customFormat = next( - ( - record["customFormatScore"] - for record in queue["records"] - if record["downloadId"] == torrent.hash.upper() - ), - None, - ) - - episode = ( - self.model_file.select() - .where(self.model_file.EntryId == entry) - .first() - ) + ): + continue # Retry on exceptions - if episode.EpisodeFileId != 0: - cfunmet = customFormat < episode.CustomFormatScore - if self.force_minimum_custom_format: - cfunmet = cfunmet & customFormat < episode.MinCustomFormatScore + if not queue.get("records"): + return False - return cfunmet - else: - entry = next( - ( - record["seriesId"] - for record in queue["records"] - if record["downloadId"] == torrent.hash.upper() - ), - None, - ) + download_id = torrent.hash.upper() + record = next( + (r for r in queue["records"] if r.get("downloadId") == download_id), + None, + ) - if not entry: - return False + if not record: + return False - customFormat = next( - ( - record["customFormatScore"] - for record in queue["records"] - if record["downloadId"] == torrent.hash.upper() - ), - None, - ) + custom_format_score = record.get("customFormatScore") + if custom_format_score is None: + return False - series = ( - self.model_file.select() - .where(self.model_file.EntryId == entry) - .first() - ) + # Default assumption: custom format requirements are met + cf_unmet = False - if self.force_minimum_custom_format: - cfunmet = customFormat < series.MinCustomFormatScore - - return cfunmet - elif self.type == "radarr": - entry = next( - ( - record["movieId"] - for record in queue["records"] - if record["downloadId"] == torrent.hash.upper() - ), - None, - ) + if self.type == "sonarr": + entry_id_field = "seriesId" if self.series_search else "episodeId" + file_id_field = None if self.series_search else "EpisodeFileId" + elif self.type == "radarr": + entry_id_field = "movieId" + file_id_field = "MovieFileId" + else: + return False # Unknown type - if not entry: - return False + entry_id = record.get(entry_id_field) + if not entry_id: + return False - customFormat = next( - ( - record["customFormatScore"] - for record in queue["records"] - if record["downloadId"] == torrent.hash.upper() - ), - None, - ) - movie = ( - self.model_file.select().where(self.model_file.EntryId == entry).first() - ) + # Retrieve the model entry from the database + model_entry = ( + self.model_file.select().where(self.model_file.EntryId == entry_id).first() + ) + if not model_entry: + return False - # assuming by default it's not unmet to prevent early reseraching and deleeting - cfunmet = False + if self.type == "sonarr" and self.series_search: + if self.force_minimum_custom_format: + min_score = getattr(model_entry, "MinCustomFormatScore", 0) + cf_unmet = custom_format_score < min_score + else: + file_id = getattr(model_entry, file_id_field, 0) if file_id_field else 0 + if file_id != 0: + model_cf_score = getattr(model_entry, "CustomFormatScore", 0) + cf_unmet = custom_format_score < model_cf_score + if self.force_minimum_custom_format: + min_score = getattr(model_entry, "MinCustomFormatScore", 0) + cf_unmet = cf_unmet and custom_format_score < min_score - if movie.MovieFileId != 0: - cfunmet = customFormat < movie.CustomFormatScore - if self.force_minimum_custom_format: - cfunmet = cfunmet & customFormat < movie.MinCustomFormatScore + return cf_unmet - return cfunmet - except KeyError: - pass + except Exception: + return False def torrent_limit_check( self, torrent: qbittorrentapi.TorrentDictionary, seeding_time_limit, ratio_limit