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.')
def inspect_rar_content(directory, filename): nzb.log_detail('Checking RAR content file %s.' % filename) if REJECT_DISC_IMAGES != 'Disabled': check_disc_image(filename) if REJECT_FAKES != 'Disabled': check_fake(filename) if REJECT_PASSWORD != 'Disabled': check_protected(directory, filename)
def on_post_processing(): directory = nzb.get_nzb_directory() category = nzb.get_nzb_category() target = get_category_path(category) if os.path.isdir(directory) and target: # We need to move the files, delete the directory, and hide the NZB # from history. file = get_largest_file(category, directory, target) if file: nzb.log_detail('Found largest file %s.' % file) source_path = file target_path = os.path.join(target, os.path.basename(file)) if os.path.isfile(target_path): nzb.log_warning('File %s already exists.' % target_path) else: nzb.log_detail('Copying %s to %s.' % (file, target_path)) shutil.copyfile(source_path, target_path) nzb.set_nzb_directory_final(target) shutil.rmtree(directory) nzb.log_detail('Deleted directory %s.' % directory) else: nzb.log_warning('Failed to find largest video file.') else: nzb.log_warning('Directory %s does not exist.' % directory)
def on_post_processing(): directory = nzb.get_nzb_directory() category = nzb.get_nzb_category() target = get_category_path(category) if os.path.isdir(directory) and target: # We need to move the files, delete the directory, and hide the NZB # from history. file = get_largest_file(category, directory, target) if file: nzb.log_detail("Found largest file %s." % file) source_path = file target_path = os.path.join(target, os.path.basename(file)) if os.path.isfile(target_path): nzb.log_warning("File %s already exists." % target_path) else: nzb.log_detail("Copying %s to %s." % (file, target_path)) shutil.copyfile(source_path, target_path) nzb.set_nzb_directory_final(target) shutil.rmtree(directory) nzb.log_detail("Deleted directory %s." % directory) else: nzb.log_warning("Failed to find largest video file.") else: nzb.log_warning("Directory %s does not exist." % directory)
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))
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))
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()
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()