Ejemplo n.º 1
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
Ejemplo n.º 2
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=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=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.DOWNLOADINFO = get_download_info(os.path.basename(dir_name), 0)
                    if core.DOWNLOADINFO:
                        logger.info('Found download info for {0}, '
                                    'setting variables now ...'.format
                                    (os.path.basename(dir_name)))
                        client_agent = text_type(core.DOWNLOADINFO[0].get('client_agent', 'manual'))
                        download_id = text_type(core.DOWNLOADINFO[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

                    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(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