From 7a3c2bc8a5506cbc703efaca2b670b9d8bab79d4 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Thu, 24 Nov 2022 22:47:10 -0500 Subject: [PATCH 01/24] Refactor `nzbToMedia.process` -> `core.processor.nzb.process` --- core/processor/__init__.py | 0 core/processor/nzb.py | 154 +++++++++++++++++++++++++++++++++++++ nzbToMedia.py | 147 +---------------------------------- 3 files changed, 158 insertions(+), 143 deletions(-) create mode 100644 core/processor/__init__.py create mode 100644 core/processor/nzb.py diff --git a/core/processor/__init__.py b/core/processor/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/core/processor/nzb.py b/core/processor/nzb.py new file mode 100644 index 00000000..a649654e --- /dev/null +++ b/core/processor/nzb.py @@ -0,0 +1,154 @@ +import datetime + +import core +from core import logger, main_db +from core.auto_process import comics, games, movies, music, tv, books +from core.auto_process.common import ProcessResult +from core.plugins.downloaders.nzb.utils import get_nzoid +from core.plugins.plex import plex_update +from core.user_scripts import external_script +from core.utils import ( + char_replace, + clean_dir, + convert_to_ascii, + extract_files, + update_download_info_status, +) + +try: + text_type = unicode +except NameError: + text_type = str + + +def process(input_directory, input_name=None, status=0, client_agent='manual', download_id=None, input_category=None, failure_link=None): + if core.SAFE_MODE and input_directory == core.NZB_DEFAULT_DIRECTORY: + logger.error( + 'The input directory:[{0}] is the Default Download Directory. Please configure category directories to prevent processing of other media.'.format( + input_directory)) + return ProcessResult( + message='', + status_code=-1, + ) + + if not download_id and client_agent == 'sabnzbd': + download_id = get_nzoid(input_name) + + if client_agent != 'manual' and not core.DOWNLOAD_INFO: + logger.debug('Adding NZB download info for directory {0} to database'.format(input_directory)) + + my_db = main_db.DBConnection() + + input_directory1 = input_directory + input_name1 = input_name + + try: + encoded, input_directory1 = char_replace(input_directory) + encoded, input_name1 = char_replace(input_name) + except Exception: + pass + + control_value_dict = {'input_directory': text_type(input_directory1)} + new_value_dict = { + 'input_name': text_type(input_name1), + 'input_hash': text_type(download_id), + 'input_id': text_type(download_id), + 'client_agent': text_type(client_agent), + 'status': 0, + 'last_update': datetime.date.today().toordinal(), + } + my_db.upsert('downloads', new_value_dict, control_value_dict) + + # auto-detect section + if input_category is None: + input_category = 'UNCAT' + usercat = input_category + section = core.CFG.findsection(input_category).isenabled() + if section is None: + section = core.CFG.findsection('ALL').isenabled() + if section is None: + logger.error( + 'Category:[{0}] is not defined or is not enabled. Please rename it or ensure it is enabled for the appropriate section in your autoProcessMedia.cfg and try again.'.format( + input_category)) + return ProcessResult( + message='', + status_code=-1, + ) + else: + usercat = 'ALL' + + if len(section) > 1: + logger.error( + 'Category:[{0}] is not unique, {1} are using it. Please rename it or disable all other sections using the same category name in your autoProcessMedia.cfg and try again.'.format( + input_category, section.keys())) + return ProcessResult( + message='', + status_code=-1, + ) + + if section: + section_name = section.keys()[0] + logger.info('Auto-detected SECTION:{0}'.format(section_name)) + else: + logger.error('Unable to locate a section with subsection:{0} enabled in your autoProcessMedia.cfg, exiting!'.format( + input_category)) + return ProcessResult( + status_code=-1, + message='', + ) + + cfg = dict(core.CFG[section_name][usercat]) + + extract = int(cfg.get('extract', 0)) + + try: + if int(cfg.get('remote_path')) and not core.REMOTE_PATHS: + logger.error('Remote Path is enabled for {0}:{1} but no Network mount points are defined. Please check your autoProcessMedia.cfg, exiting!'.format( + section_name, input_category)) + return ProcessResult( + status_code=-1, + message='', + ) + except Exception: + logger.error('Remote Path {0} is not valid for {1}:{2} Please set this to either 0 to disable or 1 to enable!'.format( + cfg.get('remote_path'), section_name, input_category)) + + input_name, input_directory = convert_to_ascii(input_name, input_directory) + + if extract == 1 and not (status > 0 and core.NOEXTRACTFAILED): + logger.debug('Checking for archives to extract in directory: {0}'.format(input_directory)) + extract_files(input_directory) + + logger.info('Calling {0}:{1} to post-process:{2}'.format(section_name, input_category, input_name)) + + if section_name in ['CouchPotato', 'Radarr', 'Watcher3']: + result = movies.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link) + elif section_name in ['SickBeard', 'SiCKRAGE', 'NzbDrone', 'Sonarr']: + result = tv.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link) + elif section_name in ['HeadPhones', 'Lidarr']: + result = music.process(section_name, input_directory, input_name, status, client_agent, input_category) + elif section_name == 'Mylar': + result = comics.process(section_name, input_directory, input_name, status, client_agent, input_category) + elif section_name == 'Gamez': + result = games.process(section_name, input_directory, input_name, status, client_agent, input_category) + elif section_name == 'LazyLibrarian': + result = books.process(section_name, input_directory, input_name, status, client_agent, input_category) + elif section_name == 'UserScript': + result = external_script(input_directory, input_name, input_category, section[usercat]) + else: + result = ProcessResult( + message='', + status_code=-1, + ) + + plex_update(input_category) + + if result.status_code == 0: + if client_agent != 'manual': + # update download status in our DB + update_download_info_status(input_name, 1) + if section_name not in ['UserScript', 'NzbDrone', 'Sonarr', 'Radarr', 'Lidarr']: + # cleanup our processing folders of any misc unwanted files and empty directories + clean_dir(input_directory, section_name, input_category) + + return result diff --git a/nzbToMedia.py b/nzbToMedia.py index 240033cc..27388cd1 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -722,7 +722,6 @@ from __future__ import ( unicode_literals, ) -import datetime import os import sys @@ -733,16 +732,12 @@ eol.check() cleanup.clean(cleanup.FOLDER_STRUCTURE) import core -from core import logger, main_db -from core.auto_process import comics, games, movies, music, tv, books +from core import logger +from core.processor.nzb import process from core.auto_process.common import ProcessResult -from core.plugins.downloaders.nzb.utils import get_nzoid -from core.plugins.plex import plex_update -from core.user_scripts import external_script from core.utils import ( - char_replace, clean_dir, convert_to_ascii, - extract_files, get_dirs, get_download_info, - update_download_info_status, + get_dirs, + get_download_info, ) try: @@ -751,140 +746,6 @@ except NameError: text_type = str -# post-processing -def process(input_directory, input_name=None, status=0, client_agent='manual', download_id=None, input_category=None, failure_link=None): - if core.SAFE_MODE and input_directory == core.NZB_DEFAULT_DIRECTORY: - logger.error( - 'The input directory:[{0}] is the Default Download Directory. Please configure category directories to prevent processing of other media.'.format( - input_directory)) - return ProcessResult( - message='', - status_code=-1, - ) - - if not download_id and client_agent == 'sabnzbd': - download_id = get_nzoid(input_name) - - if client_agent != 'manual' and not core.DOWNLOAD_INFO: - logger.debug('Adding NZB download info for directory {0} to database'.format(input_directory)) - - my_db = main_db.DBConnection() - - input_directory1 = input_directory - input_name1 = input_name - - try: - encoded, input_directory1 = char_replace(input_directory) - encoded, input_name1 = char_replace(input_name) - except Exception: - pass - - control_value_dict = {'input_directory': text_type(input_directory1)} - new_value_dict = { - 'input_name': text_type(input_name1), - 'input_hash': text_type(download_id), - 'input_id': text_type(download_id), - 'client_agent': text_type(client_agent), - 'status': 0, - 'last_update': datetime.date.today().toordinal(), - } - my_db.upsert('downloads', new_value_dict, control_value_dict) - - # auto-detect section - if input_category is None: - input_category = 'UNCAT' - usercat = input_category - section = core.CFG.findsection(input_category).isenabled() - if section is None: - section = core.CFG.findsection('ALL').isenabled() - if section is None: - logger.error( - 'Category:[{0}] is not defined or is not enabled. Please rename it or ensure it is enabled for the appropriate section in your autoProcessMedia.cfg and try again.'.format( - input_category)) - return ProcessResult( - message='', - status_code=-1, - ) - else: - usercat = 'ALL' - - if len(section) > 1: - logger.error( - 'Category:[{0}] is not unique, {1} are using it. Please rename it or disable all other sections using the same category name in your autoProcessMedia.cfg and try again.'.format( - input_category, section.keys())) - return ProcessResult( - message='', - status_code=-1, - ) - - if section: - section_name = section.keys()[0] - logger.info('Auto-detected SECTION:{0}'.format(section_name)) - else: - logger.error('Unable to locate a section with subsection:{0} enabled in your autoProcessMedia.cfg, exiting!'.format( - input_category)) - return ProcessResult( - status_code=-1, - message='', - ) - - cfg = dict(core.CFG[section_name][usercat]) - - extract = int(cfg.get('extract', 0)) - - try: - if int(cfg.get('remote_path')) and not core.REMOTE_PATHS: - logger.error('Remote Path is enabled for {0}:{1} but no Network mount points are defined. Please check your autoProcessMedia.cfg, exiting!'.format( - section_name, input_category)) - return ProcessResult( - status_code=-1, - message='', - ) - except Exception: - logger.error('Remote Path {0} is not valid for {1}:{2} Please set this to either 0 to disable or 1 to enable!'.format( - cfg.get('remote_path'), section_name, input_category)) - - input_name, input_directory = convert_to_ascii(input_name, input_directory) - - if extract == 1 and not (status > 0 and core.NOEXTRACTFAILED): - logger.debug('Checking for archives to extract in directory: {0}'.format(input_directory)) - extract_files(input_directory) - - logger.info('Calling {0}:{1} to post-process:{2}'.format(section_name, input_category, input_name)) - - if section_name in ['CouchPotato', 'Radarr', 'Watcher3']: - result = movies.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link) - elif section_name in ['SickBeard', 'SiCKRAGE', 'NzbDrone', 'Sonarr']: - result = tv.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link) - elif section_name in ['HeadPhones', 'Lidarr']: - result = music.process(section_name, input_directory, input_name, status, client_agent, input_category) - elif section_name == 'Mylar': - result = comics.process(section_name, input_directory, input_name, status, client_agent, input_category) - elif section_name == 'Gamez': - result = games.process(section_name, input_directory, input_name, status, client_agent, input_category) - elif section_name == 'LazyLibrarian': - result = books.process(section_name, input_directory, input_name, status, client_agent, input_category) - elif section_name == 'UserScript': - result = external_script(input_directory, input_name, input_category, section[usercat]) - else: - result = ProcessResult( - message='', - status_code=-1, - ) - - plex_update(input_category) - - if result.status_code == 0: - if client_agent != 'manual': - # update download status in our DB - update_download_info_status(input_name, 1) - if section_name not in ['UserScript', 'NzbDrone', 'Sonarr', 'Radarr', 'Lidarr']: - # cleanup our processing folders of any misc unwanted files and empty directories - clean_dir(input_directory, section_name, input_category) - - return result - - def main(args, section=None): # Initialize the config core.initialize(section) From 073b19034ba408cab2bbc7c3d87637251f1a89e1 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Thu, 24 Nov 2022 22:59:44 -0500 Subject: [PATCH 02/24] Extract nzbget processing from `nzbToMedia.main` -> `core.processor.nzbget` --- core/processor/nzbget.py | 83 ++++++++++++++++++++++++++++++++++++++++ nzbToMedia.py | 62 +----------------------------- 2 files changed, 85 insertions(+), 60 deletions(-) create mode 100644 core/processor/nzbget.py diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py new file mode 100644 index 00000000..1431d9e2 --- /dev/null +++ b/core/processor/nzbget.py @@ -0,0 +1,83 @@ +import os +import sys + +import core +from core import logger +from core.processor import nzb + + +def process(): + # Check if the script is called from nzbget 11.0 or later + if os.environ['NZBOP_VERSION'][0:5] < '11.0': + logger.error( + 'NZBGet Version {0} is not supported. Please update NZBGet.'.format( + os.environ['NZBOP_VERSION'])) + sys.exit(core.NZBGET_POSTPROCESS_ERROR) + + logger.info('Script triggered from NZBGet Version {0}.'.format( + os.environ['NZBOP_VERSION'])) + + status = 0 + # Check if the script is called from nzbget 13.0 or later + if 'NZBPP_TOTALSTATUS' in os.environ: + if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS': + logger.info('Download failed with status {0}.'.format( + os.environ['NZBPP_STATUS'])) + status = 1 + + else: + # Check par status + if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ[ + 'NZBPP_PARSTATUS'] == '4': + logger.warning('Par-repair failed, setting status \'failed\'') + status = 1 + + # Check unpack status + if os.environ['NZBPP_UNPACKSTATUS'] == '1': + logger.warning('Unpack failed, setting status \'failed\'') + status = 1 + + if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ[ + 'NZBPP_PARSTATUS'] == '0': + # Unpack was skipped due to nzb-file properties or due to errors during par-check + + if os.environ['NZBPP_HEALTH'] < 1000: + logger.warning( + 'Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \'failed\'') + logger.info( + 'Please check your Par-check/repair settings for future downloads.') + status = 1 + + else: + logger.info( + 'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') + logger.info( + 'Please check your Par-check/repair settings for future downloads.') + + # Check for download_id to pass to CouchPotato + download_id = '' + failure_link = None + if 'NZBPR_COUCHPOTATO' in os.environ: + download_id = os.environ['NZBPR_COUCHPOTATO'] + elif 'NZBPR_DRONE' in os.environ: + download_id = os.environ['NZBPR_DRONE'] + elif 'NZBPR_SONARR' in os.environ: + download_id = os.environ['NZBPR_SONARR'] + elif 'NZBPR_RADARR' in os.environ: + download_id = os.environ['NZBPR_RADARR'] + elif 'NZBPR_LIDARR' in os.environ: + download_id = os.environ['NZBPR_LIDARR'] + if 'NZBPR__DNZB_FAILURE' in os.environ: + failure_link = os.environ['NZBPR__DNZB_FAILURE'] + + # All checks done, now launching the script. + client_agent = 'nzbget' + return nzb.process( + os.environ['NZBPP_DIRECTORY'], + input_name=os.environ['NZBPP_NZBNAME'], + status=status, + client_agent=client_agent, + download_id=download_id, + input_category=os.environ['NZBPP_CATEGORY'], + failure_link=failure_link, + ) diff --git a/nzbToMedia.py b/nzbToMedia.py index 27388cd1..bb88ce0b 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -733,6 +733,7 @@ cleanup.clean(cleanup.FOLDER_STRUCTURE) import core from core import logger +from core.processor import nzbget from core.processor.nzb import process from core.auto_process.common import ProcessResult from core.utils import ( @@ -762,69 +763,10 @@ def main(args, section=None): message='', status_code=0, ) - status = 0 # NZBGet if 'NZBOP_SCRIPTDIR' in os.environ: - # Check if the script is called from nzbget 11.0 or later - if os.environ['NZBOP_VERSION'][0:5] < '11.0': - logger.error('NZBGet Version {0} is not supported. Please update NZBGet.'.format(os.environ['NZBOP_VERSION'])) - sys.exit(core.NZBGET_POSTPROCESS_ERROR) - - logger.info('Script triggered from NZBGet Version {0}.'.format(os.environ['NZBOP_VERSION'])) - - # Check if the script is called from nzbget 13.0 or later - if 'NZBPP_TOTALSTATUS' in os.environ: - if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS': - logger.info('Download failed with status {0}.'.format(os.environ['NZBPP_STATUS'])) - status = 1 - - else: - # Check par status - if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4': - logger.warning('Par-repair failed, setting status \'failed\'') - status = 1 - - # Check unpack status - if os.environ['NZBPP_UNPACKSTATUS'] == '1': - logger.warning('Unpack failed, setting status \'failed\'') - status = 1 - - if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ['NZBPP_PARSTATUS'] == '0': - # Unpack was skipped due to nzb-file properties or due to errors during par-check - - if os.environ['NZBPP_HEALTH'] < 1000: - logger.warning( - 'Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \'failed\'') - logger.info('Please check your Par-check/repair settings for future downloads.') - status = 1 - - else: - logger.info( - 'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') - logger.info('Please check your Par-check/repair settings for future downloads.') - - # Check for download_id to pass to CouchPotato - download_id = '' - failure_link = None - if 'NZBPR_COUCHPOTATO' in os.environ: - download_id = os.environ['NZBPR_COUCHPOTATO'] - elif 'NZBPR_DRONE' in os.environ: - download_id = os.environ['NZBPR_DRONE'] - elif 'NZBPR_SONARR' in os.environ: - download_id = os.environ['NZBPR_SONARR'] - elif 'NZBPR_RADARR' in os.environ: - download_id = os.environ['NZBPR_RADARR'] - elif 'NZBPR_LIDARR' in os.environ: - download_id = os.environ['NZBPR_LIDARR'] - if 'NZBPR__DNZB_FAILURE' in os.environ: - failure_link = os.environ['NZBPR__DNZB_FAILURE'] - - # All checks done, now launching the script. - client_agent = 'nzbget' - result = process(os.environ['NZBPP_DIRECTORY'], input_name=os.environ['NZBPP_NZBNAME'], status=status, - client_agent=client_agent, download_id=download_id, input_category=os.environ['NZBPP_CATEGORY'], - failure_link=failure_link) + result = nzbget.process() # SABnzbd elif 'SAB_SCRIPT' in os.environ: client_agent = 'sabnzbd' From 58c998712fcca1addc5deb24dddc543844b883f1 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Thu, 24 Nov 2022 23:33:17 -0500 Subject: [PATCH 03/24] Extract sabnzb processing from `nzbToMedia.main` -> `core.processor.sabnzbd` --- core/processor/sab.py | 67 +++++++++++++++++++++++++++++++++++++++++++ nzbToMedia.py | 35 +++------------------- 2 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 core/processor/sab.py diff --git a/core/processor/sab.py b/core/processor/sab.py new file mode 100644 index 00000000..3e9f4050 --- /dev/null +++ b/core/processor/sab.py @@ -0,0 +1,67 @@ +import os + +from core import logger +from core.processor import nzb + + +def process_script(): + client_agent = 'sabnzbd' + logger.info('Script triggered from SABnzbd Version {0}.'.format( + os.environ['SAB_VERSION'])) + return nzb.process( + os.environ['SAB_COMPLETE_DIR'], + input_name=os.environ['SAB_FINAL_NAME'], + status=int(os.environ['SAB_PP_STATUS']), + client_agent=client_agent, + download_id=os.environ['SAB_NZO_ID'], + input_category=os.environ['SAB_CAT'], + failure_link=os.environ['SAB_FAILURE_URL'], + ) + + +def process_legacy(args): + # SABnzbd argv: + # 1 The final directory of the job (full path) + # 2 The original name of the NZB file + # 3 Clean version of the job name (no path info and '.nzb' removed) + # 4 Indexer's report number (if supported) + # 5 User-defined category + # 6 Group that the NZB was posted in e.g. alt.binaries.x + # 7 Status of post processing. + # 0 = OK + # 1 = failed verification + # 2 = failed unpack + # 3 = 1+2 + client_agent = 'sabnzbd' + logger.info('Script triggered from SABnzbd') + return nzb.process( + args[1], + input_name=args[2], + status=int(args[7]), + input_category=args[5], + client_agent=client_agent, + download_id='', + ) + + +def process_0717(args): + # SABnzbd argv: + # 1 The final directory of the job (full path) + # 2 The original name of the NZB file + # 3 Clean version of the job name (no path info and '.nzb' removed) + # 4 Indexer's report number (if supported) + # 5 User-defined category + # 6 Group that the NZB was posted in e.g. alt.binaries.x + # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2 + # 8 Failure URL + client_agent = 'sabnzbd' + logger.info('Script triggered from SABnzbd 0.7.17+') + return nzb.process( + args[1], + input_name=args[2], + status=int(args[7]), + input_category=args[5], + client_agent=client_agent, + download_id='', + failure_link=''.join(args[8:]), + ) diff --git a/nzbToMedia.py b/nzbToMedia.py index bb88ce0b..d561e6d9 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -733,7 +733,7 @@ cleanup.clean(cleanup.FOLDER_STRUCTURE) import core from core import logger -from core.processor import nzbget +from core.processor import nzbget, sab from core.processor.nzb import process from core.auto_process.common import ProcessResult from core.utils import ( @@ -769,40 +769,13 @@ def main(args, section=None): result = nzbget.process() # SABnzbd elif 'SAB_SCRIPT' in os.environ: - client_agent = 'sabnzbd' - logger.info('Script triggered from SABnzbd Version {0}.'.format(os.environ['SAB_VERSION'])) - result = process(os.environ['SAB_COMPLETE_DIR'], input_name=os.environ['SAB_FINAL_NAME'], status=int(os.environ['SAB_PP_STATUS']), - client_agent=client_agent, download_id=os.environ['SAB_NZO_ID'], input_category=os.environ['SAB_CAT'], - failure_link=os.environ['SAB_FAILURE_URL']) + result = sab.process_script() # SABnzbd Pre 0.7.17 elif len(args) == core.SABNZB_NO_OF_ARGUMENTS: - # SABnzbd argv: - # 1 The final directory of the job (full path) - # 2 The original name of the NZB file - # 3 Clean version of the job name (no path info and '.nzb' removed) - # 4 Indexer's report number (if supported) - # 5 User-defined category - # 6 Group that the NZB was posted in e.g. alt.binaries.x - # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2 - client_agent = 'sabnzbd' - logger.info('Script triggered from SABnzbd') - result = process(args[1], input_name=args[2], status=int(args[7]), input_category=args[5], client_agent=client_agent, - download_id='') + result = sab.process_legacy(args) # SABnzbd 0.7.17+ elif len(args) >= core.SABNZB_0717_NO_OF_ARGUMENTS: - # SABnzbd argv: - # 1 The final directory of the job (full path) - # 2 The original name of the NZB file - # 3 Clean version of the job name (no path info and '.nzb' removed) - # 4 Indexer's report number (if supported) - # 5 User-defined category - # 6 Group that the NZB was posted in e.g. alt.binaries.x - # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2 - # 8 Failure URL - client_agent = 'sabnzbd' - logger.info('Script triggered from SABnzbd 0.7.17+') - result = process(args[1], input_name=args[2], status=int(args[7]), input_category=args[5], client_agent=client_agent, - download_id='', failure_link=''.join(args[8:])) + result = sab.process_0717(args) # Generic program elif len(args) > 5 and args[5] == 'generic': logger.info('Script triggered from generic program') From 528cbd02cdfb71ec0f1a412e76f1565959d643ea Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Thu, 24 Nov 2022 23:45:09 -0500 Subject: [PATCH 04/24] Extract manual processing from `nzbToMedia.main` -> `core.processor.manual` --- core/processor/manual.py | 72 ++++++++++++++++++++++++++++++++++++++++ nzbToMedia.py | 48 ++------------------------- 2 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 core/processor/manual.py diff --git a/core/processor/manual.py b/core/processor/manual.py new file mode 100644 index 00000000..4227cf3c --- /dev/null +++ b/core/processor/manual.py @@ -0,0 +1,72 @@ +import os + +import core +from core import logger +from core.auto_process.common import ProcessResult +from core.processor import nzb +from core.utils import ( + get_dirs, + get_download_info, +) + +try: + text_type = unicode +except NameError: + text_type = str + + +def process(): + # Perform Manual Post-Processing + logger.warning( + 'Invalid number of arguments received from client, Switching to manual run mode ...') + + # Post-Processing Result + result = ProcessResult( + message='', + status_code=0, + ) + + for section, subsections in core.SECTIONS.items(): + for subsection in subsections: + if not core.CFG[section][subsection].isenabled(): + continue + for dir_name in get_dirs(section, subsection, link='move'): + logger.info( + 'Starting manual run for {0}:{1} - Folder: {2}'.format( + section, subsection, dir_name)) + logger.info( + 'Checking database for download info for {0} ...'.format( + os.path.basename(dir_name))) + + core.DOWNLOAD_INFO = get_download_info( + os.path.basename(dir_name), 0) + if core.DOWNLOAD_INFO: + logger.info('Found download info for {0}, ' + 'setting variables now ...'.format + (os.path.basename(dir_name))) + client_agent = text_type( + core.DOWNLOAD_INFO[0]['client_agent']) or 'manual' + download_id = text_type( + core.DOWNLOAD_INFO[0]['input_id']) or '' + else: + logger.info('Unable to locate download info for {0}, ' + 'continuing to try and process this release ...'.format + (os.path.basename(dir_name))) + client_agent = 'manual' + download_id = '' + + if client_agent and client_agent.lower() not in core.NZB_CLIENTS: + continue + + input_name = os.path.basename(dir_name) + + results = nzb.process(dir_name, input_name, 0, + client_agent=client_agent, + download_id=download_id or None, + input_category=subsection) + if results.status_code != 0: + logger.error( + 'A problem was reported when trying to perform a manual run for {0}:{1}.'.format + (section, subsection)) + result = results + return result diff --git a/nzbToMedia.py b/nzbToMedia.py index d561e6d9..a9b89eee 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -733,18 +733,9 @@ cleanup.clean(cleanup.FOLDER_STRUCTURE) import core from core import logger -from core.processor import nzbget, sab +from core.processor import nzbget, sab, manual from core.processor.nzb import process from core.auto_process.common import ProcessResult -from core.utils import ( - get_dirs, - get_download_info, -) - -try: - text_type = unicode -except NameError: - text_type = str def main(args, section=None): @@ -783,42 +774,7 @@ def main(args, section=None): elif core.NZB_NO_MANUAL: logger.warning('Invalid number of arguments received from client, and no_manual set') else: - # Perform Manual Post-Processing - logger.warning('Invalid number of arguments received from client, Switching to manual run mode ...') - - for section, subsections in core.SECTIONS.items(): - for subsection in subsections: - if not core.CFG[section][subsection].isenabled(): - continue - for dir_name in get_dirs(section, subsection, link='move'): - logger.info('Starting manual run for {0}:{1} - Folder: {2}'.format(section, subsection, dir_name)) - logger.info('Checking database for download info for {0} ...'.format(os.path.basename(dir_name))) - - core.DOWNLOAD_INFO = get_download_info(os.path.basename(dir_name), 0) - if core.DOWNLOAD_INFO: - logger.info('Found download info for {0}, ' - 'setting variables now ...'.format - (os.path.basename(dir_name))) - client_agent = text_type(core.DOWNLOAD_INFO[0]['client_agent']) or 'manual' - download_id = text_type(core.DOWNLOAD_INFO[0]['input_id']) or '' - else: - logger.info('Unable to locate download info for {0}, ' - 'continuing to try and process this release ...'.format - (os.path.basename(dir_name))) - client_agent = 'manual' - download_id = '' - - if client_agent and client_agent.lower() not in core.NZB_CLIENTS: - continue - - input_name = os.path.basename(dir_name) - - results = process(dir_name, input_name, 0, client_agent=client_agent, - download_id=download_id or None, input_category=subsection) - if results.status_code != 0: - logger.error('A problem was reported when trying to perform a manual run for {0}:{1}.'.format - (section, subsection)) - result = results + manual.process() if result.status_code == 0: logger.info('The {0} script completed successfully.'.format(args[0])) From 637020d2bf74d5d9a52ec83c4b49a97484bf1a34 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 00:07:08 -0500 Subject: [PATCH 05/24] Merge legacy sab parsing with 0.7.17+ --- core/__init__.py | 4 --- core/processor/sab.py | 62 ++++++++++++++++--------------------------- nzbToMedia.py | 7 ++--- 3 files changed, 25 insertions(+), 48 deletions(-) diff --git a/core/__init__.py b/core/__init__.py index 7906c59b..c0c7debc 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -89,10 +89,6 @@ __version__ = '12.1.10' NZB_CLIENTS = ['sabnzbd', 'nzbget', 'manual'] TORRENT_CLIENTS = ['transmission', 'deluge', 'utorrent', 'rtorrent', 'qbittorrent', 'other', 'manual'] -# sabnzbd constants -SABNZB_NO_OF_ARGUMENTS = 8 -SABNZB_0717_NO_OF_ARGUMENTS = 9 - # sickbeard fork/branch constants FORK_DEFAULT = 'default' FORK_FAILED = 'failed' diff --git a/core/processor/sab.py b/core/processor/sab.py index 3e9f4050..701325e9 100644 --- a/core/processor/sab.py +++ b/core/processor/sab.py @@ -3,6 +3,9 @@ import os from core import logger from core.processor import nzb +# Constants +MINIMUM_ARGUMENTS = 8 + def process_script(): client_agent = 'sabnzbd' @@ -19,49 +22,30 @@ def process_script(): ) -def process_legacy(args): - # SABnzbd argv: - # 1 The final directory of the job (full path) - # 2 The original name of the NZB file - # 3 Clean version of the job name (no path info and '.nzb' removed) - # 4 Indexer's report number (if supported) - # 5 User-defined category - # 6 Group that the NZB was posted in e.g. alt.binaries.x - # 7 Status of post processing. - # 0 = OK - # 1 = failed verification - # 2 = failed unpack - # 3 = 1+2 - client_agent = 'sabnzbd' - logger.info('Script triggered from SABnzbd') +def process(args): + """ + SABnzbd arguments: + 1. The final directory of the job (full path) + 2. The original name of the NZB file + 3. Clean version of the job name (no path info and '.nzb' removed) + 4. Indexer's report number (if supported) + 5. User-defined category + 6. Group that the NZB was posted in e.g. alt.binaries.x + 7. Status of post processing: + 0 = OK + 1 = failed verification + 2 = failed unpack + 3 = 1+2 + 8. Failure URL + """ + version = '0.7.17+' if len(args) > MINIMUM_ARGUMENTS else '' + logger.info('Script triggered from SABnzbd {}'.format(version)) return nzb.process( - args[1], + input_directory=args[1], input_name=args[2], status=int(args[7]), input_category=args[5], - client_agent=client_agent, - download_id='', - ) - - -def process_0717(args): - # SABnzbd argv: - # 1 The final directory of the job (full path) - # 2 The original name of the NZB file - # 3 Clean version of the job name (no path info and '.nzb' removed) - # 4 Indexer's report number (if supported) - # 5 User-defined category - # 6 Group that the NZB was posted in e.g. alt.binaries.x - # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2 - # 8 Failure URL - client_agent = 'sabnzbd' - logger.info('Script triggered from SABnzbd 0.7.17+') - return nzb.process( - args[1], - input_name=args[2], - status=int(args[7]), - input_category=args[5], - client_agent=client_agent, + client_agent='sabnzbd', download_id='', failure_link=''.join(args[8:]), ) diff --git a/nzbToMedia.py b/nzbToMedia.py index a9b89eee..e5702999 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -762,11 +762,8 @@ def main(args, section=None): elif 'SAB_SCRIPT' in os.environ: result = sab.process_script() # SABnzbd Pre 0.7.17 - elif len(args) == core.SABNZB_NO_OF_ARGUMENTS: - result = sab.process_legacy(args) - # SABnzbd 0.7.17+ - elif len(args) >= core.SABNZB_0717_NO_OF_ARGUMENTS: - result = sab.process_0717(args) + elif len(args) >= sab.MINIMUM_ARGUMENTS: + result = sab.process(args) # Generic program elif len(args) > 5 and args[5] == 'generic': logger.info('Script triggered from generic program') From e8f5dc409a89da353b9b89fc4b1b22f12115418d Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 00:12:03 -0500 Subject: [PATCH 06/24] Standardize processing --- core/processor/sab.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/core/processor/sab.py b/core/processor/sab.py index 701325e9..b3c57203 100644 --- a/core/processor/sab.py +++ b/core/processor/sab.py @@ -8,14 +8,13 @@ MINIMUM_ARGUMENTS = 8 def process_script(): - client_agent = 'sabnzbd' - logger.info('Script triggered from SABnzbd Version {0}.'.format( - os.environ['SAB_VERSION'])) + version = os.environ['SAB_VERSION'] + logger.info('Script triggered from SABnzbd {0}.'.format(version)) return nzb.process( - os.environ['SAB_COMPLETE_DIR'], + input_directory=os.environ['SAB_COMPLETE_DIR'], input_name=os.environ['SAB_FINAL_NAME'], status=int(os.environ['SAB_PP_STATUS']), - client_agent=client_agent, + client_agent='sabnzbd', download_id=os.environ['SAB_NZO_ID'], input_category=os.environ['SAB_CAT'], failure_link=os.environ['SAB_FAILURE_URL'], From a2b2e4f6200c8211a95d5c25a8b239f8f9103b21 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 09:51:04 -0500 Subject: [PATCH 07/24] Extract download_id parsing from `core.processor.nzbget.process` -> `parse_download_id` --- core/processor/nzbget.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 1431d9e2..9d399a05 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -6,6 +6,22 @@ from core import logger from core.processor import nzb +def parse_download_id(): + # Check for download_id to pass to CouchPotato + download_id = '' + if 'NZBPR_COUCHPOTATO' in os.environ: + download_id = os.environ['NZBPR_COUCHPOTATO'] + elif 'NZBPR_DRONE' in os.environ: + download_id = os.environ['NZBPR_DRONE'] + elif 'NZBPR_SONARR' in os.environ: + download_id = os.environ['NZBPR_SONARR'] + elif 'NZBPR_RADARR' in os.environ: + download_id = os.environ['NZBPR_RADARR'] + elif 'NZBPR_LIDARR' in os.environ: + download_id = os.environ['NZBPR_LIDARR'] + return download_id + + def process(): # Check if the script is called from nzbget 11.0 or later if os.environ['NZBOP_VERSION'][0:5] < '11.0': @@ -54,19 +70,8 @@ def process(): logger.info( 'Please check your Par-check/repair settings for future downloads.') - # Check for download_id to pass to CouchPotato - download_id = '' + download_id = parse_download_id() failure_link = None - if 'NZBPR_COUCHPOTATO' in os.environ: - download_id = os.environ['NZBPR_COUCHPOTATO'] - elif 'NZBPR_DRONE' in os.environ: - download_id = os.environ['NZBPR_DRONE'] - elif 'NZBPR_SONARR' in os.environ: - download_id = os.environ['NZBPR_SONARR'] - elif 'NZBPR_RADARR' in os.environ: - download_id = os.environ['NZBPR_RADARR'] - elif 'NZBPR_LIDARR' in os.environ: - download_id = os.environ['NZBPR_LIDARR'] if 'NZBPR__DNZB_FAILURE' in os.environ: failure_link = os.environ['NZBPR__DNZB_FAILURE'] From 0a8e8fae9f142779131a5b9a5a43eee6cd0ec1d6 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 09:53:50 -0500 Subject: [PATCH 08/24] Extract failure_link parsing from `core.processor.nzbget.process` -> `parse_failure_link` --- core/processor/nzbget.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 9d399a05..594a4580 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -22,6 +22,13 @@ def parse_download_id(): return download_id +def parse_failure_link(): + failure_link = None + if 'NZBPR__DNZB_FAILURE' in os.environ: + failure_link = os.environ['NZBPR__DNZB_FAILURE'] + return failure_link + + def process(): # Check if the script is called from nzbget 11.0 or later if os.environ['NZBOP_VERSION'][0:5] < '11.0': @@ -71,9 +78,7 @@ def process(): 'Please check your Par-check/repair settings for future downloads.') download_id = parse_download_id() - failure_link = None - if 'NZBPR__DNZB_FAILURE' in os.environ: - failure_link = os.environ['NZBPR__DNZB_FAILURE'] + failure_link = parse_failure_link() # All checks done, now launching the script. client_agent = 'nzbget' From de06d45bb0e5633aa0d899d2d1d0fa93a07a912e Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 09:56:07 -0500 Subject: [PATCH 09/24] Extract status parsing from `core.processor.nzbget.process` -> `parse_status` --- core/processor/nzbget.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 594a4580..aec5190e 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -29,17 +29,7 @@ def parse_failure_link(): return failure_link -def process(): - # Check if the script is called from nzbget 11.0 or later - if os.environ['NZBOP_VERSION'][0:5] < '11.0': - logger.error( - 'NZBGet Version {0} is not supported. Please update NZBGet.'.format( - os.environ['NZBOP_VERSION'])) - sys.exit(core.NZBGET_POSTPROCESS_ERROR) - - logger.info('Script triggered from NZBGet Version {0}.'.format( - os.environ['NZBOP_VERSION'])) - +def parse_status(): status = 0 # Check if the script is called from nzbget 13.0 or later if 'NZBPP_TOTALSTATUS' in os.environ: @@ -76,7 +66,21 @@ def process(): 'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') logger.info( 'Please check your Par-check/repair settings for future downloads.') + return status + +def process(): + # Check if the script is called from nzbget 11.0 or later + if os.environ['NZBOP_VERSION'][0:5] < '11.0': + logger.error( + 'NZBGet Version {0} is not supported. Please update NZBGet.'.format( + os.environ['NZBOP_VERSION'])) + sys.exit(core.NZBGET_POSTPROCESS_ERROR) + + logger.info('Script triggered from NZBGet Version {0}.'.format( + os.environ['NZBOP_VERSION'])) + + status = parse_status() download_id = parse_download_id() failure_link = parse_failure_link() From 49af821bcb3d97683c216a99d538886635c1f218 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 09:58:59 -0500 Subject: [PATCH 10/24] Extract version checks from `core.processor.nzbget.process` -> `check_version` --- core/processor/nzbget.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index aec5190e..735f4ac2 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -69,7 +69,7 @@ def parse_status(): return status -def process(): +def check_version(): # Check if the script is called from nzbget 11.0 or later if os.environ['NZBOP_VERSION'][0:5] < '11.0': logger.error( @@ -80,6 +80,9 @@ def process(): logger.info('Script triggered from NZBGet Version {0}.'.format( os.environ['NZBOP_VERSION'])) + +def process(): + check_version() status = parse_status() download_id = parse_download_id() failure_link = parse_failure_link() From 3e676f89a59cfeb248a060c201970504fdf85a1a Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:01:04 -0500 Subject: [PATCH 11/24] Standardize processing --- core/processor/nzbget.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 735f4ac2..69dff6f9 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -86,14 +86,11 @@ def process(): status = parse_status() download_id = parse_download_id() failure_link = parse_failure_link() - - # All checks done, now launching the script. - client_agent = 'nzbget' return nzb.process( - os.environ['NZBPP_DIRECTORY'], + input_directory=os.environ['NZBPP_DIRECTORY'], input_name=os.environ['NZBPP_NZBNAME'], status=status, - client_agent=client_agent, + client_agent='nzbget', download_id=download_id, input_category=os.environ['NZBPP_CATEGORY'], failure_link=failure_link, From 9cc92ddd7b6562cf14a4be567fdf431cb122b845 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:06:10 -0500 Subject: [PATCH 12/24] Streamline `core.processor.nzbget.parse_download_id` --- core/processor/nzbget.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 69dff6f9..a76fbd6e 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -7,19 +7,21 @@ from core.processor import nzb def parse_download_id(): - # Check for download_id to pass to CouchPotato - download_id = '' - if 'NZBPR_COUCHPOTATO' in os.environ: - download_id = os.environ['NZBPR_COUCHPOTATO'] - elif 'NZBPR_DRONE' in os.environ: - download_id = os.environ['NZBPR_DRONE'] - elif 'NZBPR_SONARR' in os.environ: - download_id = os.environ['NZBPR_SONARR'] - elif 'NZBPR_RADARR' in os.environ: - download_id = os.environ['NZBPR_RADARR'] - elif 'NZBPR_LIDARR' in os.environ: - download_id = os.environ['NZBPR_LIDARR'] - return download_id + """Parse nzbget download_id from environment.""" + download_id_keys = [ + 'NZBPR_COUCHPOTATO', + 'NZBPR_DRONE', + 'NZBPR_SONARR', + 'NZBPR_RADARR', + 'NZBPR_LIDARR', + ] + for download_id_key in download_id_keys: + try: + return os.environ[download_id_key] + except KeyError: + pass + else: + return '' def parse_failure_link(): From d11dda8af8caa0b2abdabaf4d104b2c9a1cb4b1d Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:08:30 -0500 Subject: [PATCH 13/24] Streamline `core.processor.nzbget.parse_failure_link` --- core/processor/nzbget.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index a76fbd6e..a8953e6e 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -25,10 +25,8 @@ def parse_download_id(): def parse_failure_link(): - failure_link = None - if 'NZBPR__DNZB_FAILURE' in os.environ: - failure_link = os.environ['NZBPR__DNZB_FAILURE'] - return failure_link + """Parse nzbget failure_link from environment.""" + return os.environ.get('NZBPR__DNZB_FAILURE') def parse_status(): From d7c6a8e1cc86cd1c5c198dfb9c54282803e0fec5 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:12:36 -0500 Subject: [PATCH 14/24] Streamline `core.processor.nzbget.check_version` --- core/processor/nzbget.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index a8953e6e..b94378b5 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -70,15 +70,13 @@ def parse_status(): def check_version(): + """Check nzbget version and if version is unsupported, exit.""" + version = os.environ['NZBOP_VERSION'] # Check if the script is called from nzbget 11.0 or later - if os.environ['NZBOP_VERSION'][0:5] < '11.0': - logger.error( - 'NZBGet Version {0} is not supported. Please update NZBGet.'.format( - os.environ['NZBOP_VERSION'])) + if version[0:5] < '11.0': + logger.error('NZBGet Version {0} is not supported. Please update NZBGet.'.format(version)) sys.exit(core.NZBGET_POSTPROCESS_ERROR) - - logger.info('Script triggered from NZBGet Version {0}.'.format( - os.environ['NZBOP_VERSION'])) + logger.info('Script triggered from NZBGet Version {0}.'.format(version)) def process(): From fc2ebeb245eb75a139ecd10d4159e93cdb70fecf Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:24:07 -0500 Subject: [PATCH 15/24] Extract total status parsing from `core.processor.nzbget.parse_status` -> `_parse_total_status` --- core/processor/nzbget.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index b94378b5..ac6e79cb 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -29,15 +29,21 @@ def parse_failure_link(): return os.environ.get('NZBPR__DNZB_FAILURE') +def _parse_total_status(): + status = 0 + if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS': + logger.info('Download failed with status {0}.'.format( + os.environ['NZBPP_STATUS'])) + status = 1 + return status + return status + + def parse_status(): status = 0 # Check if the script is called from nzbget 13.0 or later if 'NZBPP_TOTALSTATUS' in os.environ: - if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS': - logger.info('Download failed with status {0}.'.format( - os.environ['NZBPP_STATUS'])) - status = 1 - + status = _parse_total_status() else: # Check par status if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ[ From e5ea34b569546d19f7bbbd35582362972e39123a Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:25:43 -0500 Subject: [PATCH 16/24] Streamline `core.processor.nzbget._parse_total_status` --- core/processor/nzbget.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index ac6e79cb..1d2dd635 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -30,13 +30,12 @@ def parse_failure_link(): def _parse_total_status(): - status = 0 - if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS': - logger.info('Download failed with status {0}.'.format( - os.environ['NZBPP_STATUS'])) - status = 1 - return status - return status + status_summary = os.environ['NZBPP_TOTALSTATUS'] + if status_summary != 'SUCCESS': + status = os.environ['NZBPP_STATUS'] + logger.info('Download failed with status {0}.'.format(status)) + return 1 + return 0 def parse_status(): From ab006eefb2b52f4857586ca381bc6d3ba83361e9 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:30:29 -0500 Subject: [PATCH 17/24] Extract par status parsing from `core.processor.nzbget.parse_status` -> `_parse_par_status` --- core/processor/nzbget.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 1d2dd635..5a9e60fb 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -38,6 +38,16 @@ def _parse_total_status(): return 0 +def _parse_par_status(): + """Parse nzbget par status from environment.""" + status = 0 + if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ[ + 'NZBPP_PARSTATUS'] == '4': + logger.warning('Par-repair failed, setting status \'failed\'') + status = 1 + return status + + def parse_status(): status = 0 # Check if the script is called from nzbget 13.0 or later @@ -45,10 +55,7 @@ def parse_status(): status = _parse_total_status() else: # Check par status - if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ[ - 'NZBPP_PARSTATUS'] == '4': - logger.warning('Par-repair failed, setting status \'failed\'') - status = 1 + par_status = _parse_par_status() # Check unpack status if os.environ['NZBPP_UNPACKSTATUS'] == '1': @@ -71,6 +78,7 @@ def parse_status(): 'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') logger.info( 'Please check your Par-check/repair settings for future downloads.') + return par_status or status return status From 8e96d17537e8a336af377adc7dd82f6382f728f6 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:31:19 -0500 Subject: [PATCH 18/24] Streamline `core.processor.nzbget._parse_par_status` --- core/processor/nzbget.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 5a9e60fb..26a65f4e 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -40,12 +40,11 @@ def _parse_total_status(): def _parse_par_status(): """Parse nzbget par status from environment.""" - status = 0 - if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ[ - 'NZBPP_PARSTATUS'] == '4': + par_status = os.environ['NZBPP_PARSTATUS'] + if par_status == '1' or par_status == '4': logger.warning('Par-repair failed, setting status \'failed\'') - status = 1 - return status + return 1 + return 0 def parse_status(): From 11adb220d8c317470d299691647e7fdf5428ba7b Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:33:48 -0500 Subject: [PATCH 19/24] Extract unpack status parsing from `core.processor.nzbget.parse_status` -> `_parse_unpack_status` --- core/processor/nzbget.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 26a65f4e..b11c7065 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -47,6 +47,14 @@ def _parse_par_status(): return 0 +def _parse_unpack_status(): + status = 0 + if os.environ['NZBPP_UNPACKSTATUS'] == '1': + logger.warning('Unpack failed, setting status \'failed\'') + status = 1 + return status + + def parse_status(): status = 0 # Check if the script is called from nzbget 13.0 or later @@ -57,9 +65,7 @@ def parse_status(): par_status = _parse_par_status() # Check unpack status - if os.environ['NZBPP_UNPACKSTATUS'] == '1': - logger.warning('Unpack failed, setting status \'failed\'') - status = 1 + unpack_status = _parse_unpack_status() if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ[ 'NZBPP_PARSTATUS'] == '0': @@ -77,7 +83,7 @@ def parse_status(): 'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') logger.info( 'Please check your Par-check/repair settings for future downloads.') - return par_status or status + return par_status or unpack_status or status return status From efee5c722b8e3a74413e1c88a0beff2b03e21706 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:39:46 -0500 Subject: [PATCH 20/24] Extract health status parsing from `core.processor.nzbget.parse_status` -> `_parse_health_status` --- core/processor/nzbget.py | 41 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index b11c7065..7cdd09e3 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -55,6 +55,28 @@ def _parse_unpack_status(): return status +def _parse_health_status(): + """Parse nzbget download health from environment.""" + status = 0 + if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ[ + 'NZBPP_PARSTATUS'] == '0': + # Unpack was skipped due to nzb-file properties or due to errors during par-check + + if os.environ['NZBPP_HEALTH'] < 1000: + logger.warning( + 'Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \'failed\'') + logger.info( + 'Please check your Par-check/repair settings for future downloads.') + status = 1 + + else: + logger.info( + 'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') + logger.info( + 'Please check your Par-check/repair settings for future downloads.') + return status + + def parse_status(): status = 0 # Check if the script is called from nzbget 13.0 or later @@ -67,23 +89,10 @@ def parse_status(): # Check unpack status unpack_status = _parse_unpack_status() - if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ[ - 'NZBPP_PARSTATUS'] == '0': - # Unpack was skipped due to nzb-file properties or due to errors during par-check + # Check download health + health_status = _parse_health_status() - if os.environ['NZBPP_HEALTH'] < 1000: - logger.warning( - 'Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \'failed\'') - logger.info( - 'Please check your Par-check/repair settings for future downloads.') - status = 1 - - else: - logger.info( - 'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') - logger.info( - 'Please check your Par-check/repair settings for future downloads.') - return par_status or unpack_status or status + return par_status or unpack_status or health_status or status return status From c34159d881361de6be0068dfac06130b7d12521a Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:45:13 -0500 Subject: [PATCH 21/24] Streamline `core.processor.nzbget._parse_health_status` --- core/processor/nzbget.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 7cdd09e3..d92043cc 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -58,22 +58,17 @@ def _parse_unpack_status(): def _parse_health_status(): """Parse nzbget download health from environment.""" status = 0 - if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ[ - 'NZBPP_PARSTATUS'] == '0': - # Unpack was skipped due to nzb-file properties or due to errors during par-check - - if os.environ['NZBPP_HEALTH'] < 1000: - logger.warning( - 'Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \'failed\'') - logger.info( - 'Please check your Par-check/repair settings for future downloads.') + unpack_status_value = os.environ['NZBPP_UNPACKSTATUS'] + par_status_value = os.environ['NZBPP_PARSTATUS'] + if unpack_status_value == '0' and par_status_value == '0': + # Unpack was skipped due to nzb-file properties + # or due to errors during par-check + if int(os.environ['NZBPP_HEALTH']) < 1000: + logger.warning('Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \'failed\'') status = 1 - else: - logger.info( - 'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') - logger.info( - 'Please check your Par-check/repair settings for future downloads.') + logger.info('Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful') + logger.info('Please check your Par-check/repair settings for future downloads.') return status From 7737d0c4bea77596027177683c6994433fb78999 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:46:20 -0500 Subject: [PATCH 22/24] Streamline `core.processor.nzbget._parse_unpack_status` --- core/processor/nzbget.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index d92043cc..5a8358f6 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -48,11 +48,10 @@ def _parse_par_status(): def _parse_unpack_status(): - status = 0 if os.environ['NZBPP_UNPACKSTATUS'] == '1': logger.warning('Unpack failed, setting status \'failed\'') - status = 1 - return status + return 1 + return 0 def _parse_health_status(): From 34236e8960802ec98cbe864a103fc8bcae315379 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Fri, 25 Nov 2022 10:47:40 -0500 Subject: [PATCH 23/24] Streamline `core.processor.nzbget.parse_status` --- core/processor/nzbget.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/core/processor/nzbget.py b/core/processor/nzbget.py index 5a8358f6..049674bc 100644 --- a/core/processor/nzbget.py +++ b/core/processor/nzbget.py @@ -72,21 +72,13 @@ def _parse_health_status(): def parse_status(): - status = 0 - # Check if the script is called from nzbget 13.0 or later - if 'NZBPP_TOTALSTATUS' in os.environ: + if 'NZBPP_TOTALSTATUS' in os.environ: # Called from nzbget 13.0 or later status = _parse_total_status() else: - # Check par status par_status = _parse_par_status() - - # Check unpack status unpack_status = _parse_unpack_status() - - # Check download health health_status = _parse_health_status() - - return par_status or unpack_status or health_status or status + status = par_status or unpack_status or health_status return status From c85ee42874f66b50d070e3046d9681336db45f6e Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sun, 27 Nov 2022 18:50:49 -0500 Subject: [PATCH 24/24] Add `processor` folder to folder structure --- cleanup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cleanup.py b/cleanup.py index 3724844b..bb4caf98 100644 --- a/cleanup.py +++ b/cleanup.py @@ -25,6 +25,7 @@ FOLDER_STRUCTURE = { 'auto_process', 'extractor', 'plugins', + 'processor', 'utils', ], }