From 607d2a8f6fe2b93f03a8602880d43538f15eba9d Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Fri, 12 Feb 2021 16:36:09 -0600 Subject: [PATCH] Implement hybrid quality definition logic The hybrid quality is auto-generated and takes values between the anime and non-anime tables in the following way: * The lowest minimum and highest maximum between the non-anime and anime tables are used as the min/max for each quality in the generated hybrid table. * Only 720/1080 qualities are "combined". Qualities outside of this, such as 2160, use the non-anime min/max values. --- src/app/api/sonarr.py | 4 ++-- src/app/guide/utils.py | 7 ++++--- src/trash.py | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/app/api/sonarr.py b/src/app/api/sonarr.py index f41c00f9..e921ae1e 100644 --- a/src/app/api/sonarr.py +++ b/src/app/api/sonarr.py @@ -132,8 +132,8 @@ class Sonarr(Server): self.request('put', '/qualityDefinition/update', new_definition) # -------------------------------------------------------------------------------------------------- - def find_quality_definition_entry(self, new_definition, quality): - for entry in new_definition: + def find_quality_definition_entry(self, definition, quality): + for entry in definition: if entry.get('quality').get('name') == quality: return entry diff --git a/src/app/guide/utils.py b/src/app/guide/utils.py index 04404ad9..4028e8b2 100644 --- a/src/app/guide/utils.py +++ b/src/app/guide/utils.py @@ -44,10 +44,11 @@ def find_existing_profile(profile_name, existing_profiles): return None # -------------------------------------------------------------------------------------------------- -def quality_preview(name, definition): - print(name) - formats = ' {:<20} {:<10} {:<10}' +def quality_preview(definition): + print('') + formats = '{:<20} {:<10} {:<10}' print(formats.format('Quality', 'Min', 'Max')) + print(formats.format('-------', '---', '---')) for (quality, min, max) in definition: print(formats.format(quality, min, max)) print('') diff --git a/src/trash.py b/src/trash.py index 706dbde6..14b9b7be 100644 --- a/src/trash.py +++ b/src/trash.py @@ -1,4 +1,5 @@ import requests +import re from app import guide from app.api.sonarr import Sonarr @@ -43,22 +44,47 @@ def process_sonarr_profile(args, logger): profile_to_update = guide.utils.find_existing_profile(new_profile_name, existing_profiles) if profile_to_update: - print(f'Updating existing profile: {new_profile_name}') + logger.info(f'Updating existing profile: {new_profile_name}') sonarr.update_existing_profile(profile_to_update, profile, tag_ids) else: - print(f'Creating new profile: {new_profile_name}') + logger.info(f'Creating new profile: {new_profile_name}') sonarr.create_release_profile(new_profile_name, profile, tag_ids) def process_sonarr_quality(args, logger): guide_definitions = quality.parse_markdown(logger, quality.get_markdown()) if args.type == 'sonarr:hybrid': - raise ValueError('Hybrid profile not implemented yet') + hybrid_quality_regex = re.compile(r'720|1080') + anime = guide_definitions.get('sonarr:anime') + nonanime = guide_definitions.get('sonarr:non-anime') + if len(anime) != len(nonanime): + raise RuntimeError('For some reason the anime and non-anime quality definitions are not the same length') + + logger.info('Notice: Hybrid only functions on 720/1080 qualities and uses non-anime values for the rest (e.g. 2160)') + + hybrid = [] + for i in range(len(nonanime)): + left = nonanime[i] + if not hybrid_quality_regex.search(left[0]): + logger.debug('Ignored Quality: ' + left[0]) + hybrid.append(left) + else: + right = None + for r in anime: + if r[0] == left[0]: + right = r + + if right is None: + raise RuntimeError(f'Could not find matching anime quality for non-anime quality named: {left[0]}') + + hybrid.append((left[0], min(left[1], right[1]), max(left[2], right[2]))) + + guide_definitions['sonarr:hybrid'] = hybrid selected_definition = guide_definitions.get(args.type) if args.preview: - utils.quality_preview(args.type, selected_definition) + utils.quality_preview(selected_definition) exit(0) print(f'Updating quality definition using {args.type}')