add external script for NZBs and add video check to userscript. Fixes #461

This commit is contained in:
clinton-hall 2014-07-04 23:41:51 +09:30
commit d1ba449b9a
4 changed files with 96 additions and 115 deletions

View file

@ -9,6 +9,7 @@ import nzbtomedia
from subprocess import Popen
from nzbtomedia import logger, nzbToMediaDB
from nzbtomedia.nzbToMediaUtil import convert_to_ascii, CharReplace
from nzbtomedia.nzbToMediaUserScript import external_script
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
status = 1 # 1 = failed | 0 = success
@ -83,35 +84,10 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
except:
extract = 0
if sectionName == "UserScript":
try:
nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS = section[usercat]["user_script_mediaExtensions"]
except:
nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS = None
try:
nzbtomedia.USER_SCRIPT = section[usercat]["user_script_path"]
except:
nzbtomedia.USER_SCRIPT = None
try:
nzbtomedia.USER_SCRIPT_PARAM = section[usercat]["user_script_param"]
except:
nzbtomedia.USER_SCRIPT_PARAM = None
try:
nzbtomedia.USER_SCRIPT_SUCCESSCODES = section[usercat]["user_script_successCodes"]
except:
nzbtomedia.USER_SCRIPT_SUCCESSCODES = 0
try:
nzbtomedia.USER_SCRIPT_CLEAN = int(section[usercat]["user_script_clean"])
except:
nzbtomedia.USER_SCRIPT_CLEAN = 1
try:
nzbtomedia.USER_SCRIPT_RUNONCE = int(section[usercat]["user_script_runOnce"])
except:
nzbtomedia.USER_SCRIPT_RUNONCE = 1
try:
uniquePath = int(section[usercat]["unique_path"])
except:
uniquePath = 1
try:
uniquePath = int(section[usercat]["unique_path"])
except:
uniquePath = 1
if clientAgent != 'manual':
nzbtomedia.pause_torrent(clientAgent, inputHash, inputID, inputName)
@ -213,9 +189,9 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
result = 0
if sectionName == 'UserScript':
result = external_script(outputDestination, inputName, inputCategory)
result = external_script(outputDestination, inputName, inputCategory, section[usercat])
if sectionName == 'CouchPotato':
elif sectionName == 'CouchPotato':
result = nzbtomedia.autoProcessMovie().process(sectionName,outputDestination, inputName, status, clientAgent, inputHash,
inputCategory)
elif sectionName in ['SickBeard','NzbDrone']:
@ -249,83 +225,6 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
return result
def external_script(outputDestination, torrentName, torrentLabel):
if nzbtomedia.USER_SCRIPT is None or nzbtomedia.USER_SCRIPT == "None": # do nothing and return success.
return 0
final_result = 0 # start at 0.
num_files = 0
for dirpath, dirnames, filenames in os.walk(outputDestination):
for file in filenames:
filePath = nzbtomedia.os.path.join(dirpath, file)
fileName, fileExtension = os.path.splitext(file)
if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or "ALL" in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS:
num_files = num_files + 1
if nzbtomedia.USER_SCRIPT_RUNONCE == 1 and num_files > 1: # we have already run once, so just continue to get number of files.
continue
command = [nzbtomedia.USER_SCRIPT]
for param in nzbtomedia.USER_SCRIPT_PARAM:
if param == "FN":
command.append(file)
continue
elif param == "FP":
command.append(filePath)
continue
elif param == "TN":
command.append(torrentName)
continue
elif param == "TL":
command.append(torrentLabel)
continue
elif param == "DN":
if nzbtomedia.USER_SCRIPT_RUNONCE == 1:
command.append(outputDestination)
else:
command.append(dirpath)
continue
else:
command.append(param)
continue
cmd = ""
for item in command:
cmd = cmd + " " + item
logger.info("Running script %s on file %s." % (cmd, filePath))
try:
p = Popen(command)
res = p.wait()
if str(res) in nzbtomedia.USER_SCRIPT_SUCCESSCODES: # Linux returns 0 for successful.
logger.info("UserScript %s was successfull" % (command[0]))
result = 0
else:
logger.error("UserScript %s has failed with return code: %s" % (command[0], res))
logger.info(
"If the UserScript completed successfully you should add %s to the user_script_successCodes" % (
res))
result = int(1)
except:
logger.error("UserScript %s has failed" % (command[0]))
result = int(1)
final_result = final_result + result
num_files_new = 0
for dirpath, dirnames, filenames in os.walk(outputDestination):
for file in filenames:
filePath = nzbtomedia.os.path.join(dirpath, file)
fileName, fileExtension = os.path.splitext(file)
if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS == "ALL":
num_files_new = num_files_new + 1
if nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new == 0 and final_result == 0:
logger.info("All files have been processed. Cleaning outputDirectory %s" % (outputDestination))
shutil.rmtree(outputDestination)
elif nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new != 0:
logger.info("%s files were processed, but %s still remain. outputDirectory will not be cleaned." % (
num_files, num_files_new))
return final_result
def main(args):
# Initialize the config
nzbtomedia.initialize()

View file

@ -304,6 +304,9 @@
enabled = 0
Torrent_NoLink = 0
extract = 1
video_corruption_check = 0
#Enable if you are sending commands to a remote server for this category
remote_path = 0
#What extension do you want to process? Specify all the extension, or use "ALL" to process all files.
user_script_mediaExtensions = .mkv,.avi,.divx,.xvid,.mov,.wmv,.mp4,.mpg,.mpeg
#Specify the path to your custom script. Use "None" if you wish to link this category, but NOT run any external script.

View file

@ -414,6 +414,60 @@
#wolhost=192.168.1.37
#wolport=80
## UserScript
# User Script category.
#
# category that gets called for post-processing with user script (accepts "UNCAT", "ALL", or a defined category).
#usCategory=mine
# Video Corruptio Check (0,1).
#
# Check video for corruption.
#usvideo_corruption_check=0
# User Script Remote Path (0,1).
#
# Script calls commands on another system.
#usremote_path=0
# User Script extensions.
#
# What extension do you want to process? Specify all the extension, or use "ALL" to process all files.
#user_script_mediaExtensions=.mkv,.avi,.divx,.xvid,.mov,.wmv,.mp4,.mpg,.mpeg
# User Script Path
#
# Specify the path to your custom script.
#user_script_path=/nzbToMedia/userscripts/script.sh
# User Script arguments.
#
# Specify the argument(s) passed to script, comma separated in order.
# for example FP,FN,DN, TN, TL for file path (absolute file name with path), file name, absolute directory name (with path), Torrent Name, Torrent Label/Category.
# So the result is /media/test/script/script.sh FP FN DN TN TL. Add other arguments as needed eg -f, -r
#user_script_param=FN
# User Script Run Once (0,1).
#
# Set user_script_runOnce = 0 to run for each file, or 1 to only run once (presumably on teh entire directory).
#user_script_runOnce=0
# User Script Success Codes.
#
# Specify the successcodes returned by the user script as a comma separated list. Linux default is 0
#user_script_successCodes=0
# User Script Clean After (0,1).
#
# Clean after? Note that delay function is used to prevent possible mistake :) Delay is intended as seconds
#user_script_clean=1
# User Script Delay.
#
# Delay in seconds after processing.
#usdelay=120
### NZBGET POST-PROCESSING SCRIPT ###
##############################################################################
import os
@ -426,6 +480,7 @@ from nzbtomedia.autoProcess.autoProcessMovie import autoProcessMovie
from nzbtomedia.autoProcess.autoProcessMusic import autoProcessMusic
from nzbtomedia.autoProcess.autoProcessTV import autoProcessTV
from nzbtomedia.nzbToMediaUtil import getDirs, extractFiles, cleanDir, update_downloadInfoStatus, get_downloadInfo, CharReplace, convert_to_ascii
from nzbtomedia.nzbToMediaUserScript import external_script
from nzbtomedia import logger, nzbToMediaDB
# post-processing
@ -455,12 +510,19 @@ def process(inputDirectory, inputName=None, status=0, clientAgent='manual', down
myDB.upsert("downloads", newValueDict, controlValueDict)
# auto-detect section
if inputCategory is None:
inputCategory = 'UNCAT'
usercat = inputCategory
section = nzbtomedia.CFG.findsection(inputCategory).isenabled()
if section is None:
logger.error(
'Category:[%s] 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.' % (
inputCategory))
return -1
section = nzbtomedia.CFG.findsection("ALL").isenabled()
if section is None:
logger.error(
'Category:[%s] 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.' % (
inputCategory))
return -1
else:
usercat = "ALL"
if len(section) > 1:
logger.error(
@ -477,18 +539,18 @@ def process(inputDirectory, inputName=None, status=0, clientAgent='manual', down
return -1
try:
extract = int(section[inputCategory]['extract'])
extract = int(section[usercat]['extract'])
except:
extract = 0
try:
if int(section[inputCategory]['remote_path']) and not nzbtomedia.REMOTEPATHS:
if int(section[usercat]['remote_path']) and not nzbtomedia.REMOTEPATHS:
logger.error('Remote Path is enabled for %s:%s but no Network mount points are defined. Please check your autoProcessMedia.cfg, exiting!' % (
sectionName, inputCategory))
return -1
except:
logger.error('Remote Path %s is not valid for %s:%s Please set this to either 0 to disable or 1 to enable!' % (
section[inputCategory]['remote_path'], sectionName, inputCategory))
section[usercat]['remote_path'], sectionName, inputCategory))
inputName, inputDirectory = convert_to_ascii(inputName, inputDirectory)
@ -511,6 +573,8 @@ def process(inputDirectory, inputName=None, status=0, clientAgent='manual', down
inputCategory)
elif sectionName == "Gamez":
result = autoProcessGames().process(sectionName, inputDirectory, inputName, status, clientAgent, inputCategory)
elif sectionName == 'UserScript':
result = external_script(inputDirectory, inputName, inputCategory, section[usercat])
else:
result = -1

View file

@ -395,6 +395,21 @@ class ConfigObj(configobj.ConfigObj, Section):
value = os.environ[key]
CFG_NEW[section][option] = value
section = "UserScript"
envCatKey = 'NZBPO_USCATEGORY'
envKeys = ['USVIDEO_CORRUPTION_CHECK', 'USREMOTE_PATH', 'USER_SCRIPT_MEDIAEXTENSIONS', 'USER_SCRIPT_PATH', 'USER_SCRIPT_PARAM', 'USER_SCRIPT_RUNONCE', 'USER_SCRIPT_SUCCESSCODES', 'USER_SCRIPT_CLEAN', 'USDELAY']
cfgKeys = ['video_corruption_check', 'remote_path', 'user_script_mediaExtensions', 'user_script_path', 'user_script_param', 'user_script_runOnce', 'user_script_successCodes', 'user_script_clean', 'delay']
if os.environ.has_key(envCatKey):
for index in range(len(envKeys)):
key = 'NZBPO_' + envKeys[index]
if os.environ.has_key(key):
option = cfgKeys[index]
value = os.environ[key]
if os.environ[envCatKey] not in CFG_NEW[section].sections:
CFG_NEW[section][os.environ[envCatKey]] = {}
CFG_NEW[section][os.environ[envCatKey]][option] = value
CFG_NEW[section][os.environ[envCatKey]]['enabled'] = 1
except Exception, e:
logger.debug("Error %s when applying NZBGet config" % (e))