Fix quotes - standardize to single-quoted strings

This commit is contained in:
Labrys of Knossos 2018-12-29 14:05:37 -05:00
commit c5343889fb
30 changed files with 1257 additions and 1257 deletions

View file

@ -13,24 +13,24 @@ requests.packages.urllib3.disable_warnings()
def process(section, dir_name, input_name=None, status=0, client_agent='manual', input_category=None):
apc_version = "2.04"
comicrn_version = "1.01"
apc_version = '2.04'
comicrn_version = '1.01'
cfg = dict(core.CFG[section][input_category])
host = cfg["host"]
port = cfg["port"]
apikey = cfg["apikey"]
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://"
host = cfg['host']
port = cfg['port']
apikey = cfg['apikey']
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://'
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):
logger.error("Server did not respond. Exiting", section)
logger.error('Server did not respond. Exiting', section)
return ProcessResult(
message="{0}: Failed to post-process - {0} did not respond.".format(section),
message='{0}: Failed to post-process - {0} did not respond.'.format(section),
status_code=1,
)
@ -53,19 +53,19 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual',
success = False
logger.debug("Opening URL: {0}".format(url), section)
logger.debug('Opening URL: {0}'.format(url), section)
try:
r = requests.post(url, params=params, stream=True, verify=False, timeout=(30, 300))
except requests.ConnectionError:
logger.error("Unable to open URL", section)
logger.error('Unable to open URL', section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to connect to {0}".format(section),
message='{0}: Failed to post-process - Unable to connect to {0}'.format(section),
status_code=1
)
if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
logger.error("Server returned status {0}".format(r.status_code), section)
logger.error('Server returned status {0}'.format(r.status_code), section)
return ProcessResult(
message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code),
message='{0}: Failed to post-process - Server returned status {1}'.format(section, r.status_code),
status_code=1,
)
@ -74,19 +74,19 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual',
result = result.split('\n')
for line in result:
if line:
logger.postprocess("{0}".format(line), section)
if "Post Processing SUCCESSFUL" in line:
logger.postprocess('{0}'.format(line), section)
if 'Post Processing SUCCESSFUL' in line:
success = True
if success:
logger.postprocess("SUCCESS: This issue has been processed successfully", section)
logger.postprocess('SUCCESS: This issue has been processed successfully', section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
else:
logger.warning("The issue does not appear to have successfully processed. Please check your Logs", section)
logger.warning('The issue does not appear to have successfully processed. Please check your Logs', section)
return ProcessResult(
message="{0}: Failed to post-process - Returned log from {0} was not as expected.".format(section),
message='{0}: Failed to post-process - Returned log from {0} was not as expected.'.format(section),
status_code=1,
)

View file

@ -31,32 +31,32 @@ 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)
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)
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)
logger.error('{0} did not return expected json data.'.format(section), section)
return None
def completed_download_handling(url2, headers, section="MAIN"):
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)
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)
logger.error('Server returned status {0}'.format(r.status_code), section)
return False
else:
try:
return r.json().get("enableCompletedDownloadHandling", False)
return r.json().get('enableCompletedDownloadHandling', False)
except ValueError:
# ValueError catches simplejson's JSONDecodeError and json's ValueError
return False

View file

