diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 0a6ba62a..11160988 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -11,6 +11,7 @@ import sys import core from core import logger, main_db from core.auto_process import comics, games, movies, music, tv +from core.auto_process.common import ProcessResult from core.user_scripts import external_script from core.utils import char_replace, convert_to_ascii, plex_update, replace_links from six import text_type @@ -233,31 +234,28 @@ def process_torrent(input_directory, input_name, input_category, input_hash, inp if core.TORRENT_CHMOD_DIRECTORY: core.rchmod(output_destination, core.TORRENT_CHMOD_DIRECTORY) - result = [0, ""] + result = ProcessResult( + message="", + status_code=0, + ) if section_name == 'UserScript': result = external_script(output_destination, input_name, input_category, section) - elif section_name in ['CouchPotato', 'Radarr']: - result = movies.process(section_name, output_destination, input_name, - status, client_agent, input_hash, input_category) + result = movies.process(section_name, output_destination, input_name, status, client_agent, input_hash, input_category) elif section_name in ['SickBeard', 'NzbDrone', 'Sonarr']: if input_hash: input_hash = input_hash.upper() - result = tv.process(section_name, output_destination, input_name, - status, client_agent, input_hash, input_category) + result = tv.process(section_name, output_destination, input_name, status, client_agent, input_hash, input_category) elif section_name in ['HeadPhones', 'Lidarr']: - result = music.process(section_name, output_destination, input_name, - status, client_agent, input_category) + result = music.process(section_name, output_destination, input_name, status, client_agent, input_category) elif section_name == 'Mylar': - result = comics.process(section_name, output_destination, input_name, - status, client_agent, input_category) + result = comics.process(section_name, output_destination, input_name, status, client_agent, input_category) elif section_name == 'Gamez': - result = games.process(section_name, output_destination, input_name, - status, client_agent, input_category) + result = games.process(section_name, output_destination, input_name, status, client_agent, input_category) plex_update(input_category) - if result[0] != 0: + if result.status_code != 0: if not core.TORRENT_RESUME_ON_FAILURE: logger.error("A problem was reported in the autoProcess* script. " "Torrent won't resume seeding (settings)") @@ -303,7 +301,10 @@ def main(args): logger.debug("Options passed into TorrentToMedia: {0}".format(args)) # Post-Processing Result - result = [0, ""] + result = ProcessResult( + message="", + status_code=0, + ) try: input_directory, input_name, input_category, input_hash, input_id = core.parse_args(client_agent, args) @@ -362,12 +363,12 @@ def main(args): (section, subsection)) result = results - if result[0] == 0: + if result.status_code == 0: logger.info("The {0} script completed successfully.".format(args[0])) else: logger.error("A problem was reported in the {0} script.".format(args[0])) del core.MYAPP - return result[0] + return result.status_code if __name__ == "__main__": diff --git a/core/auto_process/comics.py b/core/auto_process/comics.py index 3eb8a247..0dfda8d0 100644 --- a/core/auto_process/comics.py +++ b/core/auto_process/comics.py @@ -6,6 +6,7 @@ import requests import core from core import logger +from core.auto_process.common import ProcessResult from core.utils import convert_to_ascii, remote_dir, server_responding requests.packages.urllib3.disable_warnings() @@ -28,7 +29,10 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual', url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root) if not server_responding(url): logger.error("Server did not respond. Exiting", section) - return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)] + return ProcessResult( + message="{0}: Failed to post-process - {0} did not respond.".format(section), + status_code=1, + ) input_name, dir_name = convert_to_ascii(input_name, dir_name) clean_name, ext = os.path.splitext(input_name) @@ -54,10 +58,16 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual', r = requests.post(url, params=params, stream=True, verify=False, timeout=(30, 300)) except requests.ConnectionError: logger.error("Unable to open URL", section) - return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)] + return ProcessResult( + message="{0}: Failed to post-process - Unable to connect to {0}".format(section), + status_code=1 + ) if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]: logger.error("Server returned status {0}".format(r.status_code), section) - return [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)] + return ProcessResult( + message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code), + status_code=1, + ) result = r.content if not type(result) == list: @@ -70,7 +80,13 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual', if success: logger.postprocess("SUCCESS: This issue has been processed successfully", section) - return [0, "{0}: Successfully post-processed {1}".format(section, input_name)] + return ProcessResult( + message="{0}: Successfully post-processed {1}".format(section, input_name), + status_code=0, + ) else: logger.warning("The issue does not appear to have successfully processed. Please check your Logs", section) - return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)] + return ProcessResult( + message="{0}: Failed to post-process - Returned log from {0} was not as expected.".format(section), + status_code=1, + ) diff --git a/core/auto_process/common.py b/core/auto_process/common.py index 4d4e6ae8..53e231e0 100644 --- a/core/auto_process/common.py +++ b/core/auto_process/common.py @@ -3,6 +3,30 @@ import requests from core import logger +class ProcessResult(object): + def __init__(self, message, status_code): + self.message = message + self.status_code = status_code + + def __iter__(self): + return self.status_code, self.message + + def __bool__(self): + return not bool(self.status_code) + + def __str__(self): + return 'Processing {0}: {1}'.format( + 'succeeded' if bool(self) else 'failed', + self.message + ) + + def __repr__(self): + return ''.format( + self.status_code, + self.message, + ) + + def command_complete(url, params, headers, section): try: r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60)) diff --git a/core/auto_process/games.py b/core/auto_process/games.py index 38d88782..f0cd63a3 100644 --- a/core/auto_process/games.py +++ b/core/auto_process/games.py @@ -7,6 +7,7 @@ import requests import core from core import logger +from core.auto_process.common import ProcessResult from core.utils import convert_to_ascii, server_responding requests.packages.urllib3.disable_warnings() @@ -28,7 +29,10 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual', url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root) if not server_responding(url): logger.error("Server did not respond. Exiting", section) - return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)] + return ProcessResult( + message="{0}: Failed to post-process - {0} did not respond.".format(section), + status_code=1, + ) input_name, dir_name = convert_to_ascii(input_name, dir_name) @@ -51,7 +55,10 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual', r = requests.get(url, params=params, verify=False, timeout=(30, 300)) except requests.ConnectionError: logger.error("Unable to open URL") - return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)] + return ProcessResult( + message="{0}: Failed to post-process - Unable to connect to {1}".format(section, section), + status_code=1, + ) result = r.json() logger.postprocess("{0}".format(result), section) @@ -61,17 +68,32 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual', shutil.move(dir_name, os.path.join(library, input_name)) except Exception: logger.error("Unable to move {0} to {1}".format(dir_name, os.path.join(library, input_name)), section) - return [1, "{0}: Failed to post-process - Unable to move files".format(section)] + return ProcessResult( + message="{0}: Failed to post-process - Unable to move files".format(section), + status_code=1, + ) else: logger.error("No library specified to move files to. Please edit your configuration.", section) - return [1, "{0}: Failed to post-process - No library defined in {1}".format(section, section)] + return ProcessResult( + message="{0}: Failed to post-process - No library defined in {0}".format(section), + status_code=1, + ) if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]: logger.error("Server returned status {0}".format(r.status_code), section) - return [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)] + return ProcessResult( + message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code), + status_code=1, + ) elif result['success']: logger.postprocess("SUCCESS: Status for {0} has been set to {1} in Gamez".format(gamez_id, download_status), section) - return [0, "{0}: Successfully post-processed {1}".format(section, input_name)] + return ProcessResult( + message="{0}: Successfully post-processed {1}".format(section, input_name), + status_code=0, + ) else: logger.error("FAILED: Status for {0} has NOT been updated in Gamez".format(gamez_id), section) - return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)] + return ProcessResult( + message="{0}: Failed to post-process - Returned log from {0} was not as expected.".format(section), + status_code=1, + ) diff --git a/nzbToMedia.py b/nzbToMedia.py index ebcc9e5f..5e904c91 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -13,6 +13,7 @@ import sys import core from core import logger, main_db from core.auto_process import comics, games, movies, music, tv +from core.auto_process.common import ProcessResult 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, get_nzoid, plex_update, update_download_info_status @@ -109,26 +110,26 @@ def process(input_directory, input_name=None, status=0, client_agent='manual', d logger.info("Calling {0}:{1} to post-process:{2}".format(section_name, input_category, input_name)) if section_name in ["CouchPotato", "Radarr"]: - result = movies.process(section_name, input_directory, input_name, status, client_agent, download_id, - input_category, failure_link) + result = movies.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link) elif section_name in ["SickBeard", "NzbDrone", "Sonarr"]: - result = tv.process(section_name, input_directory, input_name, status, client_agent, - download_id, input_category, failure_link) + 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) + 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 == 'UserScript': result = external_script(input_directory, input_name, input_category, section[usercat]) else: - result = [-1, ""] + result = ProcessResult( + message="", + status_code=-1, + ) plex_update(input_category) - if result[0] == 0: + if result.staus_code == 0: if client_agent != 'manual': # update download status in our DB update_download_info_status(input_name, 1) @@ -151,7 +152,10 @@ def main(args, section=None): logger.debug("Options passed into nzbToMedia: {0}".format(args)) # Post-Processing Result - result = [0, ""] + result = ProcessResult( + message="", + status_code=0, + ) status = 0 # NZBGet @@ -289,27 +293,27 @@ def main(args, section=None): results = process(dir_name, input_name, 0, client_agent=client_agent, download_id=download_id or None, input_category=subsection) - if results[0] != 0: + 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 - if result[0] == 0: + if result.status_code == 0: logger.info("The {0} script completed successfully.".format(args[0])) - if result[1]: - print(result[1] + "!") + if result.message: + print(result.message + "!") if 'NZBOP_SCRIPTDIR' in os.environ: # return code for nzbget v11 del core.MYAPP return core.NZBGET_POSTPROCESS_SUCCESS else: logger.error("A problem was reported in the {0} script.".format(args[0])) - if result[1]: - print(result[1] + "!") + if result.message: + print(result.message + "!") if 'NZBOP_SCRIPTDIR' in os.environ: # return code for nzbget v11 del core.MYAPP return core.NZBGET_POSTPROCESS_ERROR del core.MYAPP - return result[0] + return result.status_code if __name__ == '__main__':