From 907befedfeb57de3fa04ac73a87ccca135505979 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 13:41:16 -0500 Subject: [PATCH 1/9] Add type-hints for `PyMedusaApiV1.api_call` --- core/auto_process/managers/pymedusa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index f166c730..640875e2 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -35,7 +35,7 @@ class PyMedusaApiV1(SickBeard): route, ) - def api_call(self): + def api_call(self) -> ProcessResult: self._process_fork_prarams() logger.debug(f'Opening URL: {self.url} with params: {self.sb_init.fork_params}', self.sb_init.section) try: From 912242376209fc50840c2e8a0937821f35dd8c18 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 13:39:08 -0500 Subject: [PATCH 2/9] Add type-hints for `PyMedusaApiV2.api_call` --- core/auto_process/managers/pymedusa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index 640875e2..2706f95a 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -100,7 +100,7 @@ class PyMedusaApiV2(SickBeard): return jdata - def api_call(self): + def api_call(self) -> ProcessResult: self._process_fork_prarams() logger.debug(f'Opening URL: {self.url}', self.sb_init.section) payload = self.sb_init.fork_params From f9845359e4a03845222d46a4bad2213e0d2382d5 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 12:32:44 -0500 Subject: [PATCH 3/9] Fix raising Exception directly. Exception is too generic. Raise a ValueError instead of Exception. --- core/auto_process/managers/pymedusa.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index 2706f95a..83481ceb 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -71,9 +71,14 @@ class PyMedusaApiV2(SickBeard): def __init__(self, sb_init): super().__init__(sb_init) - # Check for an apikey, as this is required with using fork = medusa-apiv2 + # Check for an apikey + # This is required with using fork = medusa-apiv2 if not sb_init.apikey: - raise Exception('For the section SickBeard `fork = medusa-apiv2` you also need to configure an `apikey`') + logger.error( + 'For the section SickBeard `fork = medusa-apiv2` you also ' + 'need to configure an `apikey`' + ) + raise ValueError('Missing apikey for fork: medusa-apiv2') @property def url(self): From 5b83d6c22ed7fc04ec48225d253fa0ad31f2e61a Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 12:43:07 -0500 Subject: [PATCH 4/9] Use f-strings instead of `.format` Use f-strings in Python 3 and clean up line-lengths. --- core/auto_process/managers/pymedusa.py | 59 +++++++++++++++++++------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index 83481ceb..85e828a7 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -43,24 +43,37 @@ class PyMedusaApiV1(SickBeard): except requests.ConnectionError: logger.error(f'Unable to open URL: {self.url}', self.sb_init.section) return ProcessResult( - message='{0}: Failed to post-process - Unable to connect to {0}'.format(self.sb_init.section), + message=f'{self.sb_init.section}: Failed to post-process - ' + f'Unable to connect to {self.sb_init.section}', status_code=1, ) - if response.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]: - logger.error('Server returned status {0}'.format(response.status_code), self.sb_init.section) + successful_status_codes = [ + requests.codes.ok, + requests.codes.created, + requests.codes.accepted, + ] + if response.status_code not in successful_status_codes: + logger.error( + f'Server returned status {response.status_code}', + self.sb_init.section, + ) return ProcessResult( - message='{0}: Failed to post-process - Server returned status {1}'.format(self.sb_init.section, response.status_code), + message=f'{self.sb_init.section}: Failed to post-process - ' + f'Server returned status {response.status_code}', status_code=1, ) if response.json()['result'] == 'success': return ProcessResult( - message='{0}: Successfully post-processed {1}'.format(self.sb_init.section, self.input_name), + message=f'{self.sb_init.section}: ' + f'Successfully post-processed {self.input_name}', status_code=0, ) return ProcessResult( - message='{0}: Failed to post-process - Returned log from {0} was not as expected.'.format(self.sb_init.section), + message=f'{self.sb_init.section}: Failed to post-process - ' + f'Returned log from {self.sb_init.section} was not as ' + f'expected.', status_code=1, # We did not receive Success confirmation. ) @@ -95,7 +108,10 @@ class PyMedusaApiV2(SickBeard): try: response = self.session.get(url, verify=False, timeout=(30, 1800)) except requests.ConnectionError: - logger.error('Unable to get postprocess identifier status', self.sb_init.section) + logger.error( + 'Unable to get postprocess identifier status', + self.sb_init.section, + ) return False try: @@ -120,11 +136,20 @@ class PyMedusaApiV2(SickBeard): # Send postprocess request try: - response = self.session.post(self.url, json=payload, verify=False, timeout=(30, 1800)) + response = self.session.post( + self.url, + json=payload, + verify=False, + timeout=(30, 1800), + ) except requests.ConnectionError: - logger.error('Unable to send postprocess request', self.sb_init.section) + logger.error( + 'Unable to send postprocess request', + self.sb_init.section, + ) return ProcessResult( - message='{0}: Unable to send postprocess request to PyMedusa', + message=f'{self.sb_init.section}: Unable to send postprocess ' + f'request to PyMedusa', status_code=1, ) @@ -157,16 +182,20 @@ class PyMedusaApiV2(SickBeard): # Log Medusa's PP logs here. if response.get('output'): for line in response['output']: - logger.postprocess('{0}'.format(line), self.sb_init.section) + logger.postprocess(line, self.sb_init.section) - # For now this will most likely always be True. But in the future we could return an exit state - # for when the PP in medusa didn't yield an expected result. + # For now this will most likely always be True. + # In the future we could return an exit state for when the PP in + # medusa didn't yield an expected result. if response.get('success'): return ProcessResult( - message='{0}: Successfully post-processed {1}'.format(self.sb_init.section, self.input_name), + message=f'{self.sb_init.section}: ' + f'Successfully post-processed {self.input_name}', status_code=0, ) return ProcessResult( - message='{0}: Failed to post-process - Returned log from {0} was not as expected.'.format(self.sb_init.section), + message=f'{self.sb_init.section}: Failed to post-process - ' + f'Returned log from {self.sb_init.section} was not ' + f'as expected.', status_code=1, # We did not receive Success confirmation. ) From b494da02bb7c4c95e3a975a42827f4a8a854a6da Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 12:57:18 -0500 Subject: [PATCH 5/9] Fix jdata may be referenced before assignment --- core/auto_process/managers/pymedusa.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index 85e828a7..6635b54d 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -160,8 +160,11 @@ class PyMedusaApiV2(SickBeard): except ValueError: logger.debug('No data returned from provider') return False + else: + jdata = {} - if not jdata.get('status') or not jdata['status'] == 'success': + status = jdata.get('status', None) + if status != 'success': return False queueitem_identifier = jdata['queueItem']['identifier'] From 2b049f6b20eff178fb6bc9c5d2d0c242edd51527 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 13:03:26 -0500 Subject: [PATCH 6/9] Move `queue_item_identifier` closer to point of use. --- core/auto_process/managers/pymedusa.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index 6635b54d..400f4cfa 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -37,9 +37,19 @@ class PyMedusaApiV1(SickBeard): def api_call(self) -> ProcessResult: self._process_fork_prarams() - logger.debug(f'Opening URL: {self.url} with params: {self.sb_init.fork_params}', self.sb_init.section) + logger.debug( + f'Opening URL: {self.url} with params: {self.sb_init.fork_params}', + self.sb_init.section, + ) try: - response = self.session.get(self.url, auth=(self.sb_init.username, self.sb_init.password), params=self.sb_init.fork_params, stream=True, verify=False, timeout=(30, 1800)) + response = self.session.get( + self.url, + auth=(self.sb_init.username, self.sb_init.password), + params=self.sb_init.fork_params, + stream=True, + verify=False, + timeout=(30, 1800), + ) except requests.ConnectionError: logger.error(f'Unable to open URL: {self.url}', self.sb_init.section) return ProcessResult( @@ -167,12 +177,12 @@ class PyMedusaApiV2(SickBeard): if status != 'success': return False - queueitem_identifier = jdata['queueItem']['identifier'] - wait_for = int(self.sb_init.config.get('wait_for', 2)) n = 0 response = {} - url = f'{self.url}/{queueitem_identifier}' + + queue_item_identifier = jdata['queueItem']['identifier'] + url = f'{self.url}/{queue_item_identifier}' while n < 12: # set up wait_for minutes to see if command completes.. time.sleep(5 * wait_for) response = self._get_identifier_status(url) From dc37deb6e2db86ddd4eb5244d715813791ed98ff Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 13:38:05 -0500 Subject: [PATCH 7/9] Reduce number of return paths. --- core/auto_process/managers/pymedusa.py | 37 ++++++++++++++------------ 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index 400f4cfa..e37a37ee 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -68,24 +68,25 @@ class PyMedusaApiV1(SickBeard): f'Server returned status {response.status_code}', self.sb_init.section, ) - return ProcessResult( + result = ProcessResult( message=f'{self.sb_init.section}: Failed to post-process - ' f'Server returned status {response.status_code}', status_code=1, ) - - if response.json()['result'] == 'success': - return ProcessResult( + elif response.json()['result'] == 'success': + result = ProcessResult( message=f'{self.sb_init.section}: ' f'Successfully post-processed {self.input_name}', status_code=0, ) - return ProcessResult( - message=f'{self.sb_init.section}: Failed to post-process - ' - f'Returned log from {self.sb_init.section} was not as ' - f'expected.', - status_code=1, # We did not receive Success confirmation. - ) + else: + result = ProcessResult( + message=f'{self.sb_init.section}: Failed to post-process - ' + f'Returned log from {self.sb_init.section} was not as ' + f'expected.', + status_code=1, # We did not receive Success confirmation. + ) + return result class PyMedusaApiV2(SickBeard): @@ -201,14 +202,16 @@ class PyMedusaApiV2(SickBeard): # In the future we could return an exit state for when the PP in # medusa didn't yield an expected result. if response.get('success'): - return ProcessResult( + result = ProcessResult( message=f'{self.sb_init.section}: ' f'Successfully post-processed {self.input_name}', status_code=0, ) - return ProcessResult( - message=f'{self.sb_init.section}: Failed to post-process - ' - f'Returned log from {self.sb_init.section} was not ' - f'as expected.', - status_code=1, # We did not receive Success confirmation. - ) + else: + result = ProcessResult( + message=f'{self.sb_init.section}: Failed to post-process - ' + f'Returned log from {self.sb_init.section} was not ' + f'as expected.', + status_code=1, # We did not receive Success confirmation. + ) + return result From 109e4dabcd129d458ce2562c2ac74245b2d4bd49 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 17:35:29 -0500 Subject: [PATCH 8/9] Fix invalid return types and use `ProcessResult.failure` or `ProcessResult.success` --- core/auto_process/managers/pymedusa.py | 57 +++++++++++--------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index e37a37ee..986ffdd2 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -52,10 +52,9 @@ class PyMedusaApiV1(SickBeard): ) except requests.ConnectionError: logger.error(f'Unable to open URL: {self.url}', self.sb_init.section) - return ProcessResult( - message=f'{self.sb_init.section}: Failed to post-process - ' - f'Unable to connect to {self.sb_init.section}', - status_code=1, + return ProcessResult.failure( + f'{self.sb_init.section}: Failed to post-process - Unable to ' + f'connect to {self.sb_init.section}' ) successful_status_codes = [ @@ -68,23 +67,20 @@ class PyMedusaApiV1(SickBeard): f'Server returned status {response.status_code}', self.sb_init.section, ) - result = ProcessResult( - message=f'{self.sb_init.section}: Failed to post-process - ' - f'Server returned status {response.status_code}', - status_code=1, + result = ProcessResult.failure( + f'{self.sb_init.section}: Failed to post-process - Server ' + f'returned status {response.status_code}' ) elif response.json()['result'] == 'success': - result = ProcessResult( - message=f'{self.sb_init.section}: ' - f'Successfully post-processed {self.input_name}', - status_code=0, + result = ProcessResult.success( + f'{self.sb_init.section}: Successfully post-processed ' + f'{self.input_name}' ) else: - result = ProcessResult( - message=f'{self.sb_init.section}: Failed to post-process - ' - f'Returned log from {self.sb_init.section} was not as ' - f'expected.', - status_code=1, # We did not receive Success confirmation. + # We did not receive Success confirmation. + result = ProcessResult.failure( + f'{self.sb_init.section}: Failed to post-process - Returned ' + f'log from {self.sb_init.section} was not as expected.' ) return result @@ -158,10 +154,9 @@ class PyMedusaApiV2(SickBeard): 'Unable to send postprocess request', self.sb_init.section, ) - return ProcessResult( - message=f'{self.sb_init.section}: Unable to send postprocess ' - f'request to PyMedusa', - status_code=1, + return ProcessResult.failure( + f'{self.sb_init.section}: Unable to send postprocess request ' + f'to PyMedusa' ) # Get UUID @@ -170,13 +165,13 @@ class PyMedusaApiV2(SickBeard): jdata = response.json() except ValueError: logger.debug('No data returned from provider') - return False + return ProcessResult.failure('No data returned from provider') else: jdata = {} status = jdata.get('status', None) if status != 'success': - return False + return ProcessResult.failure() wait_for = int(self.sb_init.config.get('wait_for', 2)) n = 0 @@ -202,16 +197,14 @@ class PyMedusaApiV2(SickBeard): # In the future we could return an exit state for when the PP in # medusa didn't yield an expected result. if response.get('success'): - result = ProcessResult( - message=f'{self.sb_init.section}: ' - f'Successfully post-processed {self.input_name}', - status_code=0, + result = ProcessResult.success( + f'{self.sb_init.section}: Successfully post-processed ' + f'{self.input_name}' ) else: - result = ProcessResult( - message=f'{self.sb_init.section}: Failed to post-process - ' - f'Returned log from {self.sb_init.section} was not ' - f'as expected.', - status_code=1, # We did not receive Success confirmation. + # We did not receive Success confirmation. + result = ProcessResult.failure( + f'{self.sb_init.section}: Failed to post-process - Returned ' + f'log from {self.sb_init.section} was not as expected.' ) return result From 46af216e3d620c0608c4c62f128bb64183adf997 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 3 Dec 2022 18:40:19 -0500 Subject: [PATCH 9/9] Blacken code --- core/auto_process/managers/pymedusa.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/auto_process/managers/pymedusa.py b/core/auto_process/managers/pymedusa.py index 986ffdd2..2e5f76f4 100644 --- a/core/auto_process/managers/pymedusa.py +++ b/core/auto_process/managers/pymedusa.py @@ -51,7 +51,10 @@ class PyMedusaApiV1(SickBeard): timeout=(30, 1800), ) except requests.ConnectionError: - logger.error(f'Unable to open URL: {self.url}', self.sb_init.section) + logger.error( + f'Unable to open URL: {self.url}', + self.sb_init.section, + ) return ProcessResult.failure( f'{self.sb_init.section}: Failed to post-process - Unable to ' f'connect to {self.sb_init.section}' @@ -136,10 +139,11 @@ class PyMedusaApiV2(SickBeard): del payload['nzbName'] # Update the session with the x-api-key - self.session.headers.update({ + headers = { 'x-api-key': self.sb_init.apikey, - 'Content-type': 'application/json' - }) + 'Content-type': 'application/json', + } + self.session.headers.update(headers) # Send postprocess request try: