Esempio n. 1
0
def on_scheduled():
    # Bail out if a lock exists, because post-processing is running.
    if nzb.lock_exists(SCRIPT_NAME):
        nzb.exit(nzb.PROCESS_SUCCESS)

    groups = nzb.proxy().listgroups(0)

    for group in groups:
        nzbid = int(group['NZBID'])
        update_filepath = get_update_filepath(nzbid)

        # Look at the next group if we couldn't find it here.
        if not os.path.isfile(update_filepath):
            continue

        nzb.log_detail('Found state file at %s.' % update_filepath)
        timestamp = int(time.mktime(datetime.datetime.utcnow().timetuple()))
        state = json.load(open(update_filepath, 'r'))
        state_nzbname = state['nzbname']
        state_lastcheck = int(state['lastcheck'])
        state_retries = int(state['retries'])
        wait_minutes = state_retries * RETRY_MINUTES
        elapsed_minutes = (timestamp - state_lastcheck) / 60 / 60

        # If the wait time has elapsed, we need to unpause the file.
        if elapsed_minutes >= wait_minutes:
            nzb.log_detail('Resuming download for %s (%s).' %
                           (state_nzbname, nzbid))
            if not nzb.proxy().editqueue('GroupResume', 0, '', [nzbid]):
                reason = 'Failed to resume %s (%s).' % (state_nzbname, nzbid)
                nzb.exit(nzb.PROCESS_FAIL_PROXY, reason)
        else:
            nzb.log_detail('Waiting for %s minutes, %s minutes elapsed.' %
                           (wait_minutes, elapsed_minutes))
Esempio n. 2
0
def on_scheduled():
    # Bail out if a lock exists, because post-processing is running.
    if nzb.lock_exists(SCRIPT_NAME):
        nzb.exit(nzb.PROCESS_SUCCESS)

    groups = nzb.proxy().listgroups(0)

    for group in groups:
        nzbid = int(group["NZBID"])
        update_filepath = get_update_filepath(nzbid)

        # Look at the next group if we couldn't find it here.
        if not os.path.isfile(update_filepath):
            continue

        nzb.log_detail("Found state file at %s." % update_filepath)
        timestamp = int(time.mktime(datetime.datetime.utcnow().timetuple()))
        state = json.load(open(update_filepath, "r"))
        state_nzbname = state["nzbname"]
        state_lastcheck = int(state["lastcheck"])
        state_retries = int(state["retries"])
        wait_minutes = state_retries * RETRY_MINUTES
        elapsed_minutes = (timestamp - state_lastcheck) / 60 / 60

        # If the wait time has elapsed, we need to unpause the file.
        if elapsed_minutes >= wait_minutes:
            nzb.log_detail("Resuming download for %s (%s)." % (state_nzbname, nzbid))
            if not nzb.proxy().editqueue("GroupResume", 0, "", [nzbid]):
                reason = "Failed to resume %s (%s)." % (state_nzbname, nzbid)
                nzb.exit(nzb.PROCESS_FAIL_PROXY, reason)
        else:
            nzb.log_detail("Waiting for %s minutes, %s minutes elapsed." % (wait_minutes, elapsed_minutes))
Esempio n. 3
0
def reorder_queued_items(nzbid):
    """
    Finds the last part of the RAR archive and moves to the top of the queue.
    """
    # If another script already sorted, then we can skip sorting.
    if bool(nzb.get_script_variable('RAR_SORTED')):
        nzb.log_info('Last RAR file was already sorted.')
        return

    # Get the list of files for this NZB.
    proxy = nzb.proxy()
    filelist = proxy.listfiles(0, 0, nzbid)

    # Enumerate the RAR files from the NZB and try to parse the part number.
    files = nzb.get_rar_xmlfiles(filelist)

    # If we found RAR files, we need to sort so that the last RAR file is the first
    # item in the list.
    if files:
        files_sorted = sorted(files,
                              key=operator.itemgetter('number'),
                              reverse=True)
        filename = files_sorted[0]['filename']
        fileid = int(files_sorted[0]['fileid'])

        if proxy.editqueue('FileMoveTop', 0, '', [fileid]):
            nzb.log_detail('Moved last RAR file %s to the top.' % filename)
            nzb.set_script_variable('RAR_SORTED', True)
        else:
            nzb.log_warning('Failed to move the last RAR file %s.' % filename)
    else:
        nzb.log_warning('Failed to get list of files to sort.')
Esempio n. 4
0
def reorder_queued_items(nzbid):
    """
    Finds the last part of the RAR archive and moves to the top of the queue.
    """
    # If another script already sorted, then we can skip sorting.
    if bool(nzb.get_script_variable('RAR_SORTED')):
        nzb.log_info('Last RAR file was already sorted.')
        return

    # Get the list of files for this NZB.
    proxy = nzb.proxy()
    filelist = proxy.listfiles(0, 0, nzbid)

    # Enumerate the RAR files from the NZB and try to parse the part number.
    files = nzb.get_rar_xmlfiles(filelist)

    # If we found RAR files, we need to sort so that the last RAR file is the first
    # item in the list.
    if files:
        files_sorted = sorted(files, key=operator.itemgetter('number'), reverse=True)
        filename = files_sorted[0]['filename']
        fileid = int(files_sorted[0]['fileid'])

        if proxy.editqueue('FileMoveTop', 0, '', [fileid]):
            nzb.log_detail('Moved last RAR file %s to the top.' % filename)
            nzb.set_script_variable('RAR_SORTED', True)
        else:
            nzb.log_warning('Failed to move the last RAR file %s.' % filename)
    else:
        nzb.log_warning('Failed to get list of files to sort.')
Esempio n. 5
0
def on_scheduled():
    categories = get_categories()
    proxy = nzb.proxy()
    histories = proxy.history()

    nzb.log_info('Processing histories...')

    for history in histories:
        category = history['Category']
        finaldir = history['FinalDir']
        status = history['Status']
        nzbid = int(history['NZBID'])
        if finaldir and category in categories and status == 'SUCCESS/ALL':
            if not proxy.editqueue('HistoryDelete', 0, '', [nzbid]):
                nzb.log_warning('Failed to mark %s as hidden.' % nzbid)

    nzb.log_info('Completed processing histories.')
Esempio n. 6
0
def on_scheduled():
    categories = get_categories()
    proxy = nzb.proxy()
    histories = proxy.history()

    nzb.log_info("Processing histories...")

    for history in histories:
        category = history["Category"]
        finaldir = history["FinalDir"]
        status = history["Status"]
        nzbid = int(history["NZBID"])
        if finaldir and category in categories and status == "SUCCESS/ALL":
            if not proxy.editqueue("HistoryDelete", 0, "", [nzbid]):
                nzb.log_warning("Failed to mark %s as hidden." % nzbid)

    nzb.log_info("Completed processing histories.")
Esempio n. 7
0
def on_post_processing():
    # Create a lock so that the scheduler also doesn't try to run.
    nzb.lock_reset(SCRIPT_NAME)

    status = nzb.get_nzb_status()

    if status != 'FAILURE/HEALTH':
        nzb.log_detail('Nothing to do, status was %s.' % status)
        nzb.exit(nzb.PROCESS_SUCCESS)

    try:
        nzbid = nzb.get_nzb_id()
        nzbname = nzb.get_nzb_name()

        nzb.log_detail('Performing health check on %s (%s).' %
                       (nzbname, status))

        check_limit_age(nzbid, nzbname)
        check_limit_retries(nzbid, nzbname)

        # Stop all other post-processing because we need to requeue the file.
        nzb.log_warning('Pausing %s due to status of %s.' % (nzbname, status))
        proxy = nzb.proxy()

        # Pause the file group.
        if not proxy.editqueue('GroupPause', 0, '', [nzbid]):
            reason = 'Failed to pause %s (%s).' % (nzbname, nzbid)
            nzb.exit(nzb.PROCESS_FAIL_PROXY, reason)

        # Send the file back to the queue.
        if not proxy.editqueue('HistoryReturn', 0, '', [nzbid]):
            reason = 'Failed to requeue %s (%s).' % (nzbname, nzbid)
            nzb.exit(nzb.PROCESS_FAIL_PROXY, reason)
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)
    finally:
        nzb.lock_release(SCRIPT_NAME)
        clean_up()
Esempio n. 8
0
def on_post_processing():
    # Create a lock so that the scheduler also doesn't try to run.
    nzb.lock_reset(SCRIPT_NAME)

    status = nzb.get_nzb_status()

    if status != "FAILURE/HEALTH":
        nzb.log_detail("Nothing to do, status was %s." % status)
        nzb.exit(nzb.PROCESS_SUCCESS)

    try:
        nzbid = nzb.get_nzb_id()
        nzbname = nzb.get_nzb_name()

        nzb.log_detail("Performing health check on %s (%s)." % (nzbname, status))

        check_limit_age(nzbid, nzbname)
        check_limit_retries(nzbid, nzbname)

        # Stop all other post-processing because we need to requeue the file.
        nzb.log_warning("Pausing %s due to status of %s." % (nzbname, status))
        proxy = nzb.proxy()

        # Pause the file group.
        if not proxy.editqueue("GroupPause", 0, "", [nzbid]):
            reason = "Failed to pause %s (%s)." % (nzbname, nzbid)
            nzb.exit(nzb.PROCESS_FAIL_PROXY, reason)

        # Send the file back to the queue.
        if not proxy.editqueue("HistoryReturn", 0, "", [nzbid]):
            reason = "Failed to requeue %s (%s)." % (nzbname, nzbid)
            nzb.exit(nzb.PROCESS_FAIL_PROXY, reason)
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)
    finally:
        nzb.lock_release(SCRIPT_NAME)
        clean_up()
Esempio n. 9
0
def reject(reason):
    nzbid = nzb.get_nzb_id()
    nzbname = nzb.get_nzb_name()
    nzb.log_error('Rejecting %s. %s.' % (nzbname, reason))

    response = None

    if REJECT_ACTION == 'Pause':
        nzb.log_error('File %s was rejected, pausing download.' % nzbname)
        response = nzb.proxy().editqueue('GroupPause', 0, '', [nzbid])
    elif REJECT_ACTION == 'Bad':
        nzb.log_error('File %s was rejected, marking as bad.' % nzbname)
        nzb.set_nzb_bad()
        response = True
    elif REJECT_ACTION == 'Fail':
        nzb.log_error('File %s was rejected, marking as failed.' % nzbname)
        response = nzb.set_nzb_fail(nzbid)

    if not response:
        nzb.log_error('Failed to apply the reject action.')
        nzb.exit(nzb.PROCESS_ERROR)

    nzb.exit(nzb.PROCESS_ERROR)
Esempio n. 10
0
def reject(reason):
    nzbid = nzb.get_nzb_id()
    nzbname = nzb.get_nzb_name()
    nzb.log_error('Rejecting %s. %s.' % (nzbname, reason))

    response = None

    if REJECT_ACTION == 'Pause':
        nzb.log_error('File %s was rejected, pausing download.' % nzbname)
        response = nzb.proxy().editqueue('GroupPause', 0, '', [nzbid])
    elif REJECT_ACTION == 'Bad':
        nzb.log_error('File %s was rejected, marking as bad.' % nzbname)
        nzb.set_nzb_bad()
        response = True
    elif REJECT_ACTION == 'Fail':
        nzb.log_error('File %s was rejected, marking as failed.' % nzbname)
        response = nzb.set_nzb_fail(nzbid)

    if not response:
        nzb.log_error('Failed to apply the reject action.')
        nzb.exit(nzb.PROCESS_ERROR)

    nzb.exit(nzb.PROCESS_ERROR)