Remove superfluous classes

This commit is contained in:
Labrys of Knossos 2018-12-27 08:24:05 -05:00 committed by Lizband
parent 5a04a7b4d9
commit d95fc59b45
7 changed files with 1042 additions and 1038 deletions

View file

@ -10,6 +10,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.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
@ -237,21 +238,21 @@ def process_torrent(input_directory, input_name, input_category, input_hash, inp
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 = core.Movie().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 = core.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 = core.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 = core.Comic().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 = core.Game().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)

View file

@ -11,67 +11,66 @@ from core.utils import convert_to_ascii, remote_dir, server_responding
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
class Comic(object): def process(section, dir_name, input_name=None, status=0, client_agent='manual', input_category=None):
def process(self, section, dir_name, input_name=None, status=0, client_agent='manual', input_category=None): apc_version = "2.04"
apc_version = "2.04" comicrn_version = "1.01"
comicrn_version = "1.01"
cfg = dict(core.CFG[section][input_category]) cfg = dict(core.CFG[section][input_category])
host = cfg["host"] host = cfg["host"]
port = cfg["port"] port = cfg["port"]
apikey = cfg["apikey"] apikey = cfg["apikey"]
ssl = int(cfg.get("ssl", 0)) ssl = int(cfg.get("ssl", 0))
web_root = cfg.get("web_root", "") web_root = cfg.get("web_root", "")
remote_path = int(cfg.get("remote_path"), 0) remote_path = int(cfg.get("remote_path"), 0)
protocol = "https://" if ssl else "http://" protocol = "https://" if ssl else "http://"
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 [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]
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)
if len(ext) == 4: # we assume this was a standard extension. if len(ext) == 4: # we assume this was a standard extension.
input_name = clean_name input_name = clean_name
params = { params = {
'cmd': 'forceProcess', 'cmd': 'forceProcess',
'apikey': apikey, 'apikey': apikey,
'nzb_folder': remote_dir(dir_name) if remote_path else dir_name, 'nzb_folder': remote_dir(dir_name) if remote_path else dir_name,
} }
if input_name is not None: if input_name is not None:
params['nzb_name'] = input_name params['nzb_name'] = input_name
params['failed'] = int(status) params['failed'] = int(status)
params['apc_version'] = apc_version params['apc_version'] = apc_version
params['comicrn_version'] = comicrn_version params['comicrn_version'] = comicrn_version
success = False success = False
logger.debug("Opening URL: {0}".format(url), section) logger.debug("Opening URL: {0}".format(url), section)
try: try:
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 [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
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 [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]
result = r.content result = r.content
if not type(result) == list: if not type(result) == list:
result = result.split('\n') result = result.split('\n')
for line in result: for line in result:
if line: if line:
logger.postprocess("{0}".format(line), section) logger.postprocess("{0}".format(line), section)
if "Post Processing SUCCESSFUL" in line: if "Post Processing SUCCESSFUL" in line:
success = True success = True
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 [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
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 [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)]

View file

@ -12,67 +12,66 @@ from core.utils import convert_to_ascii, server_responding
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
class Game(object): def process(section, dir_name, input_name=None, status=0, client_agent='manual', input_category=None):
def process(self, section, dir_name, input_name=None, status=0, client_agent='manual', input_category=None): status = int(status)
status = int(status)
cfg = dict(core.CFG[section][input_category]) cfg = dict(core.CFG[section][input_category])
host = cfg["host"] host = cfg["host"]
port = cfg["port"] port = cfg["port"]
apikey = cfg["apikey"] apikey = cfg["apikey"]
library = cfg.get("library") library = cfg.get("library")
ssl = int(cfg.get("ssl", 0)) ssl = int(cfg.get("ssl", 0))
web_root = cfg.get("web_root", "") web_root = cfg.get("web_root", "")
protocol = "https://" if ssl else "http://" protocol = "https://" if ssl else "http://"
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 [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]
input_name, dir_name = convert_to_ascii(input_name, dir_name) input_name, dir_name = convert_to_ascii(input_name, dir_name)
fields = input_name.split("-") fields = input_name.split("-")
gamez_id = fields[0].replace("[", "").replace("]", "").replace(" ", "") gamez_id = fields[0].replace("[", "").replace("]", "").replace(" ", "")
download_status = 'Downloaded' if status == 0 else 'Wanted' download_status = 'Downloaded' if status == 0 else 'Wanted'
params = { params = {
'api_key': apikey, 'api_key': apikey,
'mode': 'UPDATEREQUESTEDSTATUS', 'mode': 'UPDATEREQUESTEDSTATUS',
'db_id': gamez_id, 'db_id': gamez_id,
'status': download_status 'status': download_status
} }
logger.debug("Opening URL: {0}".format(url), section) logger.debug("Opening URL: {0}".format(url), section)
try:
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)]
result = r.json()
logger.postprocess("{0}".format(result), section)
if library:
logger.postprocess("moving files to library: {0}".format(library), section)
try: try:
r = requests.get(url, params=params, verify=False, timeout=(30, 300)) shutil.move(dir_name, os.path.join(library, input_name))
except requests.ConnectionError: except Exception:
logger.error("Unable to open URL") 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 connect to {1}".format(section, section)] return [1, "{0}: Failed to post-process - Unable to move files".format(section)]
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)]
result = r.json() if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
logger.postprocess("{0}".format(result), section) logger.error("Server returned status {0}".format(r.status_code), section)
if library: return [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]
logger.postprocess("moving files to library: {0}".format(library), section) elif result['success']:
try: logger.postprocess("SUCCESS: Status for {0} has been set to {1} in Gamez".format(gamez_id, download_status), section)
shutil.move(dir_name, os.path.join(library, input_name)) return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
except Exception: else:
logger.error("Unable to move {0} to {1}".format(dir_name, os.path.join(library, input_name)), section) logger.error("FAILED: Status for {0} has NOT been updated in Gamez".format(gamez_id), section)
return [1, "{0}: Failed to post-process - Unable to move files".format(section)] return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)]
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)]
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)]
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)]
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)]

View file