@ -18,27 +18,27 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual',
cfg = dict(core.CFG[section][input_category])
host = cfg["host"]
port = cfg["port"]
apikey = cfg["apikey"]
library = cfg.get("library")
ssl = int(cfg.get("ssl", 0))
web_root = cfg.get("web_root", "")
protocol = "https://" if ssl else "http://"
host = cfg['host']
port = cfg['port']
apikey = cfg['apikey']
library = cfg.get('library')
ssl = int(cfg.get('ssl', 0))
web_root = cfg.get('web_root', '')
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):
logger.error("Server did not respond. Exiting", section)
logger.error('Server did not respond. Exiting', section)
return ProcessResult(
message="{0}: Failed to post-process - {0} did not respond.".format(section),
message='{0}: Failed to post-process - {0} did not respond.'.format(section),
status_code=1,
)
input_name, dir_name = convert_to_ascii(input_name, dir_name)
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'
@ -49,51 +49,51 @@ def process(section, dir_name, input_name=None, status=0, client_agent='manual',
'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")
logger.error('Unable to open URL')
return ProcessResult(
message="{0}: Failed to post-process - Unable to connect to {1}".format(section, section),
message='{0}: Failed to post-process - Unable to connect to {1}'.format(section, section),
status_code=1,
)
result = r.json()
logger.postprocess("{0}".format(result), section)
logger.postprocess('{0}'.format(result), section)
if library:
logger.postprocess("moving files to library: {0}".format(library), section)
logger.postprocess('moving files to library: {0}'.format(library), section)
try:
shutil.move(dir_name, os.path.join(library, input_name))
except Exception:
logger.error("Unable to move {0} to {1}".format(dir_name, os.path.join(library, input_name)), section)
logger.error('Unable to move {0} to {1}'.format(dir_name, os.path.join(library, input_name)), section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to move files".format(section),
message='{0}: Failed to post-process - Unable to move files'.format(section),
status_code=1,
)
else:
logger.error("No library specified to move files to. Please edit your configuration.", section)
logger.error('No library specified to move files to. Please edit your configuration.', section)
return ProcessResult(
message="{0}: Failed to post-process - No library defined in {0}".format(section),
message='{0}: Failed to post-process - No library defined in {0}'.format(section),
status_code=1,
)
if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
logger.error("Server returned status {0}".format(r.status_code), section)
logger.error('Server returned status {0}'.format(r.status_code), section)
return ProcessResult(
message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code),
message='{0}: Failed to post-process - Server returned status {1}'.format(section, r.status_code),
status_code=1,
)
elif result['success']:
logger.postprocess("SUCCESS: Status for {0} has been set to {1} in Gamez".format(gamez_id, download_status), section)
logger.postprocess('SUCCESS: Status for {0} has been set to {1} in Gamez'.format(gamez_id, download_status), section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
else:
logger.error("FAILED: Status for {0} has NOT been updated in Gamez".format(gamez_id), section)
logger.error('FAILED: Status for {0} has NOT been updated in Gamez'.format(gamez_id), section)
return ProcessResult(
message="{0}: Failed to post-process - Returned log from {0} was not as expected.".format(section),
message='{0}: Failed to post-process - Returned log from {0} was not as expected.'.format(section),
status_code=1,
)

View file

@ -15,54 +15,54 @@ from core.utils import convert_to_ascii, find_download, find_imdbid, import_subs
requests.packages.urllib3.disable_warnings()
def process(section, dir_name, input_name=None, status=0, client_agent="manual", download_id="", input_category=None, failure_link=None):
def process(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])
host = cfg["host"]
port = cfg["port"]
apikey = cfg["apikey"]
if section == "CouchPotato":
method = cfg["method"]
host = cfg['host']
port = cfg['port']
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")
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", "")
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))
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)
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':
release = get_release(base_url, imdbid, download_id)
else:
release = None
else:
logger.error("Server did not respond. Exiting", section)
logger.error('Server did not respond. Exiting', section)
return ProcessResult(
message="{0}: Failed to post-process - {0} did not respond.".format(section),
message='{0}: Failed to post-process - {0} did not respond.'.format(section),
status_code=1,
)
@ -86,7 +86,7 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
specific_path = os.path.join(dir_name, str(input_name))
clean_name = os.path.splitext(specific_path)
if clean_name[1] == ".nzb":
if clean_name[1] == '.nzb':
specific_path = clean_name[0]
if os.path.isdir(specific_path):
dir_name = specific_path
@ -109,23 +109,23 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
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)
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)
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)
elif client_agent == 'manual':
logger.warning('No media files found in directory {0} to manually process.'.format(dir_name), section)
return ProcessResult(
message="",
message='',
status_code=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)
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')
@ -134,24 +134,24 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
if core.TRANSCODE == 1:
result, new_dir_name = transcoder.transcode_directory(dir_name)
if result == 0:
logger.debug("Transcoding succeeded for files in {0}".format(dir_name), section)
logger.debug('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)
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)
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)
logger.error('Transcoding failed for files in {0}'.format(dir_name), section)
return ProcessResult(
message="{0}: Failed to post-process - Transcoding failed".format(section),
message='{0}: Failed to post-process - Transcoding failed'.format(section),
status_code=1,
)
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:
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)
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)
@ -159,7 +159,7 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
if not apikey: # If only using Transcoder functions, exit here.
logger.info('No CouchPotato or Radarr apikey entered. Processing completed.')
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
@ -170,157 +170,157 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
params['media_folder'] = remote_dir(dir_name) if remote_path else dir_name
if section == "CouchPotato":
if method == "manage":
command = "manage.update"
if section == 'CouchPotato':
if method == 'manage':
command = 'manage.update'
params = {}
else:
command = "renamer.scan"
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)
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":
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)
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":
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)
logger.error('Unable to open URL', section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to connect to {0}".format(section),
message='{0}: Failed to post-process - Unable to connect to {0}'.format(section),
status_code=1,
)
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)
logger.error('Server returned status {0}'.format(r.status_code), section)
return ProcessResult(
message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code),
message='{0}: Failed to post-process - Server returned status {1}'.format(section, r.status_code),
status_code=1,
)
elif section == "CouchPotato" and result['success']:
logger.postprocess("SUCCESS: Finished {0} scan for folder {1}".format(method, dir_name), section)
if method == "manage":
elif section == 'CouchPotato' and result['success']:
logger.postprocess('SUCCESS: Finished {0} scan for folder {1}'.format(method, dir_name), section)
if method == 'manage':
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
elif section == "Radarr":
logger.postprocess("Radarr response: {0}".format(result['state']))
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)
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)
logger.warning('No scan id was returned due to: {0}'.format(e), section)
scan_id = None
else:
logger.error("FAILED: {0} scan was unable to finish for folder {1}. exiting!".format(method, dir_name),
logger.error('FAILED: {0} scan was unable to finish for folder {1}. exiting!'.format(method, dir_name),
section)
return ProcessResult(
message="{0}: Failed to post-process - Server did not return success".format(section),
message='{0}: Failed to post-process - Server did not return success'.format(section),
status_code=1,
)
else:
core.FAILED = True
logger.postprocess("FAILED DOWNLOAD DETECTED FOR {0}".format(input_name), section)
logger.postprocess('FAILED DOWNLOAD DETECTED FOR {0}'.format(input_name), section)
if failure_link:
report_nzb(failure_link, client_agent)
if section == "Radarr":
logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(section), section)
if section == 'Radarr':
logger.postprocess('FAILED: The download failed. Sending failed download to {0} for CDH processing'.format(section), section)
return ProcessResult(
message="{0}: Download Failed. Sending back to {0}".format(section),
message='{0}: Download Failed. Sending back to {0}'.format(section),
status_code=1, # Return as failed to flag this in the downloader.
)
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)
logger.postprocess('Deleting failed files and folder {0}'.format(dir_name), section)
remove_dir(dir_name)
if not release_id and not media_id:
logger.error("Could not find a downloaded movie in the database matching {0}, exiting!".format(input_name),
logger.error('Could not find a downloaded movie in the database matching {0}, exiting!'.format(input_name),
section)
return ProcessResult(
message="{0}: Failed to post-process - Failed download not found in {0}".format(section),
message='{0}: Failed to post-process - Failed download not found in {0}'.format(section),
status_code=1,
)
if release_id:
logger.postprocess("Setting failed release {0} to ignored ...".format(input_name), section)
logger.postprocess('Setting failed release {0} to ignored ...'.format(input_name), section)
url = "{url}release.ignore".format(url=base_url)
url = '{url}release.ignore'.format(url=base_url)
params = {'id': release_id}
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)
logger.debug('Opening URL: {0} with PARAMS: {1}'.format(url, params), section)
try:
r = requests.get(url, params=params, verify=False, timeout=(30, 120))
except requests.ConnectionError:
logger.error("Unable to open URL {0}".format(url), section)
logger.error('Unable to open URL {0}'.format(url), section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to connect to {1}".format(section),
message='{0}: Failed to post-process - Unable to connect to {0}'.format(section),
status_code=1,
)
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)
logger.error('Server returned status {0}'.format(r.status_code), section)
return ProcessResult(
status_code=1,
message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code),
message='{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)
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)
logger.warning('FAILED: Unable to set {0} to ignored!'.format(input_name), section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to set {1} to ignored".format(section, input_name),
message='{0}: Failed to post-process - Unable to set {1} to ignored'.format(section, input_name),
status_code=1,
)
logger.postprocess("Trying to snatch the next highest ranked release.", section)
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)
url = '{0}movie.searcher.try_next'.format(base_url)
logger.debug('Opening URL: {0}'.format(url), section)
try:
r = requests.get(url, params={'media_id': media_id}, verify=False, timeout=(30, 600))
except requests.ConnectionError:
logger.error("Unable to open URL {0}".format(url), section)
logger.error('Unable to open URL {0}'.format(url), section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to connect to {0}".format(section),
message='{0}: Failed to post-process - Unable to connect to {0}'.format(section),
status_code=1,
)
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)
logger.error('Server returned status {0}'.format(r.status_code), section)
return ProcessResult(
message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code),
message='{0}: Failed to post-process - Server returned status {1}'.format(section, r.status_code),
status_code=1,
)
elif result['success']:
logger.postprocess("SUCCESS: Snatched the next highest release ...", section)
logger.postprocess('SUCCESS: Snatched the next highest release ...', section)
return ProcessResult(
message="{0}: Successfully snatched next highest release".format(section),
message='{0}: Successfully snatched next highest release'.format(section),
status_code=0,
)
else:
logger.postprocess("SUCCESS: Unable to find a new release to snatch now. CP will keep searching!", section)
logger.postprocess('SUCCESS: Unable to find a new release to snatch now. CP will keep searching!', section)
return ProcessResult(
status_code=0,
message="{0}: No new release found now. {0} will keep searching".format(section),
message='{0}: No new release found now. {0} will keep searching'.format(section),
)
# Added a release that was not in the wanted list so confirm rename successful by finding this movie media.list.
@ -330,8 +330,8 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
# we will now check to see if CPS has finished renaming before returning to TorrentToMedia and unpausing.
timeout = time.time() + 60 * wait_for
while time.time() < timeout: # only wait 2 (default) minutes, then return.
logger.postprocess("Checking for status change, please stand by ...", section)
if section == "CouchPotato":
logger.postprocess('Checking for status change, please stand by ...', section)
if section == 'CouchPotato':
release = get_release(base_url, imdbid, download_id, release_id)
scan_id = None
else:
@ -342,50 +342,50 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
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(
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 ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
if release_status_new != release_status_old:
logger.postprocess("SUCCESS: Release for {0} has now been marked with a status of [{1}]".format(
logger.postprocess('SUCCESS: Release for {0} has now been marked with a status of [{1}]'.format(
title, str(release_status_new).upper()), section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
except Exception:
pass
elif scan_id:
url = "{0}/{1}".format(base_url, 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)
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)]
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)
logger.debug('The Scan command has failed. Renaming was not successful.', section)
# return ProcessResult(
# message="{0}: Failed to post-process {1}".format(section, input_name),
# message='{0}: Failed to post-process {1}'.format(section, input_name),
# status_code=1,
# )
if not os.path.isdir(dir_name):
logger.postprocess("SUCCESS: Input Directory [{0}] has been processed and removed".format(
logger.postprocess('SUCCESS: Input Directory [{0}] has been processed and removed'.format(
dir_name), section)
return ProcessResult(
status_code=0,
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{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(
logger.postprocess('SUCCESS: Input Directory [{0}] has no remaining media files. This has been fully processed.'.format(
dir_name), section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
@ -393,19 +393,19 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
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 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)
if section == 'Radarr' and 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 ProcessResult(
message="{0}: Complete DownLoad Handling is enabled. Passing back to {0}".format(section),
message='{0}: Complete DownLoad Handling is enabled. Passing back to {0}'.format(section),
status_code=status,
)
logger.warning(
"{0} does not appear to have changed status after {1} minutes, Please check your logs.".format(input_name, wait_for),
'{0} does not appear to have changed status after {1} minutes, Please check your logs.'.format(input_name, wait_for),
section,
)
return ProcessResult(
status_code=1,
message="{0}: Failed to post-process - No change in status".format(section),
message='{0}: Failed to post-process - No change in status'.format(section),
)
@ -415,39 +415,39 @@ def get_release(base_url, imdb_id=None, download_id=None, release_id=None):
# determine cmd and params to send to CouchPotato to get our results
section = 'movies'
cmd = "media.list"
cmd = 'media.list'
if release_id or imdb_id:
section = 'media'
cmd = "media.get"
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")
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))
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))
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")
logger.error('CouchPotato returned the following non-json data')
for line in r.iter_lines():
logger.error("{0}".format(line))
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']))
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
@ -489,7 +489,7 @@ def get_release(base_url, imdb_id=None, download_id=None, release_id=None):
for id1, x1 in results.items():
for id2, x2 in results.items():
try:
if x2["last_edit"] > x1["last_edit"]:
if x2['last_edit'] > x1['last_edit']:
results.pop(id1)
except Exception:
continue

View file

@ -15,34 +15,34 @@ from core.utils import convert_to_ascii, list_media_files, remote_dir, remove_di
requests.packages.urllib3.disable_warnings()
def process(section, dir_name, input_name=None, status=0, client_agent="manual", input_category=None):
def process(section, dir_name, input_name=None, status=0, client_agent='manual', input_category=None):
status = int(status)
cfg = dict(core.CFG[section][input_category])
host = cfg["host"]
port = cfg["port"]
apikey = cfg["apikey"]
wait_for = int(cfg["wait_for"])
ssl = int(cfg.get("ssl", 0))
delete_failed = int(cfg["delete_failed"])
web_root = cfg.get("web_root", "")
remote_path = int(cfg.get("remote_path", 0))
protocol = "https://" if ssl else "http://"
host = cfg['host']
port = cfg['port']
apikey = cfg['apikey']
wait_for = int(cfg['wait_for'])
ssl = int(cfg.get('ssl', 0))
delete_failed = int(cfg['delete_failed'])
web_root = cfg.get('web_root', '')
remote_path = int(cfg.get('remote_path', 0))
protocol = 'https://' if ssl else 'http://'
status = int(status)
if status > 0 and core.NOEXTRACTFAILED:
extract = 0
else:
extract = int(cfg.get("extract", 0))
extract = int(cfg.get('extract', 0))
if section == "Lidarr":
url = "{0}{1}:{2}{3}/api/v1".format(protocol, host, port, web_root)
if section == 'Lidarr':
url = '{0}{1}:{2}{3}/api/v1'.format(protocol, host, port, web_root)
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):
logger.error("Server did not respond. Exiting", section)
logger.error('Server did not respond. Exiting', section)
return ProcessResult(
message="{0}: Failed to post-process - {0} did not respond.".format(section),
message='{0}: Failed to post-process - {0} did not respond.'.format(section),
status_code=1,
)
@ -51,7 +51,7 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
specific_path = os.path.join(dir_name, str(input_name))
clean_name = os.path.splitext(specific_path)
if clean_name[1] == ".nzb":
if clean_name[1] == '.nzb':
specific_path = clean_name[0]
if os.path.isdir(specific_path):
dir_name = specific_path
@ -65,14 +65,14 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
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:
# logger.info("Status shown as failed from Downloader, but valid video files found. Setting as successful.", section)
# 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":
if status == 0 and section == 'HeadPhones':
params = {
'apikey': apikey,
'cmd': "forceProcess",
'cmd': 'forceProcess',
'dir': remote_dir(dir_name) if remote_path else dir_name
}
@ -82,7 +82,7 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
params = {
'apikey': apikey,
'cmd': "forceProcess",
'cmd': 'forceProcess',
'dir': os.path.split(remote_dir(dir_name))[0] if remote_path else os.path.split(dir_name)[0]
}
@ -91,29 +91,29 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
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)
logger.warning('The music album does not appear to have changed status after {0} minutes. Please check your Logs'.format(wait_for), section)
return ProcessResult(
message="{0}: Failed to post-process - No change in wanted status".format(section),
message='{0}: Failed to post-process - No change in wanted status'.format(section),
status_code=1,
)
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}
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)}
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}
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)
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)
logger.error('Unable to open URL: {0}'.format(url), section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to connect to {0}".format(section),
message='{0}: Failed to post-process - Unable to connect to {0}'.format(section),
status_code=1,
)
@ -123,20 +123,20 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
try:
res = json.loads(r.content)
scan_id = int(res['id'])
logger.debug("Scan started with id: {0}".format(scan_id), section)
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)
logger.warning('No scan id was returned due to: {0}'.format(e), section)
scan_id = None
started = False
return ProcessResult(
message="{0}: Failed to post-process - Unable to start scan".format(section),
message='{0}: Failed to post-process - Unable to start scan'.format(section),
status_code=1,
)
n = 0
params = {}
url = "{0}/{1}".format(url, scan_id)
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)
@ -144,64 +144,64 @@ def process(section, dir_name, input_name=None, status=0, client_agent="manual",
break
n += 1
if command_status:
logger.debug("The Scan command return status: {0}".format(command_status), section)
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)
logger.debug('The directory {0} has been removed. Renaming was successful.'.format(dir_name), section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
elif command_status and command_status in ['completed']:
logger.debug("The Scan command has completed successfully. Renaming was successful.", section)
logger.debug('The Scan command has completed successfully. Renaming was successful.', section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
elif command_status and command_status in ['failed']:
logger.debug("The Scan command has failed. Renaming was not successful.", section)
logger.debug('The Scan command has failed. Renaming was not successful.', section)
# return ProcessResult(
# message="{0}: Failed to post-process {1}".format(section, input_name),
# message='{0}: Failed to post-process {1}'.format(section, input_name),
# status_code=1,
# )
else:
logger.debug("The Scan command did not return status completed. Passing back to {0} to attempt complete download handling.".format(section), section)
logger.debug('The Scan command did not return status completed. Passing back to {0} to attempt complete download handling.'.format(section), section)
return ProcessResult(
message="{0}: Passing back to {0} to attempt Complete Download Handling".format(section),
message='{0}: Passing back to {0} to attempt Complete Download Handling'.format(section),
status_code=status,
)
else:
if section == "Lidarr":
logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(section), section)
if section == 'Lidarr':
logger.postprocess('FAILED: The download failed. Sending failed download to {0} for CDH processing'.format(section), section)
return ProcessResult(
message="{0}: Download Failed. Sending back to {0}".format(section),
message='{0}: Download Failed. Sending back to {0}'.format(section),
status_code=1, # Return as failed to flag this in the downloader.
)
else:
logger.warning("FAILED DOWNLOAD DETECTED", section)
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)
logger.postprocess('Deleting failed files and folder {0}'.format(dir_name), section)
remove_dir(dir_name)
return ProcessResult(
message="{0}: Failed to post-process. {0} does not support failed downloads".format(section),
message='{0}: Failed to post-process. {0} does not support failed downloads'.format(section),
status_code=1, # Return as failed to flag this in the downloader.
)
def get_status(url, apikey, dir_name):
logger.debug("Attempting to get current status for release:{0}".format(os.path.basename(dir_name)))
logger.debug('Attempting to get current status for release:{0}'.format(os.path.basename(dir_name)))
params = {
'apikey': apikey,
'cmd': "getHistory"
'cmd': 'getHistory'
}
logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params))
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")
logger.error('Unable to open URL')
return None
try:
@ -212,39 +212,39 @@ def get_status(url, apikey, dir_name):
for album in result:
if os.path.basename(dir_name) == album['FolderName']:
return album["Status"].lower()
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.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)
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)
logger.error('Unable to open URL {0}'.format(url), section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to connect to {0}".format(section),
message='{0}: Failed to post-process - Unable to connect to {0}'.format(section),
status_code=1,
)
logger.debug("Result: {0}".format(r.text), 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)
logger.error('Server returned status {0}'.format(r.status_code), section)
return ProcessResult(
message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code),
message='{0}: Failed to post-process - Server returned status {1}'.format(section, r.status_code),
status_code=1,
)
elif r.text == "OK":
logger.postprocess("SUCCESS: Post-Processing started for {0} in folder {1} ...".format(input_name, dir_name), section)
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)
logger.error('FAILED: Post-Processing has NOT started for {0} in folder {1}. exiting!'.format(input_name, dir_name), section)
return ProcessResult(
message="{0}: Failed to post-process - Returned log from {0} was not as expected.".format(section),
message='{0}: Failed to post-process - Returned log from {0} was not as expected.'.format(section),
status_code=1,
)
@ -253,20 +253,20 @@ def force_process(params, url, apikey, input_name, dir_name, section, 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)
logger.postprocess('SUCCESS: This release is now marked as status [{0}]'.format(current_status), section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
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)
logger.postprocess('SUCCESS: The input directory {0} has been removed Processing must have finished.'.format(dir_name), section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
time.sleep(10 * wait_for)
# The status hasn't changed.
return ProcessResult(
message="no change",
message='no change',
status_code=2,
)

View file

@ -18,56 +18,56 @@ from core.utils import convert_to_ascii, flatten, import_subs, list_media_files,
requests.packages.urllib3.disable_warnings()
def process(section, dir_name, input_name=None, failed=False, client_agent="manual", download_id=None, input_category=None, failure_link=None):
def process(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])
host = cfg["host"]
port = cfg["port"]
ssl = int(cfg.get("ssl", 0))
web_root = cfg.get("web_root", "")
protocol = "https://" if ssl else "http://"
username = cfg.get("username", "")
password = cfg.get("password", "")
apikey = cfg.get("apikey", "")
host = cfg['host']
port = cfg['port']
ssl = int(cfg.get('ssl', 0))
web_root = cfg.get('web_root', '')
protocol = 'https://' if ssl else 'http://'
username = cfg.get('username', '')
password = cfg.get('password', '')
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
fork, fork_params = auto_fork(section, input_category)
elif not username and not apikey:
logger.info('No SickBeard username or Sonarr apikey entered. Performing transcoder functions only')
fork, fork_params = "None", {}
fork, fork_params = 'None', {}
else:
logger.error("Server did not respond. Exiting", section)
logger.error('Server did not respond. Exiting', section)
return ProcessResult(
status_code=1,
message="{0}: Failed to post-process - {0} did not respond.".format(section),
message='{0}: Failed to post-process - {0} did not respond.'.format(section),
)
delete_failed = int(cfg.get("delete_failed", 0))
nzb_extraction_by = cfg.get("nzbExtractionBy", "Downloader")
process_method = cfg.get("process_method")
if client_agent == core.TORRENT_CLIENTAGENT and core.USELINK == "move-sym":
process_method = "symlink"
remote_path = int(cfg.get("remote_path", 0))
wait_for = int(cfg.get("wait_for", 2))
force = int(cfg.get("force", 0))
delete_on = int(cfg.get("delete_on", 0))
ignore_subs = int(cfg.get("ignore_subs", 0))
delete_failed = int(cfg.get('delete_failed', 0))
nzb_extraction_by = cfg.get('nzbExtractionBy', 'Downloader')
process_method = cfg.get('process_method')
if client_agent == core.TORRENT_CLIENTAGENT and core.USELINK == 'move-sym':
process_method = 'symlink'
remote_path = int(cfg.get('remote_path', 0))
wait_for = int(cfg.get('wait_for', 2))
force = int(cfg.get('force', 0))
delete_on = int(cfg.get('delete_on', 0))
ignore_subs = int(cfg.get('ignore_subs', 0))
status = int(failed)
if status > 0 and core.NOEXTRACTFAILED:
extract = 0
else:
extract = int(cfg.get("extract", 0))
# get importmode, default to "Move" for consistency with legacy
import_mode = cfg.get("importMode", "Move")
extract = int(cfg.get('extract', 0))
# get importmode, default to 'Move' for consistency with legacy
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.
dir_name = os.path.split(os.path.normpath(dir_name))[0]
specific_path = os.path.join(dir_name, str(input_name))
clean_name = os.path.splitext(specific_path)
if clean_name[1] == ".nzb":
if clean_name[1] == '.nzb':
specific_path = clean_name[0]
if os.path.isdir(specific_path):
dir_name = specific_path
@ -82,7 +82,7 @@ def process(section, dir_name, input_name=None, failed=False, client_agent="manu
if e.errno != errno.EEXIST:
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:
process_all_exceptions(input_name, dir_name)
input_name, dir_name = convert_to_ascii(input_name, dir_name)
@ -118,24 +118,24 @@ def process(section, dir_name, input_name=None, failed=False, client_agent="manu
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)
elif client_agent == 'manual':
logger.warning('No media files found in directory {0} to manually process.'.format(dir_name), section)
return ProcessResult(
message="",
message='',
status_code=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.")
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.")
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.")
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)
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':
@ -144,18 +144,18 @@ def process(section, dir_name, input_name=None, failed=False, client_agent="manu
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)
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)
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)
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)
logger.error('FAILED: Transcoding failed for files in {0}'.format(dir_name), section)
return ProcessResult(
message="{0}: Failed to post-process - Transcoding failed".format(section),
message='{0}: Failed to post-process - Transcoding failed'.format(section),
status_code=1,
)
@ -166,140 +166,140 @@ def process(section, dir_name, input_name=None, failed=False, client_agent="manu
fork_params['nzbName'] = input_name
for param in copy.copy(fork_params):
if param == "failed":
if param == 'failed':
fork_params[param] = failed
del fork_params['proc_type']
if "type" in fork_params:
if 'type' in fork_params:
del fork_params['type']
if param == "return_data":
if param == 'return_data':
fork_params[param] = 0
del fork_params['quiet']
if param == "type":
if param == 'type':
fork_params[param] = 'manual'
if "proc_type" in fork_params:
if 'proc_type' in fork_params:
del fork_params['proc_type']
if param in ["dir_name", "dir", "proc_dir", "process_directory", "path"]:
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 param == 'process_method':
if process_method:
fork_params[param] = process_method
else:
del fork_params[param]
if param in ["force", "force_replace"]:
if param in ['force', 'force_replace']:
if force:
fork_params[param] = force
else:
del fork_params[param]
if param in ["delete_on", "delete"]:
if param in ['delete_on', 'delete']:
if delete_on:
fork_params[param] = delete_on
else:
del fork_params[param]
if param == "ignore_subs":
if param == 'ignore_subs':
if ignore_subs:
fork_params[param] = ignore_subs
else:
del fork_params[param]
if param == "force_next":
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:
if section == 'NzbDrone' and not apikey:
logger.info('No Sonarr apikey entered. Processing completed.')
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
logger.postprocess("SUCCESS: The download succeeded, sending a post-process request", section)
logger.postprocess('SUCCESS: The download succeeded, sending a post-process request', section)
else:
core.FAILED = True
if failure_link:
report_nzb(failure_link, client_agent)
if 'failed' in fork_params:
logger.postprocess("FAILED: The download failed. Sending 'failed' process request to {0} branch".format(fork), section)
elif section == "NzbDrone":
logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(fork), section)
logger.postprocess('FAILED: The download failed. Sending \'failed\' process request to {0} branch'.format(fork), section)
elif section == 'NzbDrone':
logger.postprocess('FAILED: The download failed. Sending failed download to {0} for CDH processing'.format(fork), section)
return ProcessResult(
message="{0}: Download Failed. Sending back to {0}".format(section),
message='{0}: Download Failed. Sending back to {0}'.format(section),
status_code=1, # Return as failed to flag this in the downloader.
)
else:
logger.postprocess("FAILED: The download failed. {0} branch does not handle failed downloads. Nothing to process".format(fork), section)
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)
logger.postprocess('Deleting failed files and folder {0}'.format(dir_name), section)
remove_dir(dir_name)
return ProcessResult(
message="{0}: Failed to post-process. {0} does not support failed downloads".format(section),
message='{0}: Failed to post-process. {0} does not support failed downloads'.format(section),
status_code=1, # Return as failed to flag this in the downloader.
)
url = None
if section == "SickBeard":
if section == 'SickBeard':
if apikey:
url = "{0}{1}:{2}{3}/api/{4}/?cmd=postprocess".format(protocol, host, port, web_root, apikey)
url = '{0}{1}:{2}{3}/api/{4}/?cmd=postprocess'.format(protocol, host, port, web_root, apikey)
else:
url = "{0}{1}:{2}{3}/home/postprocess/processEpisode".format(protocol, host, port, web_root)
elif section == "NzbDrone":
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}
url = '{0}{1}:{2}{3}/home/postprocess/processEpisode'.format(protocol, host, port, web_root)
elif section == 'NzbDrone':
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}
# 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}
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}
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.pop('downloadClientId')
data = json.dumps(data)
try:
if section == "SickBeard":
logger.debug("Opening URL: {0} with params: {1}".format(url, fork_params), section)
if section == 'SickBeard':
logger.debug('Opening URL: {0} with params: {1}'.format(url, fork_params), section)
s = requests.Session()
if not apikey and username and password:
login = "{0}{1}:{2}{3}/login".format(protocol, host, port, web_root)
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)
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)
logger.error('Unable to open URL: {0}'.format(url), section)
return ProcessResult(
message="{0}: Failed to post-process - Unable to connect to {0}".format(section),
message='{0}: Failed to post-process - Unable to connect to {0}'.format(section),
status_code=1,
)
if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
logger.error("Server returned status {0}".format(r.status_code), section)
logger.error('Server returned status {0}'.format(r.status_code), section)
return ProcessResult(
message="{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code),
message='{0}: Failed to post-process - Server returned status {1}'.format(section, r.status_code),
status_code=1,
)
success = False
queued = False
started = False
if section == "SickBeard":
if section == 'SickBeard':
if apikey:
if r.json()['result'] == 'success':
success = True
@ -307,40 +307,40 @@ def process(section, dir_name, input_name=None, failed=False, client_agent="manu
for line in r.iter_lines():
if line:
line = line.decode('utf-8')
logger.postprocess("{0}".format(line), section)
if "Moving file from" in line:
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:
if 'added to the queue' in line:
queued = True
if "Processing succeeded" in line or "Successfully processed" in line:
if 'Processing succeeded' in line or 'Successfully processed' in line:
success = True
if queued:
time.sleep(60)
elif section == "NzbDrone":
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)
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)
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)
logger.postprocess('Deleting failed files and folder {0}'.format(dir_name), section)
remove_dir(dir_name)
if success:
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
elif section == "NzbDrone" and started:
elif section == 'NzbDrone' and started:
n = 0
params = {}
url = "{0}/{1}".format(url, scan_id)
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)
@ -348,39 +348,39 @@ def process(section, dir_name, input_name=None, failed=False, client_agent="manu
break
n += 1
if command_status:
logger.debug("The Scan command return status: {0}".format(command_status), section)
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)
logger.debug('The directory {0} has been removed. Renaming was successful.'.format(dir_name), section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
elif command_status and command_status in ['completed']:
logger.debug("The Scan command has completed successfully. Renaming was successful.", section)
logger.debug('The Scan command has completed successfully. Renaming was successful.', section)
return ProcessResult(
message="{0}: Successfully post-processed {1}".format(section, input_name),
message='{0}: Successfully post-processed {1}'.format(section, input_name),
status_code=0,
)
elif command_status and command_status in ['failed']:
logger.debug("The Scan command has failed. Renaming was not successful.", section)
logger.debug('The Scan command has failed. Renaming was not successful.', section)
# return ProcessResult(
# message="{0}: Failed to post-process {1}".format(section, input_name),
# message='{0}: Failed to post-process {1}'.format(section, input_name),
# status_code=1,
# )
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)
logger.debug('The Scan command did not return status completed, but complete Download Handling is enabled. Passing back to {0}.'.format(section), section)
return ProcessResult(
message="{0}: Complete DownLoad Handling is enabled. Passing back to {0}".format(section),
message='{0}: Complete DownLoad Handling is enabled. Passing back to {0}'.format(section),
status_code=status,
)
else:
logger.warning("The Scan command did not return a valid status. Renaming was not successful.", section)
logger.warning('The Scan command did not return a valid status. Renaming was not successful.', section)
return ProcessResult(
message="{0}: Failed to post-process {1}".format(section, input_name),
message='{0}: Failed to post-process {1}'.format(section, input_name),
status_code=1,
)
else:
return ProcessResult(
message="{0}: Failed to post-process - Returned log from {0} was not as expected.".format(section),
message='{0}: Failed to post-process - Returned log from {0} was not as expected.'.format(section),
status_code=1, # We did not receive Success confirmation.
)