Labrys of Knossos 27f6c05788 Fix lint errors
2022-12-19 17:03:33 -05:00

195 lines
10 KiB
Python

from __future__ import annotations
import json
import logging
import os
import time
import requests
import nzb2media
import nzb2media.utils.common
from nzb2media.auto_process.common import ProcessResult
from nzb2media.auto_process.common import command_complete
from nzb2media.scene_exceptions import process_all_exceptions
from nzb2media.utils.encoding import convert_to_ascii
from nzb2media.utils.files import extract_files
from nzb2media.utils.files import list_media_files
from nzb2media.utils.network import server_responding
from nzb2media.utils.paths import remote_dir
from nzb2media.utils.paths import remove_dir
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
def process(*, section: str, dir_name: str, input_name: str = '', status: int = 0, input_category: str = '', **kwargs) -> ProcessResult:
log.debug(f'Unused kwargs: {kwargs}')
# Get configuration
if nzb2media.CFG is None:
raise RuntimeError('Configuration not loaded.')
cfg = nzb2media.CFG[section][input_category]
# Base URL
ssl = int(cfg.get('ssl', 0))
scheme = 'https' if ssl else 'http'
host = cfg['host']
port = cfg['port']
web_root = cfg.get('web_root', '')
# Authentication
apikey = cfg.get('apikey', '')
# Params
delete_failed = int(cfg.get('delete_failed', 0))
remote_path = int(cfg.get('remote_path', 0))
wait_for = int(cfg.get('wait_for', 2))
# Misc
if status > 0 and nzb2media.NOEXTRACTFAILED:
extract = 0
else:
extract = int(cfg.get('extract', 0))
# Begin processing
route = f'{web_root}/api/v1' if section == 'Lidarr' else f'{web_root}/api'
url = nzb2media.utils.common.create_url(scheme, host, port, route)
if not server_responding(url):
log.error('Server did not respond. Exiting')
return ProcessResult.failure(f'{section}: Failed to post-process - {section} did not respond.')
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':
specific_path = clean_name[0]
if os.path.isdir(specific_path):
dir_name = specific_path
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:
log.debug(f'Checking for archives to extract in directory: {dir_name}')
extract_files(dir_name)
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)
# status = 0
if not status and section == 'HeadPhones':
params = {'apikey': apikey, 'cmd': 'forceProcess', 'dir': remote_dir(dir_name) if remote_path else dir_name}
res = force_process(params, url, apikey, input_name, dir_name, section, wait_for)
if res.status_code 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.status_code in {0, 1}:
return res
# The status hasn't changed. uTorrent can resume seeding now.
log.warning(f'The music album does not appear to have changed status after {wait_for} minutes. Please check your Logs')
return ProcessResult.failure(f'{section}: Failed to post-process - No change in wanted status')
if not status and section == 'Lidarr':
route = f'{web_root}/api/v1/command'
url = nzb2media.utils.common.create_url(scheme, host, port, route)
headers = {'X-Api-Key': apikey}
if remote_path:
log.debug(f'remote_path: {remote_dir(dir_name)}')
data = {'name': 'Rename', 'path': remote_dir(dir_name)}
else:
log.debug(f'path: {dir_name}')
data = {'name': 'Rename', 'path': dir_name}
try:
log.debug(f'Opening URL: {url} with data: {data}')
response = requests.post(url, data=json.dumps(data), headers=headers, stream=True, verify=False, timeout=(30, 1800))
except requests.ConnectionError:
log.error(f'Unable to open URL: {url}')
return ProcessResult.failure(f'{section}: Failed to post-process - Unable to connect to {section}')
try:
res = response.json()
scan_id = int(res['id'])
log.debug(f'Scan started with id: {scan_id}')
except Exception as error:
log.warning(f'No scan id was returned due to: {error}')
return ProcessResult.failure(f'{section}: Failed to post-process - Unable to start scan')
num = 0
params = {}
url = f'{url}/{scan_id}'
while num < 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
num += 1
if command_status:
log.debug(f'The Scan command return status: {command_status}')
if not os.path.exists(dir_name):
log.debug(f'The directory {dir_name} has been removed. Renaming was successful.')
return ProcessResult.success(f'{section}: Successfully post-processed {input_name}')
if command_status and command_status in {'completed'}:
log.debug('The Scan command has completed successfully. Renaming was successful.')
return ProcessResult.success(f'{section}: Successfully post-processed {input_name}')
if command_status and command_status in {'failed'}:
log.debug('The Scan command has failed. Renaming was not successful.')
# return ProcessResult.failure(f'{SECTION}: Failed to post-process {input_name}')
else:
log.debug(f'The Scan command did not return status completed. Passing back to {section} to attempt complete download handling.')
return ProcessResult(message=f'{section}: Passing back to {section} to attempt Complete Download Handling', status_code=status)
else:
if section == 'Lidarr':
log.debug(f'FAILED: The download failed. Sending failed download to {section} for CDH processing')
# Return as failed to flag this in the downloader.
return ProcessResult.failure(f'{section}: Download Failed. Sending back to {section}')
log.warning('FAILED DOWNLOAD DETECTED')
if delete_failed and os.path.isdir(dir_name) and not os.path.dirname(dir_name) == dir_name:
log.debug(f'Deleting failed files and folder {dir_name}')
remove_dir(dir_name)
# Return as failed to flag this in the downloader.
return ProcessResult.failure(f'{section}: Failed to post-process. {section} does not support failed downloads')
return ProcessResult.failure()
def get_status(url, apikey, dir_name):
log.debug(f'Attempting to get current status for release:{os.path.basename(dir_name)}')
params = {'apikey': apikey, 'cmd': 'getHistory'}
log.debug(f'Opening URL: {url} with PARAMS: {params}')
try:
response = requests.get(url, params=params, verify=False, timeout=(30, 120))
except requests.RequestException:
log.error('Unable to open URL')
return None
try:
result = response.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:
log.error(f'Could not find a status for {input_name}, is it in the wanted list ?')
log.debug(f'Opening URL: {url} with PARAMS: {params}')
try:
response = requests.get(url, params=params, verify=False, timeout=(30, 300))
except requests.ConnectionError:
log.error(f'Unable to open URL {url}')
return ProcessResult.failure(f'{section}: Failed to post-process - Unable to connect to {section}')
log.debug(f'Result: {response.text}')
if response.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
log.error(f'Server returned status {response.status_code}')
return ProcessResult.failure(f'{section}: Failed to post-process - Server returned status {response.status_code}')
if response.text == 'OK':
log.debug(f'SUCCESS: Post-Processing started for {input_name} in folder {dir_name} ...')
else:
log.error(f'FAILED: Post-Processing has NOT started for {input_name} in folder {dir_name}. exiting!')
return ProcessResult.failure(f'{section}: Failed to post-process - Returned log from {section} was not as expected.')
# 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.
log.debug(f'SUCCESS: This release is now marked as status [{current_status}]')
return ProcessResult.success(f'{section}: Successfully post-processed {input_name}')
if not os.path.isdir(dir_name):
log.debug(f'SUCCESS: The input directory {dir_name} has been removed Processing must have finished.')
return ProcessResult.success(f'{section}: Successfully post-processed {input_name}')
time.sleep(10 * wait_for)
# The status hasn't changed.
return ProcessResult(message='no change', status_code=2)