@ -14,250 +14,228 @@ from core.utils import convert_to_ascii, find_download, find_imdbid, import_subs
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
class Movie(object): def process(section, dir_name, input_name=None, status=0, client_agent="manual", download_id="", input_category=None, failure_link=None):
def process(self, section, dir_name, input_name=None, status=0, client_agent="manual", download_id="", input_category=None, failure_link=None):
cfg = dict(core.CFG[section][input_category]) cfg = dict(core.CFG[section][input_category])
host = cfg["host"] host = cfg["host"]
port = cfg["port"] port = cfg["port"]
apikey = cfg["apikey"] apikey = cfg["apikey"]
if section == "CouchPotato":
method = cfg["method"]
else:
method = None
# added importMode for Radarr config
if section == "Radarr":
import_mode = cfg.get("importMode", "Move")
else:
import_mode = None
delete_failed = int(cfg["delete_failed"])
wait_for = int(cfg["wait_for"])
ssl = int(cfg.get("ssl", 0))
web_root = cfg.get("web_root", "")
remote_path = int(cfg.get("remote_path", 0))
protocol = "https://" if ssl else "http://"
omdbapikey = cfg.get("omdbapikey", "")
status = int(status)
if status > 0 and core.NOEXTRACTFAILED:
extract = 0
else:
extract = int(cfg.get("extract", 0))
imdbid = find_imdbid(dir_name, input_name, omdbapikey)
if section == "CouchPotato":
base_url = "{0}{1}:{2}{3}/api/{4}/".format(protocol, host, port, web_root, apikey)
if section == "Radarr":
base_url = "{0}{1}:{2}{3}/api/command".format(protocol, host, port, web_root)
url2 = "{0}{1}:{2}{3}/api/config/downloadClient".format(protocol, host, port, web_root)
headers = {'X-Api-Key': apikey}
if not apikey:
logger.info('No CouchPotato or Radarr apikey entered. Performing transcoder functions only')
release = None
elif server_responding(base_url):
if section == "CouchPotato": if section == "CouchPotato":
method = cfg["method"] release = get_release(base_url, imdbid, download_id)
else: else:
method = None
# added importMode for Radarr config
if section == "Radarr":
import_mode = cfg.get("importMode", "Move")
else:
import_mode = None
delete_failed = int(cfg["delete_failed"])
wait_for = int(cfg["wait_for"])
ssl = int(cfg.get("ssl", 0))
web_root = cfg.get("web_root", "")
remote_path = int(cfg.get("remote_path", 0))
protocol = "https://" if ssl else "http://"
omdbapikey = cfg.get("omdbapikey", "")
status = int(status)
if status > 0 and core.NOEXTRACTFAILED:
extract = 0
else:
extract = int(cfg.get("extract", 0))
imdbid = find_imdbid(dir_name, input_name, omdbapikey)
if section == "CouchPotato":
base_url = "{0}{1}:{2}{3}/api/{4}/".format(protocol, host, port, web_root, apikey)
if section == "Radarr":
base_url = "{0}{1}:{2}{3}/api/command".format(protocol, host, port, web_root)
url2 = "{0}{1}:{2}{3}/api/config/downloadClient".format(protocol, host, port, web_root)
headers = {'X-Api-Key': apikey}
if not apikey:
logger.info('No CouchPotato or Radarr apikey entered. Performing transcoder functions only')
release = None release = None
elif server_responding(base_url): else:
if section == "CouchPotato": logger.error("Server did not respond. Exiting", section)
release = self.get_release(base_url, imdbid, download_id) return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]
else:
release = None
else:
logger.error("Server did not respond. Exiting", section)
return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]
# pull info from release found if available # pull info from release found if available
release_id = None release_id = None
media_id = None media_id = None
downloader = None downloader = None
release_status_old = None release_status_old = None
if release: if release:
try: try:
release_id = list(release.keys())[0] release_id = list(release.keys())[0]
media_id = release[release_id]['media_id'] media_id = release[release_id]['media_id']
download_id = release[release_id]['download_info']['id'] download_id = release[release_id]['download_info']['id']
downloader = release[release_id]['download_info']['downloader'] downloader = release[release_id]['download_info']['downloader']
release_status_old = release[release_id]['status'] release_status_old = release[release_id]['status']
except Exception: except Exception:
pass pass
if not os.path.isdir(dir_name) and os.path.isfile(dir_name): # If the input directory is a file, assume single file download and split dir/name. if not os.path.isdir(dir_name) and os.path.isfile(dir_name): # If the input directory is a file, assume single file download and split dir/name.
dir_name = os.path.split(os.path.normpath(dir_name))[0] dir_name = os.path.split(os.path.normpath(dir_name))[0]
specific_path = os.path.join(dir_name, str(input_name)) specific_path = os.path.join(dir_name, str(input_name))
clean_name = os.path.splitext(specific_path) clean_name = os.path.splitext(specific_path)
if clean_name[1] == ".nzb": if clean_name[1] == ".nzb":
specific_path = clean_name[0] specific_path = clean_name[0]
if os.path.isdir(specific_path): if os.path.isdir(specific_path):
dir_name = specific_path dir_name = specific_path
process_all_exceptions(input_name, dir_name) process_all_exceptions(input_name, dir_name)
input_name, dir_name = convert_to_ascii(input_name, dir_name)
if not list_media_files(dir_name, media=True, audio=False, meta=False, archives=False) and list_media_files(dir_name, media=False, audio=False, meta=False, archives=True) and extract:
logger.debug('Checking for archives to extract in directory: {0}'.format(dir_name))
core.extract_files(dir_name)
input_name, dir_name = convert_to_ascii(input_name, dir_name) input_name, dir_name = convert_to_ascii(input_name, dir_name)
if not list_media_files(dir_name, media=True, audio=False, meta=False, archives=False) and list_media_files(dir_name, media=False, audio=False, meta=False, archives=True) and extract: good_files = 0
logger.debug('Checking for archives to extract in directory: {0}'.format(dir_name)) num_files = 0
core.extract_files(dir_name) # Check video files for corruption
input_name, dir_name = convert_to_ascii(input_name, dir_name) for video in list_media_files(dir_name, media=True, audio=False, meta=False, archives=False):
num_files += 1
if transcoder.is_video_good(video, status):
import_subs(video)
good_files += 1
if num_files and good_files == num_files:
if status:
logger.info("Status shown as failed from Downloader, but {0} valid video files found. Setting as success.".format(good_files), section)
status = 0
elif num_files and good_files < num_files:
logger.info("Status shown as success from Downloader, but corrupt video files found. Setting as failed.", section)
if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
print('[NZB] MARK=BAD')
if failure_link:
failure_link += '&corrupt=true'
status = 1
elif client_agent == "manual":
logger.warning("No media files found in directory {0} to manually process.".format(dir_name), section)
return [0, ""] # Success (as far as this script is concerned)
else:
logger.warning("No media files found in directory {0}. Processing this as a failed download".format(dir_name), section)
status = 1
if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
print('[NZB] MARK=BAD')
good_files = 0 if status == 0:
num_files = 0 if core.TRANSCODE == 1:
# Check video files for corruption result, new_dir_name = transcoder.transcode_directory(dir_name)
for video in list_media_files(dir_name, media=True, audio=False, meta=False, archives=False): if result == 0:
num_files += 1 logger.debug("Transcoding succeeded for files in {0}".format(dir_name), section)
if transcoder.is_video_good(video, status): dir_name = new_dir_name
import_subs(video)
good_files += 1
if num_files and good_files == num_files:
if status:
logger.info("Status shown as failed from Downloader, but {0} valid video files found. Setting as success.".format(good_files), section)
status = 0
elif num_files and good_files < num_files:
logger.info("Status shown as success from Downloader, but corrupt video files found. Setting as failed.", section)
if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
print('[NZB] MARK=BAD')
if failure_link:
failure_link += '&corrupt=true'
status = 1
elif client_agent == "manual":
logger.warning("No media files found in directory {0} to manually process.".format(dir_name), section)
return [0, ""] # Success (as far as this script is concerned)
else:
logger.warning("No media files found in directory {0}. Processing this as a failed download".format(dir_name), section)
status = 1
if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
print('[NZB] MARK=BAD')
if status == 0: chmod_directory = int(str(cfg.get("chmodDirectory", "0")), 8)
if core.TRANSCODE == 1: logger.debug("Config setting 'chmodDirectory' currently set to {0}".format(oct(chmod_directory)), section)
result, new_dir_name = transcoder.transcode_directory(dir_name) if chmod_directory:
if result == 0: logger.info("Attempting to set the octal permission of '{0}' on directory '{1}'".format(oct(chmod_directory), dir_name), section)
logger.debug("Transcoding succeeded for files in {0}".format(dir_name), section) core.rchmod(dir_name, chmod_directory)
dir_name = new_dir_name
chmod_directory = int(str(cfg.get("chmodDirectory", "0")), 8)
logger.debug("Config setting 'chmodDirectory' currently set to {0}".format(oct(chmod_directory)), section)
if chmod_directory:
logger.info("Attempting to set the octal permission of '{0}' on directory '{1}'".format(oct(chmod_directory), dir_name), section)
core.rchmod(dir_name, chmod_directory)
else:
logger.error("Transcoding failed for files in {0}".format(dir_name), section)
return [1, "{0}: Failed to post-process - Transcoding failed".format(section)]
for video in list_media_files(dir_name, media=True, audio=False, meta=False, archives=False):
if not release and ".cp(tt" not in video and imdbid:
video_name, video_ext = os.path.splitext(video)
video2 = "{0}.cp({1}){2}".format(video_name, imdbid, video_ext)
if not (client_agent in [core.TORRENT_CLIENTAGENT, 'manual'] and core.USELINK == 'move-sym'):
logger.debug('Renaming: {0} to: {1}'.format(video, video2))
os.rename(video, video2)
if not apikey: # If only using Transcoder functions, exit here.
logger.info('No CouchPotato or Radarr apikey entered. Processing completed.')
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
params = {}
if download_id and release_id:
params['downloader'] = downloader or client_agent
params['download_id'] = download_id
params['media_folder'] = remote_dir(dir_name) if remote_path else dir_name
if section == "CouchPotato":
if method == "manage":
command = "manage.update"
params = {}
else:
command = "renamer.scan"
url = "{0}{1}".format(base_url, command)
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)
logger.postprocess("Starting {0} scan for {1}".format(method, input_name), section)
if section == "Radarr":
payload = {'name': 'DownloadedMoviesScan', 'path': params['media_folder'], 'downloadClientId': download_id, 'importMode': import_mode}
if not download_id:
payload.pop("downloadClientId")
logger.debug("Opening URL: {0} with PARAMS: {1}".format(base_url, payload), section)
logger.postprocess("Starting DownloadedMoviesScan scan for {0}".format(input_name), section)
try:
if section == "CouchPotato":
r = requests.get(url, params=params, verify=False, timeout=(30, 1800))
else:
r = requests.post(base_url, data=json.dumps(payload), headers=headers, stream=True, verify=False, timeout=(30, 1800))
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)]
result = r.json()
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)]
elif section == "CouchPotato" and result['success']:
logger.postprocess("SUCCESS: Finished {0} scan for folder {1}".format(method, dir_name), section)
if method == "manage":
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif section == "Radarr":
logger.postprocess("Radarr response: {0}".format(result['state']))
try:
res = json.loads(r.content)
scan_id = int(res['id'])
logger.debug("Scan started with id: {0}".format(scan_id), section)
started = True
except Exception as e:
logger.warning("No scan id was returned due to: {0}".format(e), section)
scan_id = None
else: else:
logger.error("FAILED: {0} scan was unable to finish for folder {1}. exiting!".format(method, dir_name), logger.error("Transcoding failed for files in {0}".format(dir_name), section)
section) return [1, "{0}: Failed to post-process - Transcoding failed".format(section)]
return [1, "{0}: Failed to post-process - Server did not return success".format(section)] for video in list_media_files(dir_name, media=True, audio=False, meta=False, archives=False):
if not release and ".cp(tt" not in video and imdbid:
video_name, video_ext = os.path.splitext(video)
video2 = "{0}.cp({1}){2}".format(video_name, imdbid, video_ext)
if not (client_agent in [core.TORRENT_CLIENTAGENT, 'manual'] and core.USELINK == 'move-sym'):
logger.debug('Renaming: {0} to: {1}'.format(video, video2))
os.rename(video, video2)
if not apikey: # If only using Transcoder functions, exit here.
logger.info('No CouchPotato or Radarr apikey entered. Processing completed.')
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
params = {}
if download_id and release_id:
params['downloader'] = downloader or client_agent
params['download_id'] = download_id
params['media_folder'] = remote_dir(dir_name) if remote_path else dir_name
if section == "CouchPotato":
if method == "manage":
command = "manage.update"
params = {}
else:
command = "renamer.scan"
url = "{0}{1}".format(base_url, command)
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)
logger.postprocess("Starting {0} scan for {1}".format(method, input_name), section)
if section == "Radarr":
payload = {'name': 'DownloadedMoviesScan', 'path': params['media_folder'], 'downloadClientId': download_id, 'importMode': import_mode}
if not download_id:
payload.pop("downloadClientId")
logger.debug("Opening URL: {0} with PARAMS: {1}".format(base_url, payload), section)
logger.postprocess("Starting DownloadedMoviesScan scan for {0}".format(input_name), section)
try:
if section == "CouchPotato":
r = requests.get(url, params=params, verify=False, timeout=(30, 1800))
else:
r = requests.post(base_url, data=json.dumps(payload), headers=headers, stream=True, verify=False, timeout=(30, 1800))
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)]
result = r.json()
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)]
elif section == "CouchPotato" and result['success']:
logger.postprocess("SUCCESS: Finished {0} scan for folder {1}".format(method, dir_name), section)
if method == "manage":
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif section == "Radarr":
logger.postprocess("Radarr response: {0}".format(result['state']))
try:
res = json.loads(r.content)
scan_id = int(res['id'])
logger.debug("Scan started with id: {0}".format(scan_id), section)
started = True
except Exception as e:
logger.warning("No scan id was returned due to: {0}".format(e), section)
scan_id = None
else: else:
core.FAILED = True logger.error("FAILED: {0} scan was unable to finish for folder {1}. exiting!".format(method, dir_name),
logger.postprocess("FAILED DOWNLOAD DETECTED FOR {0}".format(input_name), section) section)
if failure_link: return [1, "{0}: Failed to post-process - Server did not return success".format(section)]
report_nzb(failure_link, client_agent)
if section == "Radarr": else:
logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(section), section) core.FAILED = True
return [1, "{0}: Download Failed. Sending back to {1}".format(section, section)] # Return as failed to flag this in the downloader. logger.postprocess("FAILED DOWNLOAD DETECTED FOR {0}".format(input_name), section)
if failure_link:
report_nzb(failure_link, client_agent)
if delete_failed and os.path.isdir(dir_name) and not os.path.dirname(dir_name) == dir_name: if section == "Radarr":
logger.postprocess("Deleting failed files and folder {0}".format(dir_name), section) logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(section), section)
remove_dir(dir_name) return [1, "{0}: Download Failed. Sending back to {1}".format(section, section)] # Return as failed to flag this in the downloader.
if not release_id and not media_id: if delete_failed and os.path.isdir(dir_name) and not os.path.dirname(dir_name) == dir_name:
logger.error("Could not find a downloaded movie in the database matching {0}, exiting!".format(input_name), logger.postprocess("Deleting failed files and folder {0}".format(dir_name), section)
section) remove_dir(dir_name)
return [1, "{0}: Failed to post-process - Failed download not found in {1}".format(section, section)]
if release_id: if not release_id and not media_id:
logger.postprocess("Setting failed release {0} to ignored ...".format(input_name), section) logger.error("Could not find a downloaded movie in the database matching {0}, exiting!".format(input_name),
section)
return [1, "{0}: Failed to post-process - Failed download not found in {1}".format(section, section)]
url = "{url}release.ignore".format(url=base_url) if release_id:
params = {'id': release_id} logger.postprocess("Setting failed release {0} to ignored ...".format(input_name), section)
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section) url = "{url}release.ignore".format(url=base_url)
params = {'id': release_id}
try: logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)
r = requests.get(url, params=params, verify=False, timeout=(30, 120))
except requests.ConnectionError:
logger.error("Unable to open URL {0}".format(url), section)
return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
result = r.json()
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)]
elif result['success']:
logger.postprocess("SUCCESS: {0} has been set to ignored ...".format(input_name), section)
else:
logger.warning("FAILED: Unable to set {0} to ignored!".format(input_name), section)
return [1, "{0}: Failed to post-process - Unable to set {1} to ignored".format(section, input_name)]
logger.postprocess("Trying to snatch the next highest ranked release.", section)
url = "{0}movie.searcher.try_next".format(base_url)
logger.debug("Opening URL: {0}".format(url), section)
try: try:
r = requests.get(url, params={'media_id': media_id}, verify=False, timeout=(30, 600)) r = requests.get(url, params=params, verify=False, timeout=(30, 120))
except requests.ConnectionError: except requests.ConnectionError:
logger.error("Unable to open URL {0}".format(url), section) logger.error("Unable to open URL {0}".format(url), section)
return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)] return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
@ -267,200 +245,224 @@ class Movie(object):
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 [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]
elif result['success']: elif result['success']:
logger.postprocess("SUCCESS: Snatched the next highest release ...", section) logger.postprocess("SUCCESS: {0} has been set to ignored ...".format(input_name), section)
return [0, "{0}: Successfully snatched next highest release".format(section)]
else: else:
logger.postprocess("SUCCESS: Unable to find a new release to snatch now. CP will keep searching!", section) logger.warning("FAILED: Unable to set {0} to ignored!".format(input_name), section)
return [0, "{0}: No new release found now. {1} will keep searching".format(section, section)] return [1, "{0}: Failed to post-process - Unable to set {1} to ignored".format(section, input_name)]
# Added a release that was not in the wanted list so confirm rename successful by finding this movie media.list. logger.postprocess("Trying to snatch the next highest ranked release.", section)
if not release:
download_id = None # we don't want to filter new releases based on this.
# we will now check to see if CPS has finished renaming before returning to TorrentToMedia and unpausing. url = "{0}movie.searcher.try_next".format(base_url)
timeout = time.time() + 60 * wait_for logger.debug("Opening URL: {0}".format(url), section)
while time.time() < timeout: # only wait 2 (default) minutes, then return.
logger.postprocess("Checking for status change, please stand by ...", section)
if section == "CouchPotato":
release = self.get_release(base_url, imdbid, download_id, release_id)
scan_id = None
else:
release = None
if release:
try:
release_id = list(release.keys())[0]
title = release[release_id]['title']
release_status_new = release[release_id]['status']
if release_status_old is None: # we didn't have a release before, but now we do.
logger.postprocess("SUCCESS: Movie {0} has now been added to CouchPotato with release status of [{1}]".format(
title, str(release_status_new).upper()), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
if release_status_new != release_status_old:
logger.postprocess("SUCCESS: Release for {0} has now been marked with a status of [{1}]".format(
title, str(release_status_new).upper()), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
except Exception:
pass
elif scan_id:
url = "{0}/{1}".format(base_url, scan_id)
command_status = self.command_complete(url, params, headers, section)
if command_status:
logger.debug("The Scan command return status: {0}".format(command_status), section)
if command_status in ['completed']:
logger.debug("The Scan command has completed successfully. Renaming was successful.", section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status in ['failed']:
logger.debug("The Scan command has failed. Renaming was not successful.", section)
# return [1, "%s: Failed to post-process %s" % (section, input_name) ]
if not os.path.isdir(dir_name):
logger.postprocess("SUCCESS: Input Directory [{0}] has been processed and removed".format(
dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif not list_media_files(dir_name, media=True, audio=False, meta=False, archives=True):
logger.postprocess("SUCCESS: Input Directory [{0}] has no remaining media files. This has been fully processed.".format(
dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
# pause and let CouchPotatoServer/Radarr catch its breath
time.sleep(10 * wait_for)
# The status hasn't changed. we have waited wait_for minutes which is more than enough. uTorrent can resume seeding now.
if section == "Radarr" and self.completed_download_handling(url2, headers, section=section):
logger.debug("The Scan command did not return status completed, but complete Download Handling is enabled. Passing back to {0}.".format(section), section)
return [status, "{0}: Complete DownLoad Handling is enabled. Passing back to {1}".format(section, section)]
logger.warning(
"{0} does not appear to have changed status after {1} minutes, Please check your logs.".format(input_name, wait_for),
section)
return [1, "{0}: Failed to post-process - No change in status".format(section)]
def command_complete(self, 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={'media_id': media_id}, verify=False, timeout=(30, 600))
except requests.ConnectionError: except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url), section) logger.error("Unable to open URL {0}".format(url), section)
return None return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
result = r.json()
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 None return [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]
elif result['success']:
logger.postprocess("SUCCESS: Snatched the next highest release ...", section)
return [0, "{0}: Successfully snatched next highest release".format(section)]
else: else:
try: logger.postprocess("SUCCESS: Unable to find a new release to snatch now. CP will keep searching!", section)
return r.json()['state'] return [0, "{0}: No new release found now. {1} will keep searching".format(section, section)]
except (ValueError, KeyError):
# ValueError catches simplejson's JSONDecodeError and json's ValueError
logger.error("{0} did not return expected json data.".format(section), section)
return None
def completed_download_handling(self, url2, headers, section="MAIN"): # Added a release that was not in the wanted list so confirm rename successful by finding this movie media.list.
try: if not release:
r = requests.get(url2, params={}, headers=headers, stream=True, verify=False, timeout=(30, 60)) download_id = None # we don't want to filter new releases based on this.
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url2), section) # we will now check to see if CPS has finished renaming before returning to TorrentToMedia and unpausing.
return False timeout = time.time() + 60 * wait_for
if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]: while time.time() < timeout: # only wait 2 (default) minutes, then return.
logger.error("Server returned status {0}".format(r.status_code), section) logger.postprocess("Checking for status change, please stand by ...", section)
return False if section == "CouchPotato":
release = get_release(base_url, imdbid, download_id, release_id)
scan_id = None
else: else:
release = None
if release:
try: try:
return r.json().get("enableCompletedDownloadHandling", False) release_id = list(release.keys())[0]
except ValueError: title = release[release_id]['title']
# ValueError catches simplejson's JSONDecodeError and json's ValueError release_status_new = release[release_id]['status']
return False if release_status_old is None: # we didn't have a release before, but now we do.
logger.postprocess("SUCCESS: Movie {0} has now been added to CouchPotato with release status of [{1}]".format(
title, str(release_status_new).upper()), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
def get_release(self, base_url, imdb_id=None, download_id=None, release_id=None): if release_status_new != release_status_old:
results = {} logger.postprocess("SUCCESS: Release for {0} has now been marked with a status of [{1}]".format(
params = {} title, str(release_status_new).upper()), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
# determine cmd and params to send to CouchPotato to get our results
section = 'movies'
cmd = "media.list"
if release_id or imdb_id:
section = 'media'
cmd = "media.get"
params['id'] = release_id or imdb_id
if not (release_id or imdb_id or download_id):
logger.debug("No information available to filter CP results")
return results
url = "{0}{1}".format(base_url, cmd)
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params))
try:
r = requests.get(url, params=params, verify=False, timeout=(30, 60))
except requests.ConnectionError:
logger.error("Unable to open URL {0}".format(url))
return results
try:
result = r.json()
except ValueError:
# ValueError catches simplejson's JSONDecodeError and json's ValueError
logger.error("CouchPotato returned the following non-json data")
for line in r.iter_lines():
logger.error("{0}".format(line))
return results
if not result['success']:
if 'error' in result:
logger.error('{0}'.format(result['error']))
else:
logger.error("no media found for id {0}".format(params['id']))
return results
# Gather release info and return it back, no need to narrow results
if release_id:
try:
cur_id = result[section]['_id']
results[cur_id] = result[section]
return results
except Exception: except Exception:
pass pass
elif scan_id:
url = "{0}/{1}".format(base_url, scan_id)
command_status = command_complete(url, params, headers, section)
if command_status:
logger.debug("The Scan command return status: {0}".format(command_status), section)
if command_status in ['completed']:
logger.debug("The Scan command has completed successfully. Renaming was successful.", section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status in ['failed']:
logger.debug("The Scan command has failed. Renaming was not successful.", section)
# return [1, "%s: Failed to post-process %s" % (section, input_name) ]
# Gather release info and proceed with trying to narrow results to one release choice if not os.path.isdir(dir_name):
logger.postprocess("SUCCESS: Input Directory [{0}] has been processed and removed".format(
dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
movies = result[section] elif not list_media_files(dir_name, media=True, audio=False, meta=False, archives=True):
if not isinstance(movies, list): logger.postprocess("SUCCESS: Input Directory [{0}] has no remaining media files. This has been fully processed.".format(
movies = [movies] dir_name), section)
for movie in movies: return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
if movie['status'] not in ['active', 'done']:
continue
releases = movie['releases']
if not releases:
continue
for release in releases:
try:
if release['status'] not in ['snatched', 'downloaded', 'done']:
continue
if download_id:
if download_id.lower() != release['download_info']['id'].lower():
continue
cur_id = release['_id'] # pause and let CouchPotatoServer/Radarr catch its breath
results[cur_id] = release time.sleep(10 * wait_for)
results[cur_id]['title'] = movie['title']
except Exception:
continue
# Narrow results by removing old releases by comparing their last_edit field # The status hasn't changed. we have waited wait_for minutes which is more than enough. uTorrent can resume seeding now.
if len(results) > 1: if section == "Radarr" and completed_download_handling(url2, headers, section=section):
for id1, x1 in results.items(): logger.debug("The Scan command did not return status completed, but complete Download Handling is enabled. Passing back to {0}.".format(section), section)
for id2, x2 in results.items(): return [status, "{0}: Complete DownLoad Handling is enabled. Passing back to {1}".format(section, section)]
try: logger.warning(
if x2["last_edit"] > x1["last_edit"]: "{0} does not appear to have changed status after {1} minutes, Please check your logs.".format(input_name, wait_for),
results.pop(id1) section)
except Exception: return [1, "{0}: Failed to post-process - No change in status".format(section)]
continue
# Search downloads on clients for a match to try and narrow our results down to 1
if len(results) > 1:
for cur_id, x in results.items():
try:
if not find_download(str(x['download_info']['downloader']).lower(), x['download_info']['id']):
results.pop(cur_id)
except Exception:
continue
def command_complete(url, params, headers, section):
try:
r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url), section)
return None
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 None
else:
try:
return r.json()['state']
except (ValueError, KeyError):
# ValueError catches simplejson's JSONDecodeError and json's ValueError
logger.error("{0} did not return expected json data.".format(section), section)
return None
def completed_download_handling(url2, headers, section="MAIN"):
try:
r = requests.get(url2, params={}, headers=headers, stream=True, verify=False, timeout=(30, 60))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url2), section)
return False
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 False
else:
try:
return r.json().get("enableCompletedDownloadHandling", False)
except ValueError:
# ValueError catches simplejson's JSONDecodeError and json's ValueError
return False
def get_release(base_url, imdb_id=None, download_id=None, release_id=None):
results = {}
params = {}
# determine cmd and params to send to CouchPotato to get our results
section = 'movies'
cmd = "media.list"
if release_id or imdb_id:
section = 'media'
cmd = "media.get"
params['id'] = release_id or imdb_id
if not (release_id or imdb_id or download_id):
logger.debug("No information available to filter CP results")
return results return results
url = "{0}{1}".format(base_url, cmd)
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params))
try:
r = requests.get(url, params=params, verify=False, timeout=(30, 60))
except requests.ConnectionError:
logger.error("Unable to open URL {0}".format(url))
return results
try:
result = r.json()
except ValueError:
# ValueError catches simplejson's JSONDecodeError and json's ValueError
logger.error("CouchPotato returned the following non-json data")
for line in r.iter_lines():
logger.error("{0}".format(line))
return results
if not result['success']:
if 'error' in result:
logger.error('{0}'.format(result['error']))
else:
logger.error("no media found for id {0}".format(params['id']))
return results
# Gather release info and return it back, no need to narrow results
if release_id:
try:
cur_id = result[section]['_id']
results[cur_id] = result[section]
return results
except Exception:
pass
# Gather release info and proceed with trying to narrow results to one release choice
movies = result[section]
if not isinstance(movies, list):
movies = [movies]
for movie in movies:
if movie['status'] not in ['active', 'done']:
continue
releases = movie['releases']
if not releases:
continue
for release in releases:
try:
if release['status'] not in ['snatched', 'downloaded', 'done']:
continue
if download_id:
if download_id.lower() != release['download_info']['id'].lower():
continue
cur_id = release['_id']
results[cur_id] = release
results[cur_id]['title'] = movie['title']
except Exception:
continue
# Narrow results by removing old releases by comparing their last_edit field
if len(results) > 1:
for id1, x1 in results.items():
for id2, x2 in results.items():
try:
if x2["last_edit"] > x1["last_edit"]:
results.pop(id1)
except Exception:
continue
# Search downloads on clients for a match to try and narrow our results down to 1
if len(results) > 1:
for cur_id, x in results.items():
try:
if not find_download(str(x['download_info']['downloader']).lower(), x['download_info']['id']):
results.pop(cur_id)
except Exception:
continue
return results

View file

@ -14,226 +14,228 @@ from core.utils import convert_to_ascii, list_media_files, remote_dir, remove_di
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
class Music(object): def process(section, dir_name, input_name=None, status=0, client_agent="manual", input_category=None):
def process(self, section, dir_name, input_name=None, status=0, client_agent="manual", input_category=None): status = int(status)
status = int(status)
cfg = dict(core.CFG[section][input_category]) cfg = dict(core.CFG[section][input_category])
host = cfg["host"] host = cfg["host"]
port = cfg["port"] port = cfg["port"]
apikey = cfg["apikey"] apikey = cfg["apikey"]
wait_for = int(cfg["wait_for"]) wait_for = int(cfg["wait_for"])
ssl = int(cfg.get("ssl", 0)) ssl = int(cfg.get("ssl", 0))
delete_failed = int(cfg["delete_failed"]) delete_failed = int(cfg["delete_failed"])
web_root = cfg.get("web_root", "") web_root = cfg.get("web_root", "")
remote_path = int(cfg.get("remote_path", 0)) remote_path = int(cfg.get("remote_path", 0))
protocol = "https://" if ssl else "http://" protocol = "https://" if ssl else "http://"
status = int(status) status = int(status)
if status > 0 and core.NOEXTRACTFAILED: if status > 0 and core.NOEXTRACTFAILED:
extract = 0 extract = 0
else: else:
extract = int(cfg.get("extract", 0)) extract = int(cfg.get("extract", 0))
if section == "Lidarr": if section == "Lidarr":
url = "{0}{1}:{2}{3}/api/v1".format(protocol, host, port, web_root) url = "{0}{1}:{2}{3}/api/v1".format(protocol, host, port, web_root)
else: else:
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 [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]
if not os.path.isdir(dir_name) and os.path.isfile(dir_name): # If the input directory is a file, assume single file download and split dir/name. if not os.path.isdir(dir_name) and os.path.isfile(dir_name): # If the input directory is a file, assume single file download and split dir/name.
dir_name = os.path.split(os.path.normpath(dir_name))[0] dir_name = os.path.split(os.path.normpath(dir_name))[0]
specific_path = os.path.join(dir_name, str(input_name)) specific_path = os.path.join(dir_name, str(input_name))
clean_name = os.path.splitext(specific_path) clean_name = os.path.splitext(specific_path)
if clean_name[1] == ".nzb": if clean_name[1] == ".nzb":
specific_path = clean_name[0] specific_path = clean_name[0]
if os.path.isdir(specific_path): if os.path.isdir(specific_path):
dir_name = specific_path dir_name = specific_path
process_all_exceptions(input_name, dir_name) process_all_exceptions(input_name, dir_name)
input_name, dir_name = convert_to_ascii(input_name, dir_name)
if not list_media_files(dir_name, media=False, audio=True, meta=False, archives=False) and list_media_files(dir_name, media=False, audio=False, meta=False, archives=True) and extract:
logger.debug('Checking for archives to extract in directory: {0}'.format(dir_name))
core.extract_files(dir_name)
input_name, dir_name = convert_to_ascii(input_name, dir_name) input_name, dir_name = convert_to_ascii(input_name, dir_name)
if not list_media_files(dir_name, media=False, audio=True, meta=False, archives=False) and list_media_files(dir_name, media=False, audio=False, meta=False, archives=True) and extract: # if listMediaFiles(dir_name, media=False, audio=True, meta=False, archives=False) and status:
logger.debug('Checking for archives to extract in directory: {0}'.format(dir_name)) # logger.info("Status shown as failed from Downloader, but valid video files found. Setting as successful.", section)
core.extract_files(dir_name) # status = 0
input_name, dir_name = convert_to_ascii(input_name, dir_name)
# if listMediaFiles(dir_name, media=False, audio=True, meta=False, archives=False) and status: if status == 0 and section == "HeadPhones":
# logger.info("Status shown as failed from Downloader, but valid video files found. Setting as successful.", section)
# status = 0
if status == 0 and section == "HeadPhones":
params = {
'apikey': apikey,
'cmd': "forceProcess",
'dir': remote_dir(dir_name) if remote_path else dir_name
}
res = self.force_process(params, url, apikey, input_name, dir_name, section, wait_for)
if res[0] in [0, 1]:
return res
params = {
'apikey': apikey,
'cmd': "forceProcess",
'dir': os.path.split(remote_dir(dir_name))[0] if remote_path else os.path.split(dir_name)[0]
}
res = self.force_process(params, url, apikey, input_name, dir_name, section, wait_for)
if res[0] in [0, 1]:
return res
# The status hasn't changed. uTorrent can resume seeding now.
logger.warning("The music album does not appear to have changed status after {0} minutes. Please check your Logs".format(wait_for), section)
return [1, "{0}: Failed to post-process - No change in wanted status".format(section)]
elif status == 0 and section == "Lidarr":
url = "{0}{1}:{2}{3}/api/v1/command".format(protocol, host, port, web_root)
headers = {"X-Api-Key": apikey}
if remote_path:
logger.debug("remote_path: {0}".format(remote_dir(dir_name)), section)
data = {"name": "Rename", "path": remote_dir(dir_name)}
else:
logger.debug("path: {0}".format(dir_name), section)
data = {"name": "Rename", "path": dir_name}
data = json.dumps(data)
try:
logger.debug("Opening URL: {0} with data: {1}".format(url, data), section)
r = requests.post(url, data=data, headers=headers, stream=True, verify=False, timeout=(30, 1800))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url), section)
return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
success = False
queued = False
started = False
try:
res = json.loads(r.content)
scan_id = int(res['id'])
logger.debug("Scan started with id: {0}".format(scan_id), section)
started = True
except Exception as e:
logger.warning("No scan id was returned due to: {0}".format(e), section)
scan_id = None
started = False
return [1, "{0}: Failed to post-process - Unable to start scan".format(section)]
n = 0
params = {}
url = "{0}/{1}".format(url, scan_id)
while n < 6: # set up wait_for minutes to see if command completes..
time.sleep(10 * wait_for)
command_status = self.command_complete(url, params, headers, section)
if command_status and command_status in ['completed', 'failed']:
break
n += 1
if command_status:
logger.debug("The Scan command return status: {0}".format(command_status), section)
if not os.path.exists(dir_name):
logger.debug("The directory {0} has been removed. Renaming was successful.".format(dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status and command_status in ['completed']:
logger.debug("The Scan command has completed successfully. Renaming was successful.", section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status and command_status in ['failed']:
logger.debug("The Scan command has failed. Renaming was not successful.", section)
# return [1, "%s: Failed to post-process %s" % (section, input_name) ]
else:
logger.debug("The Scan command did not return status completed. Passing back to {0} to attempt complete download handling.".format(section), section)
return [status, "{0}: Passing back to {1} to attempt Complete Download Handling".format(section, section)]
else:
if section == "Lidarr":
logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(section), section)
return [1, "{0}: Download Failed. Sending back to {1}".format(section, section)] # Return as failed to flag this in the downloader.
else:
logger.warning("FAILED DOWNLOAD DETECTED", section)
if delete_failed and os.path.isdir(dir_name) and not os.path.dirname(dir_name) == dir_name:
logger.postprocess("Deleting failed files and folder {0}".format(dir_name), section)
remove_dir(dir_name)
return [1, "{0}: Failed to post-process. {1} does not support failed downloads".format(section, section)] # Return as failed to flag this in the downloader.
def command_complete(self, url, params, headers, section):
try:
r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url), section)
return None
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 None
else:
try:
return r.json()['state']
except (ValueError, KeyError):
# ValueError catches simplejson's JSONDecodeError and json's ValueError
logger.error("{0} did not return expected json data.".format(section), section)
return None
def get_status(self, url, apikey, dir_name):
logger.debug("Attempting to get current status for release:{0}".format(os.path.basename(dir_name)))
params = { params = {
'apikey': apikey, 'apikey': apikey,
'cmd': "getHistory" 'cmd': "forceProcess",
'dir': remote_dir(dir_name) if remote_path else dir_name
} }
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params)) res = force_process(params, url, apikey, input_name, dir_name, section, wait_for)
if res[0] in [0, 1]:
return res
params = {
'apikey': apikey,
'cmd': "forceProcess",
'dir': os.path.split(remote_dir(dir_name))[0] if remote_path else os.path.split(dir_name)[0]
}
res = force_process(params, url, apikey, input_name, dir_name, section, wait_for)
if res[0] in [0, 1]:
return res
# The status hasn't changed. uTorrent can resume seeding now.
logger.warning("The music album does not appear to have changed status after {0} minutes. Please check your Logs".format(wait_for), section)
return [1, "{0}: Failed to post-process - No change in wanted status".format(section)]
elif status == 0 and section == "Lidarr":
url = "{0}{1}:{2}{3}/api/v1/command".format(protocol, host, port, web_root)
headers = {"X-Api-Key": apikey}
if remote_path:
logger.debug("remote_path: {0}".format(remote_dir(dir_name)), section)
data = {"name": "Rename", "path": remote_dir(dir_name)}
else:
logger.debug("path: {0}".format(dir_name), section)
data = {"name": "Rename", "path": dir_name}
data = json.dumps(data)
try: try:
r = requests.get(url, params=params, verify=False, timeout=(30, 120)) logger.debug("Opening URL: {0} with data: {1}".format(url, data), section)
except requests.RequestException: r = requests.post(url, data=data, headers=headers, stream=True, verify=False, timeout=(30, 1800))
logger.error("Unable to open URL")
return None
try:
result = r.json()
except ValueError:
# ValueError catches simplejson's JSONDecodeError and json's ValueError
return None
for album in result:
if os.path.basename(dir_name) == album['FolderName']:
return album["Status"].lower()
def force_process(self, params, url, apikey, input_name, dir_name, section, wait_for):
release_status = self.get_status(url, apikey, dir_name)
if not release_status:
logger.error("Could not find a status for {0}, is it in the wanted list ?".format(input_name), section)
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)
try:
r = requests.get(url, params=params, verify=False, timeout=(30, 300))
except requests.ConnectionError: except requests.ConnectionError:
logger.error("Unable to open URL {0}".format(url), section) logger.error("Unable to open URL: {0}".format(url), section)
return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)] return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
logger.debug("Result: {0}".format(r.text), section) success = False
queued = False
started = False
try:
res = json.loads(r.content)
scan_id = int(res['id'])
logger.debug("Scan started with id: {0}".format(scan_id), section)
started = True
except Exception as e:
logger.warning("No scan id was returned due to: {0}".format(e), section)
scan_id = None
started = False
return [1, "{0}: Failed to post-process - Unable to start scan".format(section)]
if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]: n = 0
logger.error("Server returned status {0}".format(r.status_code), section) params = {}
return [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)] url = "{0}/{1}".format(url, scan_id)
elif r.text == "OK": while n < 6: # set up wait_for minutes to see if command completes..
logger.postprocess("SUCCESS: Post-Processing started for {0} in folder {1} ...".format(input_name, dir_name), section)
else:
logger.error("FAILED: Post-Processing has NOT started for {0} in folder {1}. exiting!".format(input_name, dir_name), section)
return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)]
# we will now wait for this album to be processed before returning to TorrentToMedia and unpausing.
timeout = time.time() + 60 * wait_for
while time.time() < timeout:
current_status = self.get_status(url, apikey, dir_name)
if current_status is not None and current_status != release_status: # Something has changed. CPS must have processed this movie.
logger.postprocess("SUCCESS: This release is now marked as status [{0}]".format(current_status), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
if not os.path.isdir(dir_name):
logger.postprocess("SUCCESS: The input directory {0} has been removed Processing must have finished.".format(dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
time.sleep(10 * wait_for) time.sleep(10 * wait_for)
# The status hasn't changed. command_status = command_complete(url, params, headers, section)
return [2, "no change"] if command_status and command_status in ['completed', 'failed']:
break
n += 1
if command_status:
logger.debug("The Scan command return status: {0}".format(command_status), section)
if not os.path.exists(dir_name):
logger.debug("The directory {0} has been removed. Renaming was successful.".format(dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status and command_status in ['completed']:
logger.debug("The Scan command has completed successfully. Renaming was successful.", section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status and command_status in ['failed']:
logger.debug("The Scan command has failed. Renaming was not successful.", section)
# return [1, "%s: Failed to post-process %s" % (section, input_name) ]
else:
logger.debug("The Scan command did not return status completed. Passing back to {0} to attempt complete download handling.".format(section), section)
return [status, "{0}: Passing back to {1} to attempt Complete Download Handling".format(section, section)]
else:
if section == "Lidarr":
logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(section), section)
return [1, "{0}: Download Failed. Sending back to {1}".format(section, section)] # Return as failed to flag this in the downloader.
else:
logger.warning("FAILED DOWNLOAD DETECTED", section)
if delete_failed and os.path.isdir(dir_name) and not os.path.dirname(dir_name) == dir_name:
logger.postprocess("Deleting failed files and folder {0}".format(dir_name), section)
remove_dir(dir_name)
return [1, "{0}: Failed to post-process. {1} does not support failed downloads".format(section, section)] # Return as failed to flag this in the downloader.
def command_complete(url, params, headers, section):
try:
r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url), section)
return None
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 None
else:
try:
return r.json()['state']
except (ValueError, KeyError):
# ValueError catches simplejson's JSONDecodeError and json's ValueError
logger.error("{0} did not return expected json data.".format(section), section)
return None
def get_status(url, apikey, dir_name):
logger.debug("Attempting to get current status for release:{0}".format(os.path.basename(dir_name)))
params = {
'apikey': apikey,
'cmd': "getHistory"
}
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params))
try:
r = requests.get(url, params=params, verify=False, timeout=(30, 120))
except requests.RequestException:
logger.error("Unable to open URL")
return None
try:
result = r.json()
except ValueError:
# ValueError catches simplejson's JSONDecodeError and json's ValueError
return None
for album in result:
if os.path.basename(dir_name) == album['FolderName']:
return album["Status"].lower()
def force_process(params, url, apikey, input_name, dir_name, section, wait_for):
release_status = get_status(url, apikey, dir_name)
if not release_status:
logger.error("Could not find a status for {0}, is it in the wanted list ?".format(input_name), section)
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)
try:
r = requests.get(url, params=params, verify=False, timeout=(30, 300))
except requests.ConnectionError:
logger.error("Unable to open URL {0}".format(url), section)
return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
logger.debug("Result: {0}".format(r.text), section)
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)]
elif r.text == "OK":
logger.postprocess("SUCCESS: Post-Processing started for {0} in folder {1} ...".format(input_name, dir_name), section)
else:
logger.error("FAILED: Post-Processing has NOT started for {0} in folder {1}. exiting!".format(input_name, dir_name), section)
return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)]
# we will now wait for this album to be processed before returning to TorrentToMedia and unpausing.
timeout = time.time() + 60 * wait_for
while time.time() < timeout:
current_status = get_status(url, apikey, dir_name)
if current_status is not None and current_status != release_status: # Something has changed. CPS must have processed this movie.
logger.postprocess("SUCCESS: This release is now marked as status [{0}]".format(current_status), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
if not os.path.isdir(dir_name):
logger.postprocess("SUCCESS: The input directory {0} has been removed Processing must have finished.".format(dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
time.sleep(10 * wait_for)
# The status hasn't changed.
return [2, "no change"]

View file

@ -17,358 +17,359 @@ from core.utils import convert_to_ascii, flatten, import_subs, list_media_files,
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()
class TV(object): def process(section, dir_name, input_name=None, failed=False, client_agent="manual", download_id=None, input_category=None, failure_link=None):
def process(self, section, dir_name, input_name=None, failed=False, client_agent="manual", download_id=None, input_category=None, failure_link=None):
cfg = dict(core.CFG[section][input_category]) cfg = dict(core.CFG[section][input_category])
host = cfg["host"] host = cfg["host"]
port = cfg["port"] port = cfg["port"]
ssl = int(cfg.get("ssl", 0)) ssl = int(cfg.get("ssl", 0))
web_root = cfg.get("web_root", "") web_root = cfg.get("web_root", "")
protocol = "https://" if ssl else "http://" protocol = "https://" if ssl else "http://"
username = cfg.get("username", "") username = cfg.get("username", "")
password = cfg.get("password", "") password = cfg.get("password", "")
apikey = cfg.get("apikey", "") apikey = cfg.get("apikey", "")
if server_responding("{0}{1}:{2}{3}".format(protocol, host, port, web_root)): if server_responding("{0}{1}:{2}{3}".format(protocol, host, port, web_root)):
# auto-detect correct fork # auto-detect correct fork
fork, fork_params = auto_fork(section, input_category) fork, fork_params = auto_fork(section, input_category)
elif not username and not apikey: elif not username and not apikey:
logger.info('No SickBeard username or Sonarr apikey entered. Performing transcoder functions only') logger.info('No SickBeard username or Sonarr apikey entered. Performing transcoder functions only')
fork, fork_params = "None", {} fork, fork_params = "None", {}
else: else:
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 [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]
delete_failed = int(cfg.get("delete_failed", 0)) delete_failed = int(cfg.get("delete_failed", 0))
nzb_extraction_by = cfg.get("nzbExtractionBy", "Downloader") nzb_extraction_by = cfg.get("nzbExtractionBy", "Downloader")
process_method = cfg.get("process_method") process_method = cfg.get("process_method")
if client_agent == core.TORRENT_CLIENTAGENT and core.USELINK == "move-sym": if client_agent == core.TORRENT_CLIENTAGENT and core.USELINK == "move-sym":
process_method = "symlink" process_method = "symlink"
remote_path = int(cfg.get("remote_path", 0)) remote_path = int(cfg.get("remote_path", 0))
wait_for = int(cfg.get("wait_for", 2)) wait_for = int(cfg.get("wait_for", 2))
force = int(cfg.get("force", 0)) force = int(cfg.get("force", 0))
delete_on = int(cfg.get("delete_on", 0)) delete_on = int(cfg.get("delete_on", 0))
ignore_subs = int(cfg.get("ignore_subs", 0)) ignore_subs = int(cfg.get("ignore_subs", 0))
status = int(failed) status = int(failed)
if status > 0 and core.NOEXTRACTFAILED: if status > 0 and core.NOEXTRACTFAILED:
extract = 0 extract = 0
else: else:
extract = int(cfg.get("extract", 0)) extract = int(cfg.get("extract", 0))
# get importmode, default to "Move" for consistency with legacy # get importmode, default to "Move" for consistency with legacy
import_mode = cfg.get("importMode", "Move") import_mode = cfg.get("importMode", "Move")
if not os.path.isdir(dir_name) and os.path.isfile(dir_name): # If the input directory is a file, assume single file download and split dir/name. if not os.path.isdir(dir_name) and os.path.isfile(dir_name): # If the input directory is a file, assume single file download and split dir/name.
dir_name = os.path.split(os.path.normpath(dir_name))[0] dir_name = os.path.split(os.path.normpath(dir_name))[0]
specific_path = os.path.join(dir_name, str(input_name)) specific_path = os.path.join(dir_name, str(input_name))
clean_name = os.path.splitext(specific_path) clean_name = os.path.splitext(specific_path)
if clean_name[1] == ".nzb": if clean_name[1] == ".nzb":
specific_path = clean_name[0] specific_path = clean_name[0]
if os.path.isdir(specific_path): if os.path.isdir(specific_path):
dir_name = specific_path dir_name = specific_path
# Attempt to create the directory if it doesn't exist and ignore any # Attempt to create the directory if it doesn't exist and ignore any
# error stating that it already exists. This fixes a bug where SickRage # error stating that it already exists. This fixes a bug where SickRage
# won't process the directory because it doesn't exist. # won't process the directory because it doesn't exist.
try: try:
os.makedirs(dir_name) # Attempt to create the directory os.makedirs(dir_name) # Attempt to create the directory
except OSError as e: except OSError as e:
# Re-raise the error if it wasn't about the directory not existing # Re-raise the error if it wasn't about the directory not existing
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
raise raise
if 'process_method' not in fork_params or (client_agent in ['nzbget', 'sabnzbd'] and nzb_extraction_by != "Destination"): if 'process_method' not in fork_params or (client_agent in ['nzbget', 'sabnzbd'] and nzb_extraction_by != "Destination"):
if input_name: if input_name:
process_all_exceptions(input_name, dir_name) process_all_exceptions(input_name, dir_name)
input_name, dir_name = convert_to_ascii(input_name, dir_name)
# Now check if tv files exist in destination.
if not list_media_files(dir_name, media=True, audio=False, meta=False, archives=False):
if list_media_files(dir_name, media=False, audio=False, meta=False, archives=True) and extract:
logger.debug('Checking for archives to extract in directory: {0}'.format(dir_name))
core.extract_files(dir_name)
input_name, dir_name = convert_to_ascii(input_name, dir_name) input_name, dir_name = convert_to_ascii(input_name, dir_name)
# Now check if tv files exist in destination. if list_media_files(dir_name, media=True, audio=False, meta=False, archives=False): # Check that a video exists. if not, assume failed.
if not list_media_files(dir_name, media=True, audio=False, meta=False, archives=False): flatten(dir_name)
if list_media_files(dir_name, media=False, audio=False, meta=False, archives=True) and extract:
logger.debug('Checking for archives to extract in directory: {0}'.format(dir_name))
core.extract_files(dir_name)
input_name, dir_name = convert_to_ascii(input_name, dir_name)
if list_media_files(dir_name, media=True, audio=False, meta=False, archives=False): # Check that a video exists. if not, assume failed. # Check video files for corruption
flatten(dir_name) good_files = 0
num_files = 0
# Check video files for corruption for video in list_media_files(dir_name, media=True, audio=False, meta=False, archives=False):
good_files = 0 num_files += 1
num_files = 0 if transcoder.is_video_good(video, status):
for video in list_media_files(dir_name, media=True, audio=False, meta=False, archives=False): good_files += 1
num_files += 1 import_subs(video)
if transcoder.is_video_good(video, status): if num_files > 0:
good_files += 1 if good_files == num_files and not status == 0:
import_subs(video) logger.info('Found Valid Videos. Setting status Success')
if num_files > 0: status = 0
if good_files == num_files and not status == 0: failed = 0
logger.info('Found Valid Videos. Setting status Success') if good_files < num_files and status == 0:
status = 0 logger.info('Found corrupt videos. Setting status Failed')
failed = 0
if good_files < num_files and status == 0:
logger.info('Found corrupt videos. Setting status Failed')
status = 1
failed = 1
if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
print('[NZB] MARK=BAD')
if failure_link:
failure_link += '&corrupt=true'
elif client_agent == "manual":
logger.warning("No media files found in directory {0} to manually process.".format(dir_name), section)
return [0, ""] # Success (as far as this script is concerned)
elif nzb_extraction_by == "Destination":
logger.info("Check for media files ignored because nzbExtractionBy is set to Destination.")
if int(failed) == 0:
logger.info("Setting Status Success.")
status = 0
failed = 0
else:
logger.info("Downloader reported an error during download or verification. Processing this as a failed download.")
status = 1
failed = 1
else:
logger.warning("No media files found in directory {0}. Processing this as a failed download".format(dir_name), section)
status = 1 status = 1
failed = 1 failed = 1
if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0': if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
print('[NZB] MARK=BAD') print('[NZB] MARK=BAD')
if status == 0 and core.TRANSCODE == 1: # only transcode successful downloads
result, new_dir_name = transcoder.transcode_directory(dir_name)
if result == 0:
logger.debug("SUCCESS: Transcoding succeeded for files in {0}".format(dir_name), section)
dir_name = new_dir_name
chmod_directory = int(str(cfg.get("chmodDirectory", "0")), 8)
logger.debug("Config setting 'chmodDirectory' currently set to {0}".format(oct(chmod_directory)), section)
if chmod_directory:
logger.info("Attempting to set the octal permission of '{0}' on directory '{1}'".format(oct(chmod_directory), dir_name), section)
core.rchmod(dir_name, chmod_directory)
else:
logger.error("FAILED: Transcoding failed for files in {0}".format(dir_name), section)
return [1, "{0}: Failed to post-process - Transcoding failed".format(section)]
# configure SB params to pass
fork_params['quiet'] = 1
fork_params['proc_type'] = 'manual'
if input_name is not None:
fork_params['nzbName'] = input_name
for param in copy.copy(fork_params):
if param == "failed":
fork_params[param] = failed
del fork_params['proc_type']
if "type" in fork_params:
del fork_params['type']
if param == "return_data":
fork_params[param] = 0
del fork_params['quiet']
if param == "type":
fork_params[param] = 'manual'
if "proc_type" in fork_params:
del fork_params['proc_type']
if param in ["dir_name", "dir", "proc_dir", "process_directory", "path"]:
fork_params[param] = dir_name
if remote_path:
fork_params[param] = remote_dir(dir_name)
if param == "process_method":
if process_method:
fork_params[param] = process_method
else:
del fork_params[param]
if param in ["force", "force_replace"]:
if force:
fork_params[param] = force
else:
del fork_params[param]
if param in ["delete_on", "delete"]:
if delete_on:
fork_params[param] = delete_on
else:
del fork_params[param]
if param == "ignore_subs":
if ignore_subs:
fork_params[param] = ignore_subs
else:
del fork_params[param]
if param == "force_next":
fork_params[param] = 1
# delete any unused params so we don't pass them to SB by mistake
[fork_params.pop(k) for k, v in fork_params.items() if v is None]
if status == 0:
if section == "NzbDrone" and not apikey:
logger.info('No Sonarr apikey entered. Processing completed.')
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
logger.postprocess("SUCCESS: The download succeeded, sending a post-process request", section)
else:
core.FAILED = True
if failure_link: if failure_link:
report_nzb(failure_link, client_agent) failure_link += '&corrupt=true'
if 'failed' in fork_params: elif client_agent == "manual":
logger.postprocess("FAILED: The download failed. Sending 'failed' process request to {0} branch".format(fork), section) logger.warning("No media files found in directory {0} to manually process.".format(dir_name), section)
elif section == "NzbDrone": return [0, ""] # Success (as far as this script is concerned)
logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(fork), section) elif nzb_extraction_by == "Destination":
return [1, "{0}: Download Failed. Sending back to {1}".format(section, section)] # Return as failed to flag this in the downloader. logger.info("Check for media files ignored because nzbExtractionBy is set to Destination.")
else: if int(failed) == 0:
logger.postprocess("FAILED: The download failed. {0} branch does not handle failed downloads. Nothing to process".format(fork), section) logger.info("Setting Status Success.")
if delete_failed and os.path.isdir(dir_name) and not os.path.dirname(dir_name) == dir_name: status = 0
logger.postprocess("Deleting failed files and folder {0}".format(dir_name), section) failed = 0
remove_dir(dir_name) else:
return [1, "{0}: Failed to post-process. {1} does not support failed downloads".format(section, section)] # Return as failed to flag this in the downloader. logger.info("Downloader reported an error during download or verification. Processing this as a failed download.")
status = 1
failed = 1
else:
logger.warning("No media files found in directory {0}. Processing this as a failed download".format(dir_name), section)
status = 1
failed = 1
if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
print('[NZB] MARK=BAD')
url = None if status == 0 and core.TRANSCODE == 1: # only transcode successful downloads
if section == "SickBeard": result, new_dir_name = transcoder.transcode_directory(dir_name)
if apikey: if result == 0:
url = "{0}{1}:{2}{3}/api/{4}/?cmd=postprocess".format(protocol, host, port, web_root, apikey) logger.debug("SUCCESS: Transcoding succeeded for files in {0}".format(dir_name), section)
else: dir_name = new_dir_name
url = "{0}{1}:{2}{3}/home/postprocess/processEpisode".format(protocol, host, port, web_root)
elif section == "NzbDrone": chmod_directory = int(str(cfg.get("chmodDirectory", "0")), 8)
url = "{0}{1}:{2}{3}/api/command".format(protocol, host, port, web_root) logger.debug("Config setting 'chmodDirectory' currently set to {0}".format(oct(chmod_directory)), section)
url2 = "{0}{1}:{2}{3}/api/config/downloadClient".format(protocol, host, port, web_root) if chmod_directory:
headers = {"X-Api-Key": apikey} logger.info("Attempting to set the octal permission of '{0}' on directory '{1}'".format(oct(chmod_directory), dir_name), section)
# params = {'sortKey': 'series.title', 'page': 1, 'pageSize': 1, 'sortDir': 'asc'} core.rchmod(dir_name, chmod_directory)
else:
logger.error("FAILED: Transcoding failed for files in {0}".format(dir_name), section)
return [1, "{0}: Failed to post-process - Transcoding failed".format(section)]
# configure SB params to pass
fork_params['quiet'] = 1
fork_params['proc_type'] = 'manual'
if input_name is not None:
fork_params['nzbName'] = input_name
for param in copy.copy(fork_params):
if param == "failed":
fork_params[param] = failed
del fork_params['proc_type']
if "type" in fork_params:
del fork_params['type']
if param == "return_data":
fork_params[param] = 0
del fork_params['quiet']
if param == "type":
fork_params[param] = 'manual'
if "proc_type" in fork_params:
del fork_params['proc_type']
if param in ["dir_name", "dir", "proc_dir", "process_directory", "path"]:
fork_params[param] = dir_name
if remote_path: if remote_path:
logger.debug("remote_path: {0}".format(remote_dir(dir_name)), section) fork_params[param] = remote_dir(dir_name)
data = {"name": "DownloadedEpisodesScan", "path": remote_dir(dir_name), "downloadClientId": download_id, "importMode": import_mode}
if param == "process_method":
if process_method:
fork_params[param] = process_method
else: else:
logger.debug("path: {0}".format(dir_name), section) del fork_params[param]
data = {"name": "DownloadedEpisodesScan", "path": dir_name, "downloadClientId": download_id, "importMode": import_mode}
if not download_id:
data.pop("downloadClientId")
data = json.dumps(data)
try: if param in ["force", "force_replace"]:
if section == "SickBeard": if force:
logger.debug("Opening URL: {0} with params: {1}".format(url, fork_params), section) fork_params[param] = force
s = requests.Session()
if not apikey and username and password:
login = "{0}{1}:{2}{3}/login".format(protocol, host, port, web_root)
login_params = {'username': username, 'password': password}
r = s.get(login, verify=False, timeout=(30, 60))
if r.status_code == 401 and r.cookies.get('_xsrf'):
login_params['_xsrf'] = r.cookies.get('_xsrf')
s.post(login, data=login_params, stream=True, verify=False, timeout=(30, 60))
r = s.get(url, auth=(username, password), params=fork_params, stream=True, verify=False, timeout=(30, 1800))
elif section == "NzbDrone":
logger.debug("Opening URL: {0} with data: {1}".format(url, data), section)
r = requests.post(url, data=data, headers=headers, stream=True, verify=False, timeout=(30, 1800))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url), section)
return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
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)]
success = False
queued = False
started = False
if section == "SickBeard":
if apikey:
if r.json()['result'] == 'success':
success = True
else: else:
for line in r.iter_lines(): del fork_params[param]
if line:
line = line.decode('utf-8')
logger.postprocess("{0}".format(line), section)
if "Moving file from" in line:
input_name = os.path.split(line)[1]
if "added to the queue" in line:
queued = True
if "Processing succeeded" in line or "Successfully processed" in line:
success = True
if queued: if param in ["delete_on", "delete"]:
time.sleep(60) if delete_on:
elif section == "NzbDrone": fork_params[param] = delete_on
try: else:
res = json.loads(r.content) del fork_params[param]
scan_id = int(res['id'])
logger.debug("Scan started with id: {0}".format(scan_id), section)
started = True
except Exception as e:
logger.warning("No scan id was returned due to: {0}".format(e), section)
scan_id = None
started = False
if status != 0 and delete_failed and not os.path.dirname(dir_name) == dir_name: if param == "ignore_subs":
logger.postprocess("Deleting failed files and folder {0}".format(dir_name), section) if ignore_subs:
remove_dir(dir_name) fork_params[param] = ignore_subs
else:
del fork_params[param]
if success: if param == "force_next":
fork_params[param] = 1
# delete any unused params so we don't pass them to SB by mistake
[fork_params.pop(k) for k, v in fork_params.items() if v is None]
if status == 0:
if section == "NzbDrone" and not apikey:
logger.info('No Sonarr apikey entered. Processing completed.')
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)] return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif section == "NzbDrone" and started: logger.postprocess("SUCCESS: The download succeeded, sending a post-process request", section)
n = 0 else:
params = {} core.FAILED = True
url = "{0}/{1}".format(url, scan_id) if failure_link:
while n < 6: # set up wait_for minutes to see if command completes.. report_nzb(failure_link, client_agent)
time.sleep(10 * wait_for) if 'failed' in fork_params:
command_status = self.command_complete(url, params, headers, section) logger.postprocess("FAILED: The download failed. Sending 'failed' process request to {0} branch".format(fork), section)
if command_status and command_status in ['completed', 'failed']: elif section == "NzbDrone":
break logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(fork), section)
n += 1 return [1, "{0}: Download Failed. Sending back to {1}".format(section, section)] # Return as failed to flag this in the downloader.
if command_status:
logger.debug("The Scan command return status: {0}".format(command_status), section)
if not os.path.exists(dir_name):
logger.debug("The directory {0} has been removed. Renaming was successful.".format(dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status and command_status in ['completed']:
logger.debug("The Scan command has completed successfully. Renaming was successful.", section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status and command_status in ['failed']:
logger.debug("The Scan command has failed. Renaming was not successful.", section)
# return [1, "%s: Failed to post-process %s" % (section, input_name) ]
if self.completed_download_handling(url2, headers, section=section):
logger.debug("The Scan command did not return status completed, but complete Download Handling is enabled. Passing back to {0}.".format(section), section)
return [status, "{0}: Complete DownLoad Handling is enabled. Passing back to {1}".format(section, section)]
else:
logger.warning("The Scan command did not return a valid status. Renaming was not successful.", section)
return [1, "{0}: Failed to post-process {1}".format(section, input_name)]
else: else:
return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)] # We did not receive Success confirmation. logger.postprocess("FAILED: The download failed. {0} branch does not handle failed downloads. Nothing to process".format(fork), section)
if delete_failed and os.path.isdir(dir_name) and not os.path.dirname(dir_name) == dir_name:
logger.postprocess("Deleting failed files and folder {0}".format(dir_name), section)
remove_dir(dir_name)
return [1, "{0}: Failed to post-process. {1} does not support failed downloads".format(section, section)] # Return as failed to flag this in the downloader.
def command_complete(self, url, params, headers, section): url = None
try: if section == "SickBeard":
r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60)) if apikey:
except requests.ConnectionError: url = "{0}{1}:{2}{3}/api/{4}/?cmd=postprocess".format(protocol, host, port, web_root, apikey)
logger.error("Unable to open URL: {0}".format(url), section)
return None
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 None
else: else:
try: url = "{0}{1}:{2}{3}/home/postprocess/processEpisode".format(protocol, host, port, web_root)
return r.json()['state'] elif section == "NzbDrone":
except (ValueError, KeyError): url = "{0}{1}:{2}{3}/api/command".format(protocol, host, port, web_root)
# ValueError catches simplejson's JSONDecodeError and json's ValueError url2 = "{0}{1}:{2}{3}/api/config/downloadClient".format(protocol, host, port, web_root)
logger.error("{0} did not return expected json data.".format(section), section) headers = {"X-Api-Key": apikey}
return None # params = {'sortKey': 'series.title', 'page': 1, 'pageSize': 1, 'sortDir': 'asc'}
if remote_path:
logger.debug("remote_path: {0}".format(remote_dir(dir_name)), section)
data = {"name": "DownloadedEpisodesScan", "path": remote_dir(dir_name), "downloadClientId": download_id, "importMode": import_mode}
else:
logger.debug("path: {0}".format(dir_name), section)
data = {"name": "DownloadedEpisodesScan", "path": dir_name, "downloadClientId": download_id, "importMode": import_mode}
if not download_id:
data.pop("downloadClientId")
data = json.dumps(data)
def completed_download_handling(self, url2, headers, section="MAIN"): try:
try: if section == "SickBeard":
r = requests.get(url2, params={}, headers=headers, stream=True, verify=False, timeout=(30, 60)) logger.debug("Opening URL: {0} with params: {1}".format(url, fork_params), section)
except requests.ConnectionError: s = requests.Session()
logger.error("Unable to open URL: {0}".format(url2), section) if not apikey and username and password:
return False login = "{0}{1}:{2}{3}/login".format(protocol, host, port, web_root)
if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]: login_params = {'username': username, 'password': password}
logger.error("Server returned status {0}".format(r.status_code), section) r = s.get(login, verify=False, timeout=(30, 60))
return False if r.status_code == 401 and r.cookies.get('_xsrf'):
login_params['_xsrf'] = r.cookies.get('_xsrf')
s.post(login, data=login_params, stream=True, verify=False, timeout=(30, 60))
r = s.get(url, auth=(username, password), params=fork_params, stream=True, verify=False, timeout=(30, 1800))
elif section == "NzbDrone":
logger.debug("Opening URL: {0} with data: {1}".format(url, data), section)
r = requests.post(url, data=data, headers=headers, stream=True, verify=False, timeout=(30, 1800))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url), section)
return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]
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)]
success = False
queued = False
started = False
if section == "SickBeard":
if apikey:
if r.json()['result'] == 'success':
success = True
else: else:
try: for line in r.iter_lines():
return r.json().get("enableCompletedDownloadHandling", False) if line:
except ValueError: line = line.decode('utf-8')
# ValueError catches simplejson's JSONDecodeError and json's ValueError logger.postprocess("{0}".format(line), section)
return False if "Moving file from" in line:
input_name = os.path.split(line)[1]
if "added to the queue" in line:
queued = True
if "Processing succeeded" in line or "Successfully processed" in line:
success = True
if queued:
time.sleep(60)
elif section == "NzbDrone":
try:
res = json.loads(r.content)
scan_id = int(res['id'])
logger.debug("Scan started with id: {0}".format(scan_id), section)
started = True
except Exception as e:
logger.warning("No scan id was returned due to: {0}".format(e), section)
scan_id = None
started = False
if status != 0 and delete_failed and not os.path.dirname(dir_name) == dir_name:
logger.postprocess("Deleting failed files and folder {0}".format(dir_name), section)
remove_dir(dir_name)
if success:
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif section == "NzbDrone" and started:
n = 0
params = {}
url = "{0}/{1}".format(url, scan_id)
while n < 6: # set up wait_for minutes to see if command completes..
time.sleep(10 * wait_for)
command_status = command_complete(url, params, headers, section)
if command_status and command_status in ['completed', 'failed']:
break
n += 1
if command_status:
logger.debug("The Scan command return status: {0}".format(command_status), section)
if not os.path.exists(dir_name):
logger.debug("The directory {0} has been removed. Renaming was successful.".format(dir_name), section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status and command_status in ['completed']:
logger.debug("The Scan command has completed successfully. Renaming was successful.", section)
return [0, "{0}: Successfully post-processed {1}".format(section, input_name)]
elif command_status and command_status in ['failed']:
logger.debug("The Scan command has failed. Renaming was not successful.", section)
# return [1, "%s: Failed to post-process %s" % (section, input_name) ]
if completed_download_handling(url2, headers, section=section):
logger.debug("The Scan command did not return status completed, but complete Download Handling is enabled. Passing back to {0}.".format(section), section)
return [status, "{0}: Complete DownLoad Handling is enabled. Passing back to {1}".format(section, section)]
else:
logger.warning("The Scan command did not return a valid status. Renaming was not successful.", section)
return [1, "{0}: Failed to post-process {1}".format(section, input_name)]
else:
return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)] # We did not receive Success confirmation.
def command_complete(url, params, headers, section):
try:
r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url), section)
return None
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 None
else:
try:
return r.json()['state']
except (ValueError, KeyError):
# ValueError catches simplejson's JSONDecodeError and json's ValueError
logger.error("{0} did not return expected json data.".format(section), section)
return None
def completed_download_handling(url2, headers, section="MAIN"):
try:
r = requests.get(url2, params={}, headers=headers, stream=True, verify=False, timeout=(30, 60))
except requests.ConnectionError:
logger.error("Unable to open URL: {0}".format(url2), section)
return False
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 False
else:
try:
return r.json().get("enableCompletedDownloadHandling", False)
except ValueError:
# ValueError catches simplejson's JSONDecodeError and json's ValueError
return False

View file

@ -12,7 +12,7 @@ import sys
import core import core
from core import logger, main_db from core import logger, main_db
from core.auto_process import Comic, Game, Movie, Music, TV from core.auto_process import comics, games, movies, music, tv
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,18 +109,18 @@ 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 = Movie().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 = Comic().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 = Game().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: