mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-20 05:13:16 -07:00
Add ProcessResult to auto_process.common
This commit is contained in:
parent
1d46f716e1
commit
8c4353bc90
5 changed files with 111 additions and 44 deletions
|
@ -11,6 +11,7 @@ import sys
|
||||||
import core
|
import core
|
||||||
from core import logger, main_db
|
from core import logger, main_db
|
||||||
from core.auto_process import comics, games, movies, music, tv
|
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.user_scripts import external_script
|
||||||
from core.utils import char_replace, convert_to_ascii, plex_update, replace_links
|
from core.utils import char_replace, convert_to_ascii, plex_update, replace_links
|
||||||
from six import text_type
|
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:
|
if core.TORRENT_CHMOD_DIRECTORY:
|
||||||
core.rchmod(output_destination, core.TORRENT_CHMOD_DIRECTORY)
|
core.rchmod(output_destination, core.TORRENT_CHMOD_DIRECTORY)
|
||||||
|
|
||||||
result = [0, ""]
|
result = ProcessResult(
|
||||||
|
message="",
|
||||||
|
status_code=0,
|
||||||
|
)
|
||||||
if section_name == 'UserScript':
|
if section_name == 'UserScript':
|
||||||
result = external_script(output_destination, input_name, input_category, section)
|
result = external_script(output_destination, input_name, input_category, section)
|
||||||
|
|
||||||
elif section_name in ['CouchPotato', 'Radarr']:
|
elif section_name in ['CouchPotato', 'Radarr']:
|
||||||
result = movies.process(section_name, output_destination, input_name,
|
result = movies.process(section_name, output_destination, input_name, status, client_agent, input_hash, input_category)
|
||||||
status, client_agent, input_hash, input_category)
|
|
||||||
elif section_name in ['SickBeard', 'NzbDrone', 'Sonarr']:
|
elif section_name in ['SickBeard', 'NzbDrone', 'Sonarr']:
|
||||||
if input_hash:
|
if input_hash:
|
||||||
input_hash = input_hash.upper()
|
input_hash = input_hash.upper()
|
||||||
result = tv.process(section_name, output_destination, input_name,
|
result = tv.process(section_name, output_destination, input_name, status, client_agent, input_hash, input_category)
|
||||||
status, client_agent, input_hash, input_category)
|
|
||||||
elif section_name in ['HeadPhones', 'Lidarr']:
|
elif section_name in ['HeadPhones', 'Lidarr']:
|
||||||
result = music.process(section_name, output_destination, input_name,
|
result = music.process(section_name, output_destination, input_name, status, client_agent, input_category)
|
||||||
status, client_agent, input_category)
|
|
||||||
elif section_name == 'Mylar':
|
elif section_name == 'Mylar':
|
||||||
result = comics.process(section_name, output_destination, input_name,
|
result = comics.process(section_name, output_destination, input_name, status, client_agent, input_category)
|
||||||
status, client_agent, input_category)
|
|
||||||
elif section_name == 'Gamez':
|
elif section_name == 'Gamez':
|
||||||
result = games.process(section_name, output_destination, input_name,
|
result = games.process(section_name, output_destination, input_name, status, client_agent, input_category)
|
||||||
status, client_agent, input_category)
|
|
||||||
|
|
||||||
plex_update(input_category)
|
plex_update(input_category)
|
||||||
|
|
||||||
if result[0] != 0:
|
if result.status_code != 0:
|
||||||
if not core.TORRENT_RESUME_ON_FAILURE:
|
if not core.TORRENT_RESUME_ON_FAILURE:
|
||||||
logger.error("A problem was reported in the autoProcess* script. "
|
logger.error("A problem was reported in the autoProcess* script. "
|
||||||
"Torrent won't resume seeding (settings)")
|
"Torrent won't resume seeding (settings)")
|
||||||
|
@ -303,7 +301,10 @@ def main(args):
|
||||||
logger.debug("Options passed into TorrentToMedia: {0}".format(args))
|
logger.debug("Options passed into TorrentToMedia: {0}".format(args))
|
||||||
|
|
||||||
# Post-Processing Result
|
# Post-Processing Result
|
||||||
result = [0, ""]
|
result = ProcessResult(
|
||||||
|
message="",
|
||||||
|
status_code=0,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
input_directory, input_name, input_category, input_hash, input_id = core.parse_args(client_agent, args)
|
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))
|
(section, subsection))
|
||||||
result = results
|
result = results
|
||||||
|
|
||||||
if result[0] == 0:
|
if result.status_code == 0:
|
||||||
logger.info("The {0} script completed successfully.".format(args[0]))
|
logger.info("The {0} script completed successfully.".format(args[0]))
|
||||||
else:
|
else:
|
||||||
logger.error("A problem was reported in the {0} script.".format(args[0]))
|
logger.error("A problem was reported in the {0} script.".format(args[0]))
|
||||||
del core.MYAPP
|
del core.MYAPP
|
||||||
return result[0]
|
return result.status_code
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -6,6 +6,7 @@ import requests
|
||||||
|
|
||||||
import core
|
import core
|
||||||
from core import logger
|
from core import logger
|
||||||
|
from core.auto_process.common import ProcessResult
|
||||||
from core.utils import convert_to_ascii, remote_dir, server_responding
|
from core.utils import convert_to_ascii, remote_dir, server_responding
|
||||||
|
|
||||||
requests.packages.urllib3.disable_warnings()
|
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)
|
url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root)
|
||||||
if not server_responding(url):
|
if not server_responding(url):
|
||||||
logger.error("Server did not respond. Exiting", section)
|
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)
|
input_name, dir_name = convert_to_ascii(input_name, dir_name)
|
||||||
clean_name, ext = os.path.splitext(input_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))
|
r = requests.post(url, params=params, stream=True, verify=False, timeout=(30, 300))
|
||||||
except requests.ConnectionError:
|
except requests.ConnectionError:
|
||||||
logger.error("Unable to open URL", section)
|
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]:
|
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)
|
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
|
result = r.content
|
||||||
if not type(result) == list:
|
if not type(result) == list:
|
||||||
|
@ -70,7 +80,13 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual',
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
logger.postprocess("SUCCESS: This issue has been processed successfully", section)
|
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:
|
else:
|
||||||
logger.warning("The issue does not appear to have successfully processed. Please check your Logs", section)
|
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,
|
||||||
|
)
|
||||||
|
|
|
@ -3,6 +3,30 @@ import requests
|
||||||
from core import logger
|
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 '<ProcessResult {0}: {1}>'.format(
|
||||||
|
self.status_code,
|
||||||
|
self.message,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def command_complete(url, params, headers, section):
|
def command_complete(url, params, headers, section):
|
||||||
try:
|
try:
|
||||||
r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60))
|
r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60))
|
||||||
|
|
|
@ -7,6 +7,7 @@ import requests
|
||||||
|
|
||||||
import core
|
import core
|
||||||
from core import logger
|
from core import logger
|
||||||
|
from core.auto_process.common import ProcessResult
|
||||||
from core.utils import convert_to_ascii, server_responding
|
from core.utils import convert_to_ascii, server_responding
|
||||||
|
|
||||||
requests.packages.urllib3.disable_warnings()
|
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)
|
url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root)
|
||||||
if not server_responding(url):
|
if not server_responding(url):
|
||||||
logger.error("Server did not respond. Exiting", section)
|
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)
|
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))
|
r = requests.get(url, params=params, verify=False, timeout=(30, 300))
|
||||||
except requests.ConnectionError:
|
except requests.ConnectionError:
|
||||||
logger.error("Unable to open URL")
|
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()
|
result = r.json()
|
||||||
logger.postprocess("{0}".format(result), section)
|
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))
|
shutil.move(dir_name, os.path.join(library, input_name))
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.error("Unable to move {0} to {1}".format(dir_name, os.path.join(library, input_name)), section)
|
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:
|
else:
|
||||||
logger.error("No library specified to move files to. Please edit your configuration.", section)
|
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]:
|
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)
|
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']:
|
elif result['success']:
|
||||||
logger.postprocess("SUCCESS: Status for {0} has been set to {1} in Gamez".format(gamez_id, download_status), section)
|
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:
|
else:
|
||||||
logger.error("FAILED: Status for {0} has NOT been updated in Gamez".format(gamez_id), section)
|
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,
|
||||||
|
)
|
||||||
|
|
|
@ -13,6 +13,7 @@ import sys
|
||||||
import core
|
import core
|
||||||
from core import logger, main_db
|
from core import logger, main_db
|
||||||
from core.auto_process import comics, games, movies, music, tv
|
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.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
|
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))
|
logger.info("Calling {0}:{1} to post-process:{2}".format(section_name, input_category, input_name))
|
||||||
|
|
||||||
if section_name in ["CouchPotato", "Radarr"]:
|
if section_name in ["CouchPotato", "Radarr"]:
|
||||||
result = movies.process(section_name, input_directory, input_name, status, client_agent, download_id,
|
result = movies.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link)
|
||||||
input_category, failure_link)
|
|
||||||
elif section_name in ["SickBeard", "NzbDrone", "Sonarr"]:
|
elif section_name in ["SickBeard", "NzbDrone", "Sonarr"]:
|
||||||
result = tv.process(section_name, input_directory, input_name, status, client_agent,
|
result = tv.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link)
|
||||||
download_id, input_category, failure_link)
|
|
||||||
elif section_name in ["HeadPhones", "Lidarr"]:
|
elif section_name in ["HeadPhones", "Lidarr"]:
|
||||||
result = music.process(section_name, input_directory, input_name, status, client_agent, input_category)
|
result = music.process(section_name, input_directory, input_name, status, client_agent, input_category)
|
||||||
elif section_name == "Mylar":
|
elif section_name == "Mylar":
|
||||||
result = comics.process(section_name, input_directory, input_name, status, client_agent,
|
result = comics.process(section_name, input_directory, input_name, status, client_agent, input_category)
|
||||||
input_category)
|
|
||||||
elif section_name == "Gamez":
|
elif section_name == "Gamez":
|
||||||
result = games.process(section_name, input_directory, input_name, status, client_agent, input_category)
|
result = games.process(section_name, input_directory, input_name, status, client_agent, input_category)
|
||||||
elif section_name == 'UserScript':
|
elif section_name == 'UserScript':
|
||||||
result = external_script(input_directory, input_name, input_category, section[usercat])
|
result = external_script(input_directory, input_name, input_category, section[usercat])
|
||||||
else:
|
else:
|
||||||
result = [-1, ""]
|
result = ProcessResult(
|
||||||
|
message="",
|
||||||
|
status_code=-1,
|
||||||
|
)
|
||||||
|
|
||||||
plex_update(input_category)
|
plex_update(input_category)
|
||||||
|
|
||||||
if result[0] == 0:
|
if result.staus_code == 0:
|
||||||
if client_agent != 'manual':
|
if client_agent != 'manual':
|
||||||
# update download status in our DB
|
# update download status in our DB
|
||||||
update_download_info_status(input_name, 1)
|
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))
|
logger.debug("Options passed into nzbToMedia: {0}".format(args))
|
||||||
|
|
||||||
# Post-Processing Result
|
# Post-Processing Result
|
||||||
result = [0, ""]
|
result = ProcessResult(
|
||||||
|
message="",
|
||||||
|
status_code=0,
|
||||||
|
)
|
||||||
status = 0
|
status = 0
|
||||||
|
|
||||||
# NZBGet
|
# NZBGet
|
||||||
|
@ -289,27 +293,27 @@ def main(args, section=None):
|
||||||
|
|
||||||
results = process(dir_name, input_name, 0, client_agent=client_agent,
|
results = process(dir_name, input_name, 0, client_agent=client_agent,
|
||||||
download_id=download_id or None, input_category=subsection)
|
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
|
logger.error("A problem was reported when trying to perform a manual run for {0}:{1}.".format
|
||||||
(section, subsection))
|
(section, subsection))
|
||||||
result = results
|
result = results
|
||||||
|
|
||||||
if result[0] == 0:
|
if result.status_code == 0:
|
||||||
logger.info("The {0} script completed successfully.".format(args[0]))
|
logger.info("The {0} script completed successfully.".format(args[0]))
|
||||||
if result[1]:
|
if result.message:
|
||||||
print(result[1] + "!")
|
print(result.message + "!")
|
||||||
if 'NZBOP_SCRIPTDIR' in os.environ: # return code for nzbget v11
|
if 'NZBOP_SCRIPTDIR' in os.environ: # return code for nzbget v11
|
||||||
del core.MYAPP
|
del core.MYAPP
|
||||||
return core.NZBGET_POSTPROCESS_SUCCESS
|
return core.NZBGET_POSTPROCESS_SUCCESS
|
||||||
else:
|
else:
|
||||||
logger.error("A problem was reported in the {0} script.".format(args[0]))
|
logger.error("A problem was reported in the {0} script.".format(args[0]))
|
||||||
if result[1]:
|
if result.message:
|
||||||
print(result[1] + "!")
|
print(result.message + "!")
|
||||||
if 'NZBOP_SCRIPTDIR' in os.environ: # return code for nzbget v11
|
if 'NZBOP_SCRIPTDIR' in os.environ: # return code for nzbget v11
|
||||||
del core.MYAPP
|
del core.MYAPP
|
||||||
return core.NZBGET_POSTPROCESS_ERROR
|
return core.NZBGET_POSTPROCESS_ERROR
|
||||||
del core.MYAPP
|
del core.MYAPP
|
||||||
return result[0]
|
return result.status_code
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue