From 72b195009cee2f93b03e56467f2fc9b6a668bed4 Mon Sep 17 00:00:00 2001 From: salty Date: Sat, 19 Feb 2022 16:00:43 +0100 Subject: [PATCH] minor code cleanup --- media/pvr.py | 8 +++++--- media/trakt.py | 5 ++--- misc/config.py | 15 +++++++-------- notifications/pushover.py | 2 +- notifications/slack.py | 2 +- traktarr.py | 17 ++++++++--------- 6 files changed, 24 insertions(+), 25 deletions(-) diff --git a/media/pvr.py b/media/pvr.py index e0ef81e..7d71a68 100644 --- a/media/pvr.py +++ b/media/pvr.py @@ -177,9 +177,11 @@ class PVR(ABC): if 'json' in req.headers['Content-Type'].lower(): response_json = misc.get_response_dict(req.json(), identifier_field, identifier) - if (req.status_code == 201 or req.status_code == 200) \ - and (response_json and identifier_field in response_json) \ - and response_json[identifier_field] == identifier: + if ( + req.status_code in [201, 200] + and (response_json and identifier_field in response_json) + and response_json[identifier_field] == identifier + ): log.debug("Successfully added: \'%s [%d]\'", payload['title'], identifier) return True elif response_json and ('errorMessage' in response_json or 'message' in response_json): diff --git a/media/trakt.py b/media/trakt.py index c8cf7e7..b0e41ea 100644 --- a/media/trakt.py +++ b/media/trakt.py @@ -66,8 +66,7 @@ class Trakt: req, resp_data = self._make_request(url, payload) if req.status_code == 200 and len(resp_data): - resp_json = json.loads(resp_data) - return resp_json + return json.loads(resp_data) elif req.status_code == 401: log.error("The authentication to Trakt is revoked. Please re-authenticate.") exit() @@ -133,9 +132,9 @@ class Trakt: try: resp_data = '' + max_attempts = 6 while True: attempts = 0 - max_attempts = 6 retrieve_error = False while attempts <= max_attempts: try: diff --git a/misc/config.py b/misc/config.py index 9b5a111..dc54796 100644 --- a/misc/config.py +++ b/misc/config.py @@ -147,13 +147,12 @@ class Config(object, metaclass=Singleton): return self.log_path def build_config(self): - if not os.path.exists(self.config_path): - print("Dumping default config to: %s" % self.config_path) - with open(self.config_path, 'w') as fp: - json.dump(self.base_config, fp, sort_keys=True, indent=2) - return True - else: + if os.path.exists(self.config_path): return False + print("Dumping default config to: %s" % self.config_path) + with open(self.config_path, 'w') as fp: + json.dump(self.base_config, fp, sort_keys=True, indent=2) + return True def dump_config(self): if os.path.exists(self.config_path): @@ -184,10 +183,10 @@ class Config(object, metaclass=Singleton): continue # iterate children - if isinstance(v, dict) or isinstance(v, list): + if isinstance(v, (dict, list)): merged[k], did_upgrade = self.__inner_upgrade(settings1[k], settings2[k], key=k, overwrite=overwrite) - sub_upgraded = did_upgrade if did_upgrade else sub_upgraded + sub_upgraded = did_upgrade or sub_upgraded elif settings1[k] != settings2[k] and overwrite: merged = settings1 sub_upgraded = True diff --git a/notifications/pushover.py b/notifications/pushover.py index 35f8ddd..beee86a 100644 --- a/notifications/pushover.py +++ b/notifications/pushover.py @@ -28,7 +28,7 @@ class Pushover: 'priority': self.priority, } resp = requests.post('https://api.pushover.net/1/messages.json', data=payload, timeout=30) - return True if resp.status_code == 200 else False + return resp.status_code == 200 except Exception: log.exception("Error sending notification to %r", self.user_token) diff --git a/notifications/slack.py b/notifications/slack.py index 839a223..e8ed2f7 100644 --- a/notifications/slack.py +++ b/notifications/slack.py @@ -31,7 +31,7 @@ class Slack: payload['channel'] = self.channel resp = requests.post(self.webhook_url, json=payload, timeout=30) - return True if resp.status_code == 200 else False + return resp.status_code == 200 except Exception: log.exception("Error sending notification to %r", self.webhook_url) diff --git a/traktarr.py b/traktarr.py index 47b82fe..1f69cce 100755 --- a/traktarr.py +++ b/traktarr.py @@ -111,7 +111,7 @@ def validate_pvr(pvr, pvr_type, notifications): def get_quality_profile_id(pvr, quality_profile): # retrieve profile id for requested quality profile quality_profile_id = pvr.get_quality_profile_id(quality_profile) - if not quality_profile_id or not quality_profile_id > 0: + if not quality_profile_id or quality_profile_id <= 0: log.error("Aborting due to failure to retrieve Quality Profile ID for: %s", quality_profile) exit() log.info("Retrieved Quality Profile ID for \'%s\': %d", quality_profile, quality_profile_id) @@ -121,7 +121,7 @@ def get_quality_profile_id(pvr, quality_profile): def get_language_profile_id(pvr, language_profile): # retrieve profile id for requested language profile language_profile_id = pvr.get_language_profile_id(language_profile) - if not language_profile_id or not language_profile_id > 0: + if not language_profile_id or language_profile_id <= 0: log.error("No Language Profile ID for: %s", language_profile) else: log.info("Retrieved Language Profile ID for \'%s\': %d", language_profile, language_profile_id) @@ -267,12 +267,11 @@ def show( tag_names) else: log.info("ADDED: \'%s (%s)\'", series_title, series_year) + elif profile_tags is not None: + log.error("FAILED ADDING: \'%s (%s)\' with Sonarr Tags: %s", series_title, series_year, + tag_names) else: - if profile_tags is not None: - log.error("FAILED ADDING: \'%s (%s)\' with Sonarr Tags: %s", series_title, series_year, - tag_names) - else: - log.info("FAILED ADDING: \'%s (%s)\'", series_title, series_year) + log.info("FAILED ADDING: \'%s (%s)\'", series_title, series_year) return @@ -1399,7 +1398,7 @@ def automatic_shows( ) if added_shows is None: - if not list_type.lower() == 'lists': + if list_type.lower() != 'lists': log.info("FAILED ADDING shows from Trakt's \'%s\' list.", list_type) time.sleep(10) continue @@ -1535,7 +1534,7 @@ def automatic_movies( ) if added_movies is None: - if not list_type.lower() == 'lists': + if list_type.lower() != 'lists': log.info("FAILED ADDING movies from Trakt's \'%s\' list.", list_type.capitalize()) time.sleep(10) continue