示例#1
0
    def process_response(self, response):
        """Iterate over the lines returned, and log.

        :param response: Streamed Requests response object.
        This method will need to be overwritten in the forks, for alternative response handling.
        """
        for line in response.iter_lines():
            if line:
                line = line.decode('utf-8')
                logger.postprocess('{0}'.format(line), self.sb_init.section)
                # if 'Moving file from' in line:
                #     input_name = os.path.split(line)[1]
                # if 'added to the queue' in line:
                #     queued = True
                # For the refactoring i'm only considering vanilla sickbeard, as for the base class.
                if 'Processing succeeded' in line or 'Successfully processed' in line:
                    self.success = True

        if self.success:
            return ProcessResult(
                message='{0}: Successfully post-processed {1}'.format(self.sb_init.section, self.input_name),
                status_code=0,
            )
        return ProcessResult(
            message='{0}: Failed to post-process - Returned log from {0} was not as expected.'.format(self.sb_init.section),
            status_code=1,  # We did not receive Success confirmation.
        )
示例#2
0
    def api_call(self):
        """Perform a base sickbeard api call."""
        self._process_fork_prarams()
        url = self._create_url()

        logger.debug('Opening URL: {0} with params: {1}'.format(url, self.sb_init.fork_params), self.sb_init.section)
        try:
            if not self.sb_init.apikey and self.sb_init.username and self.sb_init.password:
                # If not using the api, we need to login using user/pass first.
                login = '******'.format(self.sb_init.protocol, self.sb_init.host, self.sb_init.port, self.sb_init.web_root)
                login_params = {'username': self.sb_init.username, 'password': self.sb_init.password}
                r = self.session.get(login, verify=False, timeout=(30, 60))
                if r.status_code in [401, 403] and r.cookies.get('_xsrf'):
                    login_params['_xsrf'] = r.cookies.get('_xsrf')
                self.session.post(login, data=login_params, stream=True, verify=False, timeout=(30, 60))
            response = self.session.get(url, auth=(self.sb_init.username, self.sb_init.password), params=self.sb_init.fork_params, stream=True, verify=False, timeout=(30, 1800))
        except requests.ConnectionError:
            logger.error('Unable to open URL: {0}'.format(url), self.sb_init.section)
            return ProcessResult(
                message='{0}: Failed to post-process - Unable to connect to {0}'.format(self.sb_init.section),
                status_code=1,
            )

        if response.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
            logger.error('Server returned status {0}'.format(response.status_code), self.sb_init.section)
            return ProcessResult(
                message='{0}: Failed to post-process - Server returned status {1}'.format(self.sb_init.section, response.status_code),
                status_code=1,
            )

        return self.process_response(response)
示例#3
0
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']
    ssl = int(cfg.get('ssl', 0))
    web_root = cfg.get('web_root', '')
    protocol = 'https://' if ssl else 'http://'
    remote_path = int(cfg.get('remote_path', 0))

    url = '{0}{1}:{2}{3}/api'.format(protocol, host, port, web_root)
    if not server_responding(url):
        logger.error('Server did not respond. Exiting', section)
        return ProcessResult(
            message='{0}: Failed to post-process - {0} did not respond.'.format(section),
            status_code=1,
        )

    input_name, dir_name = convert_to_ascii(input_name, dir_name)

    params = {
        'apikey': apikey,
        'cmd': 'forceProcess',
        'dir': remote_dir(dir_name) if remote_path else dir_name,
    }
    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')
        return ProcessResult(
            message='{0}: Failed to post-process - Unable to connect to {1}'.format(section, section),
            status_code=1,
        )

    logger.postprocess('{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 ProcessResult(
            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: ForceProcess for {0} has been started in LazyLibrarian'.format(dir_name), section)
        return ProcessResult(
            message='{0}: Successfully post-processed {1}'.format(section, input_name),
            status_code=0,
        )
    else:
        logger.error('FAILED: ForceProcess of {0} has Failed in LazyLibrarian'.format(dir_name), section)
        return ProcessResult(
            message='{0}: Failed to post-process - Returned log from {0} was not as expected.'.format(section),
            status_code=1,
        )
示例#4
0
    def api_call(self):
        self._process_fork_prarams()
        url = self._create_url()

        logger.debug(
            'Opening URL: {0} with params: {1}'.format(
                url, self.sb_init.fork_params), self.sb_init.section)
        try:
            response = self.session.get(url,
                                        auth=(self.sb_init.username,
                                              self.sb_init.password),
                                        params=self.sb_init.fork_params,
                                        stream=True,
                                        verify=False,
                                        timeout=(30, 1800))
        except requests.ConnectionError:
            logger.error('Unable to open URL: {0}'.format(url),
                         self.sb_init.section)
            return ProcessResult(
                message='{0}: Failed to post-process - Unable to connect to {0}'
                .format(self.sb_init.section),
                status_code=1,
            )

        if response.status_code not in [
                requests.codes.ok, requests.codes.created,
                requests.codes.accepted
        ]:
            logger.error(
                'Server returned status {0}'.format(response.status_code),
                self.sb_init.section)
            return ProcessResult(
                message=
                '{0}: Failed to post-process - Server returned status {1}'.
                format(self.sb_init.section, response.status_code),
                status_code=1,
            )

        if response.json()['result'] == 'success':
            return ProcessResult(
                message='{0}: Successfully post-processed {1}'.format(
                    self.sb_init.section, self.input_name),
                status_code=0,
            )
        return ProcessResult(
            message=
            '{0}: Failed to post-process - Returned log from {0} was not as expected.'
            .format(self.sb_init.section),
            status_code=1,  # We did not receive Success confirmation.
        )
示例#5
0
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 ProcessResult(
            message='{0}: Failed to post-process - Unable to connect to {0}'.format(section),
            status_code=1,
        )

    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 ProcessResult(
            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)
    else:
        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),
            status_code=1,
        )

    # 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 ProcessResult(
                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)
            return ProcessResult(
                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',
        status_code=2,
    )
示例#6
0
def main(args):
    # Initialize the config
    core.initialize()

    # clientAgent for Torrents
    client_agent = core.TORRENT_CLIENTAGENT

    logger.info('#########################################################')
    logger.info('## ..::[{0}]::.. ##'.format(os.path.basename(__file__)))
    logger.info('#########################################################')

    # debug command line options
    logger.debug('Options passed into TorrentToMedia: {0}'.format(args))

    # Post-Processing Result
    result = ProcessResult(
        message='',
        status_code=0,
    )

    try:
        input_directory, input_name, input_category, input_hash, input_id = core.parse_args(client_agent, args)
    except Exception:
        logger.error('There was a problem loading variables')
        return -1

    if input_directory and input_name and input_hash and input_id:
        result = process_torrent(input_directory, input_name, input_category, input_hash, input_id, client_agent)
    else:
        # Perform Manual Post-Processing
        logger.warning('Invalid number of arguments received from client, Switching to manual run mode ...')

        for section, subsections in core.SECTIONS.items():
            for subsection in subsections:
                if not core.CFG[section][subsection].isenabled():
                    continue
                for dir_name in core.get_dirs(section, subsection, link='hard'):
                    logger.info('Starting manual run for {0}:{1} - Folder:{2}'.format
                                (section, subsection, dir_name))

                    logger.info('Checking database for download info for {0} ...'.format
                                (os.path.basename(dir_name)))
                    core.DOWNLOADINFO = core.get_download_info(os.path.basename(dir_name), 0)
                    if core.DOWNLOADINFO:
                        client_agent = text_type(core.DOWNLOADINFO[0].get('client_agent', 'manual'))
                        input_hash = text_type(core.DOWNLOADINFO[0].get('input_hash', ''))
                        input_id = text_type(core.DOWNLOADINFO[0].get('input_id', ''))
                        logger.info('Found download info for {0}, '
                                    'setting variables now ...'.format(os.path.basename(dir_name)))
                    else:
                        logger.info('Unable to locate download info for {0}, '
                                    'continuing to try and process this release ...'.format
                                    (os.path.basename(dir_name)))
                        client_agent = 'manual'
                        input_hash = ''
                        input_id = ''

                    if client_agent.lower() not in core.TORRENT_CLIENTS:
                        continue

                    try:
                        dir_name = dir_name.encode(core.SYS_ENCODING)
                    except UnicodeError:
                        pass
                    input_name = os.path.basename(dir_name)
                    try:
                        input_name = input_name.encode(core.SYS_ENCODING)
                    except UnicodeError:
                        pass

                    results = process_torrent(dir_name, input_name, subsection, input_hash or None, input_id or None,
                                              client_agent)
                    if results.status_code != 0:
                        logger.error('A problem was reported when trying to perform a manual run for {0}:{1}.'.format
                                     (section, subsection))
                        result = results

    if result.status_code == 0:
        logger.info('The {0} script completed successfully.'.format(args[0]))
    else:
        logger.error('A problem was reported in the {0} script.'.format(args[0]))
    del core.MYAPP
    return result.status_code
示例#7
0
def process_torrent(input_directory, input_name, input_category, input_hash, input_id, client_agent):
    status = 1  # 1 = failed | 0 = success
    root = 0
    found_file = 0

    if client_agent != 'manual' and not core.DOWNLOADINFO:
        logger.debug('Adding TORRENT download info for directory {0} to database'.format(input_directory))

        my_db = main_db.DBConnection()

        input_directory1 = input_directory
        input_name1 = input_name

        try:
            encoded, input_directory1 = char_replace(input_directory)
            encoded, input_name1 = char_replace(input_name)
        except Exception:
            pass

        control_value_dict = {'input_directory': text_type(input_directory1)}
        new_value_dict = {
            'input_name': text_type(input_name1),
            'input_hash': text_type(input_hash),
            'input_id': text_type(input_id),
            'client_agent': text_type(client_agent),
            'status': 0,
            'last_update': datetime.date.today().toordinal(),
        }
        my_db.upsert('downloads', new_value_dict, control_value_dict)

    logger.debug('Received Directory: {0} | Name: {1} | Category: {2}'.format(input_directory, input_name, input_category))

    # Confirm the category by parsing directory structure
    input_directory, input_name, input_category, root = core.category_search(input_directory, input_name, input_category,
                                                                             root, core.CATEGORIES)
    if input_category == '':
        input_category = 'UNCAT'

    usercat = input_category
    try:
        input_name = input_name.encode(core.SYS_ENCODING)
    except UnicodeError:
        pass
    try:
        input_directory = input_directory.encode(core.SYS_ENCODING)
    except UnicodeError:
        pass

    logger.debug('Determined Directory: {0} | Name: {1} | Category: {2}'.format
                 (input_directory, input_name, input_category))

    # auto-detect section
    section = core.CFG.findsection(input_category).isenabled()
    if section is None:
        section = core.CFG.findsection('ALL').isenabled()
        if section is None:
            logger.error('Category:[{0}] is not defined or is not enabled. '
                         'Please rename it or ensure it is enabled for the appropriate section '
                         'in your autoProcessMedia.cfg and try again.'.format
                         (input_category))
            return [-1, '']
        else:
            usercat = 'ALL'

    if len(section) > 1:
        logger.error('Category:[{0}] is not unique, {1} are using it. '
                     'Please rename it or disable all other sections using the same category name '
                     'in your autoProcessMedia.cfg and try again.'.format
                     (usercat, section.keys()))
        return [-1, '']

    if section:
        section_name = section.keys()[0]
        logger.info('Auto-detected SECTION:{0}'.format(section_name))
    else:
        logger.error('Unable to locate a section with subsection:{0} '
                     'enabled in your autoProcessMedia.cfg, exiting!'.format
                     (input_category))
        return [-1, '']

    section = dict(section[section_name][usercat])  # Type cast to dict() to allow effective usage of .get()

    torrent_no_link = int(section.get('Torrent_NoLink', 0))
    keep_archive = int(section.get('keep_archive', 0))
    extract = int(section.get('extract', 0))
    extensions = section.get('user_script_mediaExtensions', '').lower().split(',')
    unique_path = int(section.get('unique_path', 1))

    if client_agent != 'manual':
        core.pause_torrent(client_agent, input_hash, input_id, input_name)

    # In case input is not directory, make sure to create one.
    # This way Processing is isolated.
    if not os.path.isdir(os.path.join(input_directory, input_name)):
        basename = os.path.basename(input_directory)
        basename = core.sanitize_name(input_name) \
            if input_name == basename else os.path.splitext(core.sanitize_name(input_name))[0]
        output_destination = os.path.join(core.OUTPUTDIRECTORY, input_category, basename)
    elif unique_path:
        output_destination = os.path.normpath(
            core.os.path.join(core.OUTPUTDIRECTORY, input_category, core.sanitize_name(input_name).replace(' ', '.')))
    else:
        output_destination = os.path.normpath(
            core.os.path.join(core.OUTPUTDIRECTORY, input_category))
    try:
        output_destination = output_destination.encode(core.SYS_ENCODING)
    except UnicodeError:
        pass

    if output_destination in input_directory:
        output_destination = input_directory

    logger.info('Output directory set to: {0}'.format(output_destination))

    if core.SAFE_MODE and output_destination == core.TORRENT_DEFAULTDIR:
        logger.error('The output directory:[{0}] is the Download Directory. '
                     'Edit outputDirectory in autoProcessMedia.cfg. Exiting'.format
                     (input_directory))
        return [-1, '']

    logger.debug('Scanning files in directory: {0}'.format(input_directory))

    if section_name in ['HeadPhones', 'Lidarr']:
        core.NOFLATTEN.extend(
            input_category)  # Make sure we preserve folder structure for HeadPhones.

    now = datetime.datetime.now()

    if extract == 1:
        input_files = core.list_media_files(input_directory, archives=False, other=True, otherext=extensions)
    else:
        input_files = core.list_media_files(input_directory, other=True, otherext=extensions)
    if len(input_files) == 0 and os.path.isfile(input_directory):
        input_files = [input_directory]
        logger.debug('Found 1 file to process: {0}'.format(input_directory))
    else:
        logger.debug('Found {0} files in {1}'.format(len(input_files), input_directory))
    for inputFile in input_files:
        file_path = os.path.dirname(inputFile)
        file_name, file_ext = os.path.splitext(os.path.basename(inputFile))
        full_file_name = os.path.basename(inputFile)

        target_file = core.os.path.join(output_destination, full_file_name)
        if input_category in core.NOFLATTEN:
            if not os.path.basename(file_path) in output_destination:
                target_file = core.os.path.join(
                    core.os.path.join(output_destination, os.path.basename(file_path)), full_file_name)
                logger.debug('Setting outputDestination to {0} to preserve folder structure'.format
                             (os.path.dirname(target_file)))
        try:
            target_file = target_file.encode(core.SYS_ENCODING)
        except UnicodeError:
            pass
        if root == 1:
            if not found_file:
                logger.debug('Looking for {0} in: {1}'.format(input_name, inputFile))
            if any([core.sanitize_name(input_name) in core.sanitize_name(inputFile),
                    core.sanitize_name(file_name) in core.sanitize_name(input_name)]):
                found_file = True
                logger.debug('Found file {0} that matches Torrent Name {1}'.format
                             (full_file_name, input_name))
            else:
                continue

        if root == 2:
            mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(inputFile))
            ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(inputFile))

            if not found_file:
                logger.debug('Looking for files with modified/created dates less than 5 minutes old.')
            if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)):
                found_file = True
                logger.debug('Found file {0} with date modified/created less than 5 minutes ago.'.format
                             (full_file_name))
            else:
                continue  # This file has not been recently moved or created, skip it

        if torrent_no_link == 0:
            try:
                core.copy_link(inputFile, target_file, core.USELINK)
                core.remove_read_only(target_file)
            except Exception:
                logger.error('Failed to link: {0} to {1}'.format(inputFile, target_file))

    input_name, output_destination = convert_to_ascii(input_name, output_destination)

    if extract == 1:
        logger.debug('Checking for archives to extract in directory: {0}'.format(input_directory))
        core.extract_files(input_directory, output_destination, keep_archive)

    if input_category not in core.NOFLATTEN:
        # don't flatten hp in case multi cd albums, and we need to copy this back later.
        core.flatten(output_destination)

    # Now check if video files exist in destination:
    if section_name in ['SickBeard', 'NzbDrone', 'Sonarr', 'CouchPotato', 'Radarr']:
        num_videos = len(
            core.list_media_files(output_destination, media=True, audio=False, meta=False, archives=False))
        if num_videos > 0:
            logger.info('Found {0} media files in {1}'.format(num_videos, output_destination))
            status = 0
        elif extract != 1:
            logger.info('Found no media files in {0}. Sending to {1} to process'.format(output_destination, section_name))
            status = 0
        else:
            logger.warning('Found no media files in {0}'.format(output_destination))

    # Only these sections can handling failed downloads
    # so make sure everything else gets through without the check for failed
    if section_name not in ['CouchPotato', 'Radarr', 'SickBeard', 'NzbDrone', 'Sonarr']:
        status = 0

    logger.info('Calling {0}:{1} to post-process:{2}'.format(section_name, usercat, input_name))

    if core.TORRENT_CHMOD_DIRECTORY:
        core.rchmod(output_destination, core.TORRENT_CHMOD_DIRECTORY)

    result = ProcessResult(
        message='',
        status_code=0,
    )
    if section_name == 'UserScript':
        result = external_script(output_destination, input_name, input_category, section)
    elif section_name in ['CouchPotato', 'Radarr']:
        result = movies.process(section_name, output_destination, input_name, status, client_agent, input_hash, input_category)
    elif section_name in ['SickBeard', 'NzbDrone', 'Sonarr']:
        if input_hash:
            input_hash = input_hash.upper()
        result = tv.process(section_name, output_destination, input_name, status, client_agent, input_hash, input_category)
    elif section_name in ['HeadPhones', 'Lidarr']:
        result = music.process(section_name, output_destination, input_name, status, client_agent, input_category)
    elif section_name == 'Mylar':
        result = comics.process(section_name, output_destination, input_name, status, client_agent, input_category)
    elif section_name == 'Gamez':
        result = games.process(section_name, output_destination, input_name, status, client_agent, input_category)

    plex_update(input_category)

    if result.status_code != 0:
        if not core.TORRENT_RESUME_ON_FAILURE:
            logger.error('A problem was reported in the autoProcess* script. '
                         'Torrent won\'t resume seeding (settings)')
        elif client_agent != 'manual':
            logger.error('A problem was reported in the autoProcess* script. '
                         'If torrent was paused we will resume seeding')
            core.resume_torrent(client_agent, input_hash, input_id, input_name)

    else:
        if client_agent != 'manual':
            # update download status in our DB
            core.update_download_info_status(input_name, 1)

            # remove torrent
            if core.USELINK == 'move-sym' and not core.DELETE_ORIGINAL == 1:
                logger.debug('Checking for sym-links to re-direct in: {0}'.format(input_directory))
                for dirpath, dirs, files in os.walk(input_directory):
                    for file in files:
                        logger.debug('Checking symlink: {0}'.format(os.path.join(dirpath, file)))
                        replace_links(os.path.join(dirpath, file))
            core.remove_torrent(client_agent, input_hash, input_id, input_name)

        if not section_name == 'UserScript':
            # for user script, we assume this is cleaned by the script or option USER_SCRIPT_CLEAN
            # cleanup our processing folders of any misc unwanted files and empty directories
            core.clean_dir(output_destination, section_name, input_category)

    return result
示例#8
0
    def api_call(self):
        self._process_fork_prarams()
        url = self._create_url()

        logger.debug('Opening URL: {0}'.format(url), self.sb_init.section)
        payload = self.sb_init.fork_params
        payload['resource'] = self.sb_init.fork_params['nzbName']
        del payload['nzbName']

        # Update the session with the x-api-key
        self.session.headers.update({
            'x-api-key': self.sb_init.apikey,
            'Content-type': 'application/json'
        })

        # Send postprocess request
        try:
            response = self.session.post(url,
                                         json=payload,
                                         verify=False,
                                         timeout=(30, 1800))
        except requests.ConnectionError:
            logger.error('Unable to send postprocess request',
                         self.sb_init.section)
            return ProcessResult(
                message='{0}: Unable to send postprocess request to PyMedusa',
                status_code=1,
            )

        # Get UUID
        if response:
            try:
                jdata = response.json()
            except ValueError:
                logger.debug('No data returned from provider')
                return False

        if not jdata.get('status') or not jdata['status'] == 'success':
            return False

        queueitem_identifier = jdata['queueItem']['identifier']

        wait_for = int(self.sb_init.config.get('wait_for', 2))
        n = 0
        response = {}
        url = '{0}/{1}'.format(url, queueitem_identifier)
        while n < 12:  # set up wait_for minutes to see if command completes..
            time.sleep(5 * wait_for)
            response = self._get_identifier_status(url)
            if response and response.get('success'):
                break
            if 'error' in response:
                break
            n += 1

        # Log Medusa's PP logs here.
        if response.get('output'):
            for line in response['output']:
                logger.postprocess('{0}'.format(line), self.sb_init.section)

        # For now this will most likely always be True. But in the future we could return an exit state
        # for when the PP in medusa didn't yield an expected result.
        if response.get('success'):
            return ProcessResult(
                message='{0}: Successfully post-processed {1}'.format(
                    self.sb_init.section, self.input_name),
                status_code=0,
            )
        return ProcessResult(
            message=
            '{0}: Failed to post-process - Returned log from {0} was not as expected.'
            .format(self.sb_init.section),
            status_code=1,  # We did not receive Success confirmation.
        )
示例#9
0
def main(args, section=None):
    # Initialize the config
    core.initialize(section)

    logger.info('#########################################################')
    logger.info('## ..::[{0}]::.. ##'.format(os.path.basename(__file__)))
    logger.info('#########################################################')

    # debug command line options
    logger.debug('Options passed into nzbToMedia: {0}'.format(args))

    # Post-Processing Result
    result = ProcessResult(
        message='',
        status_code=0,
    )
    status = 0

    # NZBGet
    if 'NZBOP_SCRIPTDIR' in os.environ:
        # Check if the script is called from nzbget 11.0 or later
        if os.environ['NZBOP_VERSION'][0:5] < '11.0':
            logger.error('NZBGet Version {0} is not supported. Please update NZBGet.'.format(os.environ['NZBOP_VERSION']))
            sys.exit(core.NZBGET_POSTPROCESS_ERROR)

        logger.info('Script triggered from NZBGet Version {0}.'.format(os.environ['NZBOP_VERSION']))

        # Check if the script is called from nzbget 13.0 or later
        if 'NZBPP_TOTALSTATUS' in os.environ:
            if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS':
                logger.info('Download failed with status {0}.'.format(os.environ['NZBPP_STATUS']))
                status = 1

        else:
            # Check par status
            if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4':
                logger.warning('Par-repair failed, setting status \'failed\'')
                status = 1

            # Check unpack status
            if os.environ['NZBPP_UNPACKSTATUS'] == '1':
                logger.warning('Unpack failed, setting status \'failed\'')
                status = 1

            if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ['NZBPP_PARSTATUS'] == '0':
                # Unpack was skipped due to nzb-file properties or due to errors during par-check

                if os.environ['NZBPP_HEALTH'] < 1000:
                    logger.warning(
                        'Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \'failed\'')
                    logger.info('Please check your Par-check/repair settings for future downloads.')
                    status = 1

                else:
                    logger.info(
                        'Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful')
                    logger.info('Please check your Par-check/repair settings for future downloads.')

        # Check for download_id to pass to CouchPotato
        download_id = ''
        failure_link = None
        if 'NZBPR_COUCHPOTATO' in os.environ:
            download_id = os.environ['NZBPR_COUCHPOTATO']
        elif 'NZBPR_DRONE' in os.environ:
            download_id = os.environ['NZBPR_DRONE']
        elif 'NZBPR_SONARR' in os.environ:
            download_id = os.environ['NZBPR_SONARR']
        elif 'NZBPR_RADARR' in os.environ:
            download_id = os.environ['NZBPR_RADARR']
        elif 'NZBPR_LIDARR' in os.environ:
            download_id = os.environ['NZBPR_LIDARR']
        if 'NZBPR__DNZB_FAILURE' in os.environ:
            failure_link = os.environ['NZBPR__DNZB_FAILURE']

        # All checks done, now launching the script.
        client_agent = 'nzbget'
        result = process(os.environ['NZBPP_DIRECTORY'], input_name=os.environ['NZBPP_NZBNAME'], status=status,
                         client_agent=client_agent, download_id=download_id, input_category=os.environ['NZBPP_CATEGORY'],
                         failure_link=failure_link)
    # SABnzbd Pre 0.7.17
    elif len(args) == core.SABNZB_NO_OF_ARGUMENTS:
        # SABnzbd argv:
        # 1 The final directory of the job (full path)
        # 2 The original name of the NZB file
        # 3 Clean version of the job name (no path info and '.nzb' removed)
        # 4 Indexer's report number (if supported)
        # 5 User-defined category
        # 6 Group that the NZB was posted in e.g. alt.binaries.x
        # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2
        client_agent = 'sabnzbd'
        logger.info('Script triggered from SABnzbd')
        result = process(args[1], input_name=args[2], status=int(args[7]), input_category=args[5], client_agent=client_agent,
                         download_id='')
    # SABnzbd 0.7.17+
    elif len(args) >= core.SABNZB_0717_NO_OF_ARGUMENTS:
        # SABnzbd argv:
        # 1 The final directory of the job (full path)
        # 2 The original name of the NZB file
        # 3 Clean version of the job name (no path info and '.nzb' removed)
        # 4 Indexer's report number (if supported)
        # 5 User-defined category
        # 6 Group that the NZB was posted in e.g. alt.binaries.x
        # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2
        # 8 Failure URL
        client_agent = 'sabnzbd'
        logger.info('Script triggered from SABnzbd 0.7.17+')
        result = process(args[1], input_name=args[2], status=int(args[7]), input_category=args[5], client_agent=client_agent,
                         download_id='', failure_link=''.join(args[8:]))
    # Generic program
    elif len(args) > 5 and args[5] == 'generic':
        logger.info('Script triggered from generic program')
        result = process(args[1], input_name=args[2], input_category=args[3], download_id=args[4])
    else:
        # Perform Manual Post-Processing
        logger.warning('Invalid number of arguments received from client, Switching to manual run mode ...')

        for section, subsections in core.SECTIONS.items():
            for subsection in subsections:
                if not core.CFG[section][subsection].isenabled():
                    continue
                for dir_name in get_dirs(section, subsection, link='move'):
                    logger.info('Starting manual run for {0}:{1} - Folder: {2}'.format(section, subsection, dir_name))
                    logger.info('Checking database for download info for {0} ...'.format(os.path.basename(dir_name)))

                    core.DOWNLOAD_INFO = get_download_info(os.path.basename(dir_name), 0)
                    if core.DOWNLOAD_INFO:
                        logger.info('Found download info for {0}, '
                                    'setting variables now ...'.format
                                    (os.path.basename(dir_name)))
                        client_agent = text_type(core.DOWNLOAD_INFO[0].get('client_agent', 'manual'))
                        download_id = text_type(core.DOWNLOAD_INFO[0].get('input_id', ''))
                    else:
                        logger.info('Unable to locate download info for {0}, '
                                    'continuing to try and process this release ...'.format
                                    (os.path.basename(dir_name)))
                        client_agent = 'manual'
                        download_id = ''

                    if client_agent and client_agent.lower() not in core.NZB_CLIENTS:
                        continue

                    input_name = os.path.basename(dir_name)

                    results = process(dir_name, input_name, 0, client_agent=client_agent,
                                      download_id=download_id or None, input_category=subsection)
                    if results.status_code != 0:
                        logger.error('A problem was reported when trying to perform a manual run for {0}:{1}.'.format
                                     (section, subsection))
                        result = results

    if result.status_code == 0:
        logger.info('The {0} script completed successfully.'.format(args[0]))
        if result.message:
            print(result.message + '!')
        if 'NZBOP_SCRIPTDIR' in os.environ:  # return code for nzbget v11
            del core.MYAPP
            return core.NZBGET_POSTPROCESS_SUCCESS
    else:
        logger.error('A problem was reported in the {0} script.'.format(args[0]))
        if result.message:
            print(result.message + '!')
        if 'NZBOP_SCRIPTDIR' in os.environ:  # return code for nzbget v11
            del core.MYAPP
            return core.NZBGET_POSTPROCESS_ERROR
    del core.MYAPP
    return result.status_code
示例#10
0
def process(input_directory, input_name=None, status=0, client_agent='manual', download_id=None, input_category=None, failure_link=None):
    if core.SAFE_MODE and input_directory == core.NZB_DEFAULT_DIRECTORY:
        logger.error(
            'The input directory:[{0}] is the Default Download Directory. Please configure category directories to prevent processing of other media.'.format(
                input_directory))
        return ProcessResult(
            message='',
            status_code=-1,
        )

    if not download_id and client_agent == 'sabnzbd':
        download_id = get_nzoid(input_name)

    if client_agent != 'manual' and not core.DOWNLOAD_INFO:
        logger.debug('Adding NZB download info for directory {0} to database'.format(input_directory))

        my_db = main_db.DBConnection()

        input_directory1 = input_directory
        input_name1 = input_name

        try:
            encoded, input_directory1 = char_replace(input_directory)
            encoded, input_name1 = char_replace(input_name)
        except Exception:
            pass

        control_value_dict = {'input_directory': text_type(input_directory1)}
        new_value_dict = {
            'input_name': text_type(input_name1),
            'input_hash': text_type(download_id),
            'input_id': text_type(download_id),
            'client_agent': text_type(client_agent),
            'status': 0,
            'last_update': datetime.date.today().toordinal(),
        }
        my_db.upsert('downloads', new_value_dict, control_value_dict)

    # auto-detect section
    if input_category is None:
        input_category = 'UNCAT'
    usercat = input_category
    section = core.CFG.findsection(input_category).isenabled()
    if section is None:
        section = core.CFG.findsection('ALL').isenabled()
        if section is None:
            logger.error(
                'Category:[{0}] is not defined or is not enabled. Please rename it or ensure it is enabled for the appropriate section in your autoProcessMedia.cfg and try again.'.format(
                    input_category))
            return ProcessResult(
                message='',
                status_code=-1,
            )
        else:
            usercat = 'ALL'

    if len(section) > 1:
        logger.error(
            'Category:[{0}] is not unique, {1} are using it. Please rename it or disable all other sections using the same category name in your autoProcessMedia.cfg and try again.'.format(
                input_category, section.keys()))
        return ProcessResult(
            message='',
            status_code=-1,
        )

    if section:
        section_name = section.keys()[0]
        logger.info('Auto-detected SECTION:{0}'.format(section_name))
    else:
        logger.error('Unable to locate a section with subsection:{0} enabled in your autoProcessMedia.cfg, exiting!'.format(
            input_category))
        return ProcessResult(
            status_code=-1,
            message='',
        )

    cfg = dict(core.CFG[section_name][usercat])

    extract = int(cfg.get('extract', 0))

    try:
        if int(cfg.get('remote_path')) and not core.REMOTE_PATHS:
            logger.error('Remote Path is enabled for {0}:{1} but no Network mount points are defined. Please check your autoProcessMedia.cfg, exiting!'.format(
                section_name, input_category))
            return ProcessResult(
                status_code=-1,
                message='',
            )
    except Exception:
        logger.error('Remote Path {0} is not valid for {1}:{2} Please set this to either 0 to disable or 1 to enable!'.format(
            cfg.get('remote_path'), section_name, input_category))

    input_name, input_directory = convert_to_ascii(input_name, input_directory)

    if extract == 1 and not (status > 0 and core.NOEXTRACTFAILED):
        logger.debug('Checking for archives to extract in directory: {0}'.format(input_directory))
        extract_files(input_directory)

    logger.info('Calling {0}:{1} to post-process:{2}'.format(section_name, input_category, input_name))

    if section_name in ['CouchPotato', 'Radarr', 'Watcher3']:
        result = movies.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link)
    elif section_name in ['SickBeard', 'NzbDrone', 'Sonarr']:
        result = tv.process(section_name, input_directory, input_name, status, client_agent, download_id, input_category, failure_link)
    elif section_name in ['HeadPhones', 'Lidarr']:
        result = music.process(section_name, input_directory, input_name, status, client_agent, input_category)
    elif section_name == 'Mylar':
        result = comics.process(section_name, input_directory, input_name, status, client_agent, input_category)
    elif section_name == 'Gamez':
        result = games.process(section_name, input_directory, input_name, status, client_agent, input_category)
    elif section_name == 'LazyLibrarian':
        result = books.process(section_name, input_directory, input_name, status, client_agent, input_category)
    elif section_name == 'UserScript':
        result = external_script(input_directory, input_name, input_category, section[usercat])
    else:
        result = ProcessResult(
            message='',
            status_code=-1,
        )

    plex_update(input_category)

    if result.status_code == 0:
        if client_agent != 'manual':
            # update download status in our DB
            update_download_info_status(input_name, 1)
        if section_name not in ['UserScript', 'NzbDrone', 'Sonarr', 'Radarr', 'Lidarr']:
            # cleanup our processing folders of any misc unwanted files and empty directories
            clean_dir(input_directory, section_name, input_category)

    return result
示例#11
0
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://'
    status = int(status)
    if status > 0 and core.NOEXTRACTFAILED:
        extract = 0
    else:
        extract = int(cfg.get('extract', 0))

    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)
    if not server_responding(url):
        logger.error('Server did not respond. Exiting', section)
        return ProcessResult(
            message='{0}: Failed to post-process - {0} did not respond.'.
            format(section),
            status_code=1,
        )

    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:
        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 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 status == 0 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.
        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),
            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}
        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 ProcessResult(
                message='{0}: Failed to post-process - Unable to connect to {0}'
                .format(section),
                status_code=1,
            )

        try:
            res = r.json()
            scan_id = int(res['id'])
            logger.debug('Scan started with id: {0}'.format(scan_id), section)
        except Exception as e:
            logger.warning('No scan id was returned due to: {0}'.format(e),
                           section)
            return ProcessResult(
                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)
        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 ProcessResult(
                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)
            return ProcessResult(
                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)
            # return ProcessResult(
            #     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)
            return ProcessResult(
                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)
            return ProcessResult(
                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)
            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 ProcessResult(
                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.
            )
示例#12
0
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', '')

    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', {}
    else:
        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),
        )

    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_CLIENT_AGENT and core.USE_LINK == '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')

    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

    # 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
    # won't process the directory because it doesn't exist.
    if dir_name:
        try:
            os.makedirs(dir_name)  # Attempt to create the directory
        except OSError as e:
            # Re-raise the error if it wasn't about the directory not existing
            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 input_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)

        if list_media_files(
                dir_name, media=True, audio=False, meta=False, archives=False
        ):  # Check that a video exists. if not, assume failed.
            flatten(dir_name)

    # Check video files for corruption
    good_files = 0
    num_files = 0
    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):
            good_files += 1
            import_subs(video)
    if num_files > 0:
        if good_files == num_files and not status == 0:
            logger.info('Found Valid Videos. Setting status Success')
            status = 0
            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 ProcessResult(
            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.'
        )
        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
        failed = 1
        if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][
                0:5] >= '14.0':
            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 ProcessResult(
                message='{0}: Failed to post-process - Transcoding failed'.
                format(section),
                status_code=1,
            )

    # 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
            if 'proc_type' in fork_params:
                del fork_params['proc_type']
            if 'type' in fork_params:
                del fork_params['type']

        if param == 'return_data':
            fork_params[param] = 0
            if 'quiet' in fork_params:
                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 list(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 ProcessResult(
                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)
    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)
            return ProcessResult(
                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)
            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 ProcessResult(
                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 apikey:
            url = '{0}{1}:{2}{3}/api/{4}/'.format(protocol, host, port,
                                                  web_root, apikey)
            if not 'cmd' in fork_params:
                if 'SickGear' in fork:
                    fork_params['cmd'] = 'sg.postprocess'
                else:
                    fork_params['cmd'] = 'postprocess'
        elif fork == 'Stheno':
            url = '{0}{1}:{2}{3}/home/postprocess/process_episode'.format(
                protocol, host, port, web_root)
        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}
        # 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)

    try:
        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 = '******'.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 in [401, 403] 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 ProcessResult(
            message='{0}: Failed to post-process - Unable to connect to {0}'.
            format(section),
            status_code=1,
        )

    if r.status_code not in [
            requests.codes.ok, requests.codes.created, requests.codes.accepted
    ]:
        logger.error('Server returned status {0}'.format(r.status_code),
                     section)
        return ProcessResult(
            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 apikey:
            if r.json()['result'] == 'success':
                success = True
        else:
            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:
                        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 = r.json()
            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 ProcessResult(
            message='{0}: Successfully post-processed {1}'.format(
                section, input_name),
            status_code=0,
        )
    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 ProcessResult(
                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)
            return ProcessResult(
                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)
            # return ProcessResult(
            #     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)
            return ProcessResult(
                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)
            return ProcessResult(
                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),
            status_code=1,  # We did not receive Success confirmation.
        )
示例#13
0
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']
    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)
    if not server_responding(url):
        logger.error('Server did not respond. Exiting', section)
        return ProcessResult(
            message='{0}: Failed to post-process - {0} did not respond.'.
            format(section),
            status_code=1,
        )

    input_name, dir_name = convert_to_ascii(input_name, dir_name)

    fields = input_name.split('-')

    gamez_id = fields[0].replace('[', '').replace(']', '').replace(' ', '')

    download_status = 'Downloaded' if status == 0 else 'Wanted'

    params = {
        'api_key': apikey,
        'mode': 'UPDATEREQUESTEDSTATUS',
        'db_id': gamez_id,
        'status': download_status,
    }

    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 ProcessResult(
            message='{0}: Failed to post-process - Unable to connect to {1}'.
            format(section, section),
            status_code=1,
        )

    result = r.json()
    logger.postprocess('{0}'.format(result), section)
    if library:
        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)
            return ProcessResult(
                message='{0}: Failed to post-process - Unable to move files'.
                format(section),
                status_code=1,
            )
    else:
        logger.error(
            'No library specified to move files to. Please edit your configuration.',
            section)
        return ProcessResult(
            message='{0}: Failed to post-process - No library defined in {0}'.
            format(section),
            status_code=1,
        )

    if r.status_code not in [
            requests.codes.ok, requests.codes.created, requests.codes.accepted
    ]:
        logger.error('Server returned status {0}'.format(r.status_code),
                     section)
        return ProcessResult(
            message='{0}: Failed to post-process - Server returned status {1}'.
            format(section, r.status_code),
            status_code=1,
        )
    elif result['success']:
        logger.postprocess(
            'SUCCESS: Status for {0} has been set to {1} in Gamez'.format(
                gamez_id, download_status), section)
        return ProcessResult(
            message='{0}: Successfully post-processed {1}'.format(
                section, input_name),
            status_code=0,
        )
    else:
        logger.error(
            'FAILED: Status for {0} has NOT been updated in Gamez'.format(
                gamez_id), section)
        return ProcessResult(
            message=
            '{0}: Failed to post-process - Returned log from {0} was not as expected.'
            .format(section),
            status_code=1,
        )
示例#14
0
def process(section,
            dir_name,
            input_name=None,
            status=0,
            client_agent='manual',
            input_category=None):
    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://'

    url = '{0}{1}:{2}{3}/api'.format(protocol, host, port, web_root)
    if not server_responding(url):
        logger.error('Server did not respond. Exiting', section)
        return ProcessResult(
            message='{0}: Failed to post-process - {0} did not respond.'.
            format(section),
            status_code=1,
        )

    input_name, dir_name = convert_to_ascii(input_name, dir_name)
    clean_name, ext = os.path.splitext(input_name)
    if len(ext) == 4:  # we assume this was a standard extension.
        input_name = clean_name

    params = {
        'cmd': 'forceProcess',
        'apikey': apikey,
        'nzb_folder': remote_dir(dir_name) if remote_path else dir_name,
    }

    if input_name is not None:
        params['nzb_name'] = input_name
    params['failed'] = int(status)
    params['apc_version'] = apc_version
    params['comicrn_version'] = comicrn_version

    success = False

    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)
        return ProcessResult(
            message='{0}: Failed to post-process - Unable to connect to {0}'.
            format(section),
            status_code=1,
        )
    if r.status_code not in [
            requests.codes.ok, requests.codes.created, requests.codes.accepted
    ]:
        logger.error('Server returned status {0}'.format(r.status_code),
                     section)
        return ProcessResult(
            message='{0}: Failed to post-process - Server returned status {1}'.
            format(section, r.status_code),
            status_code=1,
        )

    result = r.content
    if not type(result) == list:
        result = result.split('\n')
    for line in result:
        if 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)
        return ProcessResult(
            message='{0}: Successfully post-processed {1}'.format(
                section, input_name),
            status_code=0,
        )
    else:
        logger.warning(
            'The issue does not appear to have successfully processed. Please check your Logs',
            section)
        return ProcessResult(
            message=
            '{0}: Failed to post-process - Returned log from {0} was not as expected.'
            .format(section),
            status_code=1,
        )
示例#15
0
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']
    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 section == 'Watcher3':
        base_url = '{0}{1}:{2}{3}/postprocessing'.format(
            protocol, host, port, web_root)
    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':
            release = get_release(base_url, imdbid, download_id)
        else:
            release = None
    else:
        logger.error('Server did not respond. Exiting', section)
        return ProcessResult(
            message='{0}: Failed to post-process - {0} did not respond.'.
            format(section),
            status_code=1,
        )

    # pull info from release found if available
    release_id = None
    media_id = None
    downloader = None
    release_status_old = None
    if release:
        try:
            release_id = list(release.keys())[0]
            media_id = release[release_id]['media_id']
            download_id = release[release_id]['download_info']['id']
            downloader = release[release_id]['download_info']['downloader']
            release_status_old = release[release_id]['status']
        except Exception:
            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.
        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=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)

    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):
        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 ProcessResult(
            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)
        status = 1
        if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][
                0:5] >= '14.0':
            print('[NZB] MARK=BAD')

    if status == 0:
        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)
                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 ProcessResult(
                    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:
                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_CLIENT_AGENT, 'manual']
                        and core.USE_LINK == '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 or Watcher3 apikey entered. Processing completed.'
            )
            return ProcessResult(
                message='{0}: Successfully post-processed {1}'.format(
                    section, input_name),
                status_code=0,
            )

        params = {
            'media_folder': remote_dir(dir_name) if remote_path else dir_name,
        }

        if download_id and release_id:
            params['downloader'] = downloader or client_agent
            params['download_id'] = download_id

        if section == 'CouchPotato':
            if method == 'manage':
                command = 'manage.update'
                params.clear()
            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)

        if section == 'Watcher3':
            if input_name and os.path.isfile(os.path.join(
                    dir_name, input_name)):
                params['media_folder'] = os.path.join(params['media_folder'],
                                                      input_name)
            payload = {
                'apikey': apikey,
                'path': params['media_folder'],
                'guid': download_id,
                'mode': 'complete'
            }
            if not download_id:
                payload.pop('guid')
            logger.debug(
                'Opening URL: {0} with PARAMS: {1}'.format(base_url, payload),
                section)
            logger.postprocess(
                'Starting postprocessing scan for {0}'.format(input_name),
                section)

        try:
            if section == 'CouchPotato':
                r = requests.get(url,
                                 params=params,
                                 verify=False,
                                 timeout=(30, 1800))
            elif section == 'Watcher3':
                r = requests.post(base_url,
                                  data=payload,
                                  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 ProcessResult(
                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)
            return ProcessResult(
                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':
                return ProcessResult(
                    message='{0}: Successfully post-processed {1}'.format(
                        section, input_name),
                    status_code=0,
                )
        elif section == 'Radarr':
            logger.postprocess('Radarr response: {0}'.format(result['state']))
            try:
                scan_id = int(result['id'])
                logger.debug('Scan started with id: {0}'.format(scan_id),
                             section)
            except Exception as e:
                logger.warning('No scan id was returned due to: {0}'.format(e),
                               section)
                scan_id = None
        elif section == 'Watcher3' and result['status'] == 'finished':
            logger.postprocess('Watcher3 updated status to {0}'.format(
                result['tasks']['update_movie_status']))
            if result['tasks']['update_movie_status'] == 'Finished':
                return ProcessResult(
                    message='{0}: Successfully post-processed {1}'.format(
                        section, input_name),
                    status_code=status,
                )
            else:
                return ProcessResult(
                    message='{0}: Failed to post-process - changed status to {1}'
                    .format(section, result['tasks']['update_movie_status']),
                    status_code=1,
                )
        else:
            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),
                status_code=1,
            )
    else:
        core.FAILED = True
        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(
                'SUCCESS: Sending failed download to {0} for CDH processing'.
                format(section), section)
            return ProcessResult(
                message='{0}: Sending failed download back to {0}'.format(
                    section),
                status_code=
                1,  # Return as failed to flag this in the downloader.
            )  # Return failed flag, but log the event as successful.
        elif section == 'Watcher3':
            logger.postprocess(
                'Sending failed download to {0} for CDH processing'.format(
                    section), section)
            path = remote_dir(dir_name) if remote_path else dir_name
            if input_name and os.path.isfile(os.path.join(
                    dir_name, input_name)):
                path = os.path.join(path, input_name)
            payload = {
                'apikey': apikey,
                'path': path,
                'guid': download_id,
                'mode': 'failed'
            }
            r = requests.post(base_url,
                              data=payload,
                              verify=False,
                              timeout=(30, 1800))
            result = r.json()
            logger.postprocess('Watcher3 response: {0}'.format(result))
            if result['status'] == 'finished':
                return ProcessResult(
                    message='{0}: Sending failed download back to {0}'.format(
                        section),
                    status_code=
                    1,  # Return as failed to flag this in the downloader.
                )  # Return failed flag, but log the event as successful.

        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)

        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), section)
            return ProcessResult(
                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)

            url = '{url}release.ignore'.format(url=base_url)
            params = {'id': release_id}

            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)
                return ProcessResult(
                    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)
                return ProcessResult(
                    status_code=1,
                    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)
            else:
                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),
                    status_code=1,
                )

        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:
            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)
            return ProcessResult(
                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)
            return ProcessResult(
                message=
                '{0}: Failed to post-process - Server returned status {1}'.
                format(section, r.status_code),
                status_code=1,
            )
        elif result['success']:
            logger.postprocess(
                'SUCCESS: Snatched the next highest release ...', section)
            return ProcessResult(
                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)
            return ProcessResult(
                status_code=0,
                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.
    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.
    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':
            release = 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 ProcessResult(
                        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(title,
                                str(release_status_new).upper()), section)
                    return ProcessResult(
                        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)
            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 ProcessResult(
                        message='{0}: Successfully post-processed {1}'.format(
                            section, input_name),
                        status_code=0,
                    )
                elif command_status in ['failed']:
                    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),
                    #     status_code=1,
                    # )

        if not os.path.isdir(dir_name):
            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),
            )

        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 ProcessResult(
                message='{0}: Successfully post-processed {1}'.format(
                    section, input_name),
                status_code=0,
            )

        # 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 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),
            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),
        section,
    )
    return ProcessResult(
        status_code=1,
        message='{0}: Failed to post-process - No change in status'.format(
            section),
    )
def external_script(output_destination, torrent_name, torrent_label, settings):
    final_result = 0  # start at 0.
    num_files = 0
    core.USER_SCRIPT_MEDIAEXTENSIONS = settings.get('user_script_mediaExtensions', '')
    try:
        if isinstance(core.USER_SCRIPT_MEDIAEXTENSIONS, str):
            core.USER_SCRIPT_MEDIAEXTENSIONS = core.USER_SCRIPT_MEDIAEXTENSIONS.lower().split(',')
    except Exception:
        logger.error('user_script_mediaExtensions could not be set', 'USERSCRIPT')
        core.USER_SCRIPT_MEDIAEXTENSIONS = []

    core.USER_SCRIPT = settings.get('user_script_path', '')

    if not core.USER_SCRIPT or core.USER_SCRIPT == 'None':
        # do nothing and return success. This allows the user an option to Link files only and not run a script.
        return ProcessResult(
            status_code=0,
            message='No user script defined',
        )

    core.USER_SCRIPT_PARAM = settings.get('user_script_param', '')
    try:
        if isinstance(core.USER_SCRIPT_PARAM, str):
            core.USER_SCRIPT_PARAM = core.USER_SCRIPT_PARAM.split(',')
    except Exception:
        logger.error('user_script_params could not be set', 'USERSCRIPT')
        core.USER_SCRIPT_PARAM = []

    core.USER_SCRIPT_SUCCESSCODES = settings.get('user_script_successCodes', 0)
    try:
        if isinstance(core.USER_SCRIPT_SUCCESSCODES, str):
            core.USER_SCRIPT_SUCCESSCODES = core.USER_SCRIPT_SUCCESSCODES.split(',')
    except Exception:
        logger.error('user_script_successCodes could not be set', 'USERSCRIPT')
        core.USER_SCRIPT_SUCCESSCODES = 0

    core.USER_SCRIPT_CLEAN = int(settings.get('user_script_clean', 1))
    core.USER_SCRIPT_RUNONCE = int(settings.get('user_script_runOnce', 1))

    if core.CHECK_MEDIA:
        for video in list_media_files(output_destination, media=True, audio=False, meta=False, archives=False):
            if transcoder.is_video_good(video, 0):
                import_subs(video)
            else:
                logger.info('Corrupt video file found {0}. Deleting.'.format(video), 'USERSCRIPT')
                os.unlink(video)

    for dirpath, _, filenames in os.walk(output_destination):
        for file in filenames:

            file_path = core.os.path.join(dirpath, file)
            file_name, file_extension = os.path.splitext(file)
            logger.debug('Checking file {0} to see if this should be processed.'.format(file), 'USERSCRIPT')

            if file_extension in core.USER_SCRIPT_MEDIAEXTENSIONS or 'all' in core.USER_SCRIPT_MEDIAEXTENSIONS:
                num_files += 1
                if core.USER_SCRIPT_RUNONCE == 1 and num_files > 1:  # we have already run once, so just continue to get number of files.
                    continue
                command = [core.USER_SCRIPT]
                for param in core.USER_SCRIPT_PARAM:
                    if param == 'FN':
                        command.append('{0}'.format(file))
                        continue
                    elif param == 'FP':
                        command.append('{0}'.format(file_path))
                        continue
                    elif param == 'TN':
                        command.append('{0}'.format(torrent_name))
                        continue
                    elif param == 'TL':
                        command.append('{0}'.format(torrent_label))
                        continue
                    elif param == 'DN':
                        if core.USER_SCRIPT_RUNONCE == 1:
                            command.append('{0}'.format(output_destination))
                        else:
                            command.append('{0}'.format(dirpath))
                        continue
                    else:
                        command.append(param)
                        continue
                cmd = ''
                for item in command:
                    cmd = '{cmd} {item}'.format(cmd=cmd, item=item)
                logger.info('Running script {cmd} on file {path}.'.format(cmd=cmd, path=file_path), 'USERSCRIPT')
                try:
                    p = Popen(command)
                    res = p.wait()
                    if str(res) in core.USER_SCRIPT_SUCCESSCODES:  # Linux returns 0 for successful.
                        logger.info('UserScript {0} was successfull'.format(command[0]))
                        result = 0
                    else:
                        logger.error('UserScript {0} has failed with return code: {1}'.format(command[0], res), 'USERSCRIPT')
                        logger.info(
                            'If the UserScript completed successfully you should add {0} to the user_script_successCodes'.format(
                                res), 'USERSCRIPT')
                        result = int(1)
                except Exception:
                    logger.error('UserScript {0} has failed'.format(command[0]), 'USERSCRIPT')
                    result = int(1)
                final_result += result

    num_files_new = 0
    for _, _, filenames in os.walk(output_destination):
        for file in filenames:
            file_name, file_extension = os.path.splitext(file)

            if file_extension in core.USER_SCRIPT_MEDIAEXTENSIONS or core.USER_SCRIPT_MEDIAEXTENSIONS == 'ALL':
                num_files_new += 1

    if core.USER_SCRIPT_CLEAN == int(1) and num_files_new == 0 and final_result == 0:
        logger.info('All files have been processed. Cleaning outputDirectory {0}'.format(output_destination))
        remove_dir(output_destination)
    elif core.USER_SCRIPT_CLEAN == int(1) and num_files_new != 0:
        logger.info('{0} files were processed, but {1} still remain. outputDirectory will not be cleaned.'.format(
            num_files, num_files_new))
    return ProcessResult(
        status_code=final_result,
        message='User Script Completed',
    )