Пример #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))
Пример #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))
Пример #3
0
def main():
    """
    We need to check to make sure the script can run in the provided
    environment and that certain status checks have occurred. All of the
    calls here will exit with an exit code if the check fails.
    """
    try:
        # If the script state was set to Disabled, we don't need to run.
        if SCRIPT_STATE == 'Disabled':
            nzb.exit(nzb.PROCESS_SUCCESS)

        # Check version of NZBGet to make sure we can run.
        nzb.check_nzb_version(13.0)

        # Wire up your event handlers before the call.
        # Use the form nzb.set_handler(<event>, <function>)
        nzb.set_handler('POST_PROCESSING', on_post_processing)
        nzb.set_handler('SCHEDULED', on_scheduled)

        # Do not change this line, it checks the current event
        # and executes any event handlers.
        nzb.execute()
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)
Пример #4
0
def main():
    """
    We need to check to make sure the script can run in the provided
    environment and that certain status checks have occurred. All of the
    calls here will exit with an exit code if the check fails.
    """
    try:
        # If the script state was set to Disabled, we don't need to run.
        if SCRIPT_STATE == "Disabled":
            nzb.exit(nzb.PROCESS_SUCCESS)

        # Check version of NZBGet to make sure we can run.
        nzb.check_nzb_version(13.0)

        # Wire up your event handlers before the call.
        # Use the form nzb.set_handler(<event>, <function>)
        nzb.set_handler("POST_PROCESSING", on_post_processing)
        nzb.set_handler("SCHEDULED", on_scheduled)

        # Do not change this line, it checks the current event
        # and executes any event handlers.
        nzb.execute()
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)
Пример #5
0
def check_limit_age(nzbid, nzbname):
    """
    Checks if the age of the NZB is older than our limit. We assume that
    anything older must be fairly complete and if the health check fails,
    it's because the files are gone.
    """
    nzbage = nzb.get_nzb_age(nzbid)
    nzbage_hours = int(nzbage / 60)
    if nzbage > AGE_LIMIT:
        clean_up()
        reason = "File %s is %s hours old, but limit was %s." % (nzbname, nzbage_hours, AGE_LIMIT)
        nzb.exit(nzb.PROCESS_SUCCESS, reason)
Пример #6
0
def check_limit_age(nzbid, nzbname):
    """
    Checks if the age of the NZB is older than our limit. We assume that
    anything older must be fairly complete and if the health check fails,
    it's because the files are gone.
    """
    nzbage = nzb.get_nzb_age(nzbid)
    nzbage_hours = int(nzbage / 60)
    if nzbage > AGE_LIMIT:
        clean_up()
        reason = 'File %s is %s hours old, but limit was %s.' % (
            nzbname, nzbage_hours, AGE_LIMIT)
        nzb.exit(nzb.PROCESS_SUCCESS, reason)
Пример #7
0
def check_limit_retries(nzbid, nzbname):
    """
    Checks to see how many retries have already been performed and exits
    if we are the limit.
    """
    # Update the state so we can determine how long we should wait.
    state = update_state(nzbid, nzbname)
    retries = int(state["retries"])
    wait_minutes = retries * RETRY_MINUTES

    # If we already reached the limit, we'll bail.
    if retries >= RETRY_LIMIT:
        clean_up()
        reason = "Number of retries has been reached (%s) for %s (%s)." % (retries, nzbid, nzbname)
        nzb.exit(nzb.PROCESS_SUCCESS)
Пример #8
0
def check_limit_retries(nzbid, nzbname):
    """
    Checks to see how many retries have already been performed and exits
    if we are the limit.
    """
    # Update the state so we can determine how long we should wait.
    state = update_state(nzbid, nzbname)
    retries = int(state['retries'])
    wait_minutes = retries * RETRY_MINUTES

    # If we already reached the limit, we'll bail.
    if retries >= RETRY_LIMIT:
        clean_up()
        reason = 'Number of retries has been reached (%s) for %s (%s).' % (
            retries, nzbid, nzbname)
        nzb.exit(nzb.PROCESS_SUCCESS)
Пример #9
0
def main():
    """
    We need to check to make sure the script can run in the provided
    environment and that certain status checks have occurred. All of the
    calls here will exit with an exit code if the check fails.
    """
    try:
        # If the script state was set to Disabled, we don't need to run.
        if SCRIPT_STATE == 'Disabled':
            nzb.exit(nzb.PROCESS_SUCCESS)

        # Check the version NZBGet we're running on.
        nzb.check_nzb_version(13.0)

        nzb.log_info('CURRENT EVENT: %s' % nzb.get_nzb_event())

        # Wire up your event handlers before the call.
        # User the form nzb.set_handler(<event>, <function>)
        nzb.set_handler('FILE_DOWNLOADED', on_file_downloaded)
        nzb.set_handler('NZB_ADDED', on_nzb_added)
        nzb.set_handler('NZB_DOWNLOADED', on_nzb_downloaded)
        nzb.set_handler('POST_PROCESSING', on_post_processing)
        nzb.set_handler('QUEUEING', on_queueing)
        nzb.set_handler('SCANNING', on_scanning)
        nzb.set_handler('SCHEDULED', on_scheduled)

        # Do not change this line, it checks the current event
        # and executes any event handlers.
        nzb.execute()
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)

    nzb.exit(nzb.PROCESS_SUCCESS)
Пример #10
0
def main():
    """
    We need to check to make sure the script can run in the provided
    environment and that certain status checks have occurred. All of the
    calls here will exit with an exit code if the check fails.
    """
    try:
        # If the script state was set to Disabled, we don't need to run.
        if SCRIPT_STATE == 'Disabled':
            nzb.exit(nzb.PROCESS_SUCCESS)

        # Check the version NZBGet we're running on.
        nzb.check_nzb_version(13.0)

        nzb.log_info('CURRENT EVENT: %s' % nzb.get_nzb_event())

        # Wire up your event handlers before the call.
        # User the form nzb.set_handler(<event>, <function>)
        nzb.set_handler('FILE_DOWNLOADED', on_file_downloaded)
        nzb.set_handler('NZB_ADDED', on_nzb_added)
        nzb.set_handler('NZB_DOWNLOADED', on_nzb_downloaded)
        nzb.set_handler('POST_PROCESSING', on_post_processing)
        nzb.set_handler('QUEUEING', on_queueing)
        nzb.set_handler('SCANNING', on_scanning)
        nzb.set_handler('SCHEDULED', on_scheduled)

        # Do not change this line, it checks the current event
        # and executes any event handlers.
        nzb.execute()
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)

    nzb.exit(nzb.PROCESS_SUCCESS)
Пример #11
0
def main():
    """
    We need to check to make sure the script can run in the provided
    environment and that certain status checks have occurred. All of the
    calls here will exit with an exit code if the check fails.
    """
    try:
        # If the script state was set to Disabled, we don't need to run.
        if SCRIPT_STATE == 'Disabled':
            nzb.exit(nzb.PROCESS_SUCCESS)

        # Determine if we have features enabled.
        DiscImageEnabled = REJECT_DISC_IMAGES != 'Disabled'
        FakeCheckEnabled = REJECT_FAKES != 'Disabled'
        PasswordCheckEnabled = REJECT_PASSWORD != 'Disabled'
        if not (DiscImageEnabled and FakeCheckEnabled and PasswordCheckEnabled):
            nzb.log_info('No features enabled. Skipping script execution.')
            nzb.exit(nzb.PROCESS_SUCCESS)

        # Check version of NZBGet to make sure we can run.
        nzb.check_nzb_version(13.0)

        # Wire up your event handlers before the call.
        # Use the form nzb.set_handler(<event>, <function>)
        nzb.set_handler('FILE_DOWNLOADED', on_file_downloaded)
        nzb.set_handler('NZB_ADDED', on_nzb_added)
        nzb.set_handler('NZB_DOWNLOADED', on_nzb_downloaded)

        # Do not change this line, it checks the current event
        # and executes any event handlers.
        nzb.execute()
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)
        clean_up()
Пример #12
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)
Пример #13
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)
Пример #14
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()
Пример #15
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()
Пример #16
0
def main():
    """
    We need to check to make sure the script can run in the provided
    environment and that certain status checks have occurred. All of the
    calls here will exit with an exit code if the check fails.
    """
    try:
        # If the script state was set to Disabled, we don't need to run.
        if SCRIPT_STATE == 'Disabled':
            nzb.exit(nzb.PROCESS_SUCCESS)

        # Determine if we have features enabled.
        DiscImageEnabled = REJECT_DISC_IMAGES != 'Disabled'
        FakeCheckEnabled = REJECT_FAKES != 'Disabled'
        PasswordCheckEnabled = REJECT_PASSWORD != 'Disabled'
        if not (DiscImageEnabled and FakeCheckEnabled
                and PasswordCheckEnabled):
            nzb.log_info('No features enabled. Skipping script execution.')
            nzb.exit(nzb.PROCESS_SUCCESS)

        # Check version of NZBGet to make sure we can run.
        nzb.check_nzb_version(13.0)

        # Wire up your event handlers before the call.
        # Use the form nzb.set_handler(<event>, <function>)
        nzb.set_handler('FILE_DOWNLOADED', on_file_downloaded)
        nzb.set_handler('NZB_ADDED', on_nzb_added)
        nzb.set_handler('NZB_DOWNLOADED', on_nzb_downloaded)

        # Do not change this line, it checks the current event
        # and executes any event handlers.
        nzb.execute()
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)
        clean_up()
Пример #17
0
        nzb.log_info('CURRENT EVENT: %s' % nzb.get_nzb_event())

        # Wire up your event handlers before the call.
        # User the form nzb.set_handler(<event>, <function>)
        nzb.set_handler('FILE_DOWNLOADED', on_file_downloaded)
        nzb.set_handler('NZB_ADDED', on_nzb_added)
        nzb.set_handler('NZB_DOWNLOADED', on_nzb_downloaded)
        nzb.set_handler('POST_PROCESSING', on_post_processing)
        nzb.set_handler('QUEUEING', on_queueing)
        nzb.set_handler('SCANNING', on_scanning)
        nzb.set_handler('SCHEDULED', on_scheduled)

        # Do not change this line, it checks the current event
        # and executes any event handlers.
        nzb.execute()
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)

    nzb.exit(nzb.PROCESS_SUCCESS)


# Main entry-point
##############################################################################
main()

# NZBGet is weird and doesn't use 0 to signal the successful execution of a
# script, so we use the PROCESS_SUCCESS code here.
nzb.exit(nzb.PROCESS_SUCCESS)
Пример #18
0
            nzb.log_info('Lock exists, skipping execution.')
            nzb.exit(nzb.PROCESS_SUCCESS)
        else:
            nzb.lock_create('FileMover')

        # Check version of NZBGet to make sure we can run.
        nzb.check_nzb_version(13.0)

        # Wire up your event handlers before the call.
        # User the form nzb.set_handler(<event>, <function>)
        nzb.set_handler('POST_PROCESSING', on_post_processing)
        nzb.set_handler('SCHEDULED', on_scheduled)

        # Do not change this line, it checks the current event
        # and executes any event handlers.
        nzb.execute()
    except Exception as e:
        traceback.print_exc()
        nzb.exit(nzb.PROCESS_ERROR, e)
    finally:
        clean_up()


# Main entry-point
##############################################################################
main()

# NZBGet is weird and doesn't use 0 to signal the successful execution of a
# script, so we use the PROCESS_SUCCESS code here.
nzb.exit(nzb.PROCESS_SUCCESS)