Resolves userscript issues.

Converted userscript categories to new subsection format.
This commit is contained in:
echel0n 2014-04-25 12:19:07 -07:00
commit 0d725a78d6
6 changed files with 106 additions and 91 deletions

View file

@ -9,12 +9,23 @@ import nzbtomedia
from subprocess import Popen from subprocess import Popen
from nzbtomedia import logger, nzbToMediaDB from nzbtomedia import logger, nzbToMediaDB
USER_SCRIPT_MEDIAEXTENSIONS = None
USER_SCRIPT = None
USER_SCRIPT_PARAM = None
USER_SCRIPT_SUCCESSCODES = None
USER_SCRIPT_CLEAN = None
USER_DELAY = None
USER_SCRIPT_RUNONCE = None
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent): def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
status = 1 # 1 = failed | 0 = success status = 1 # 1 = failed | 0 = success
root = 0 root = 0
foundFile = 0 foundFile = 0
global USER_DELAY, USER_SCRIPT, USER_SCRIPT_CLEAN, USER_SCRIPT_MEDIAEXTENSIONS, \
USER_SCRIPT_PARAM, USER_SCRIPT_RUNONCE, USER_SCRIPT_SUCCESSCODES
if clientAgent != 'manual': if clientAgent != 'manual':
logger.debug('Adding TORRENT download info for directory %s to database' % (inputDirectory)) logger.debug('Adding TORRENT download info for directory %s to database' % (inputDirectory))
@ -64,6 +75,15 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
except: except:
extract = 0 extract = 0
if not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES:
USER_SCRIPT_MEDIAEXTENSIONS = (section[inputCategory]["user_script_mediaExtensions"])
USER_SCRIPT = section[inputCategory]["user_script_path"]
USER_SCRIPT_PARAM = (section[inputCategory]["user_script_param"])
USER_SCRIPT_SUCCESSCODES = (section[inputCategory]["user_script_successCodes"])
USER_SCRIPT_CLEAN = int(section[inputCategory]["user_script_clean"])
USER_DELAY = int(section[inputCategory]["delay"])
USER_SCRIPT_RUNONCE = int(section[inputCategory]["user_script_runOnce"])
if clientAgent != 'manual': if clientAgent != 'manual':
nzbtomedia.pause_torrent(clientAgent, inputHash, inputID, inputName) nzbtomedia.pause_torrent(clientAgent, inputHash, inputID, inputName)
@ -163,7 +183,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
result = 0 result = 0
if (inputCategory in nzbtomedia.USER_SCRIPT_CATEGORIES and not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES) or ( if (inputCategory in nzbtomedia.USER_SCRIPT_CATEGORIES and not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES) or (
"ALL" in nzbtomedia.USER_SCRIPT_CATEGORIES and not inputCategory in processCategories): "ALL" in nzbtomedia.USER_SCRIPT_CATEGORIES and not inputCategory in processCategories):
logger.info("Processing user script %s." % (nzbtomedia.USER_SCRIPT)) logger.info("Processing user script %s." % (USER_SCRIPT))
result = external_script(outputDestination, inputName, inputCategory) result = external_script(outputDestination, inputName, inputCategory)
elif status != 0: elif status != 0:
logger.error("Something failed! Please check logs. Exiting") logger.error("Something failed! Please check logs. Exiting")
@ -213,12 +233,12 @@ def external_script(outputDestination, torrentName, torrentLabel):
filePath = nzbtomedia.os.path.join(dirpath, file) filePath = nzbtomedia.os.path.join(dirpath, file)
fileName, fileExtension = os.path.splitext(file) fileName, fileExtension = os.path.splitext(file)
if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or "ALL" in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS: if fileExtension in USER_SCRIPT_MEDIAEXTENSIONS or "ALL" in USER_SCRIPT_MEDIAEXTENSIONS:
num_files = num_files + 1 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. if USER_SCRIPT_RUNONCE == 1 and num_files > 1: # we have already run once, so just continue to get number of files.
continue continue
command = [nzbtomedia.USER_SCRIPT] command = [USER_SCRIPT]
for param in nzbtomedia.USER_SCRIPT_PARAM: for param in USER_SCRIPT_PARAM:
if param == "FN": if param == "FN":
command.append(file) command.append(file)
continue continue
@ -232,7 +252,7 @@ def external_script(outputDestination, torrentName, torrentLabel):
command.append(torrentLabel) command.append(torrentLabel)
continue continue
elif param == "DN": elif param == "DN":
if nzbtomedia.USER_SCRIPT_RUNONCE == 1: if USER_SCRIPT_RUNONCE == 1:
command.append(outputDestination) command.append(outputDestination)
else: else:
command.append(dirpath) command.append(dirpath)
@ -247,7 +267,7 @@ def external_script(outputDestination, torrentName, torrentLabel):
try: try:
p = Popen(command) p = Popen(command)
res = p.wait() res = p.wait()
if str(res) in nzbtomedia.USER_SCRIPT_SUCCESSCODES: # Linux returns 0 for successful. if str(res) in USER_SCRIPT_SUCCESSCODES: # Linux returns 0 for successful.
logger.info("UserScript %s was successfull" % (command[0])) logger.info("UserScript %s was successfull" % (command[0]))
result = 0 result = 0
else: else:
@ -261,20 +281,20 @@ def external_script(outputDestination, torrentName, torrentLabel):
result = int(1) result = int(1)
final_result = final_result + result final_result = final_result + result
time.sleep(nzbtomedia.USER_DELAY) time.sleep(USER_DELAY)
num_files_new = 0 num_files_new = 0
for dirpath, dirnames, filenames in os.walk(outputDestination): for dirpath, dirnames, filenames in os.walk(outputDestination):
for file in filenames: for file in filenames:
filePath = nzbtomedia.os.path.join(dirpath, file) filePath = nzbtomedia.os.path.join(dirpath, file)
fileName, fileExtension = os.path.splitext(file) fileName, fileExtension = os.path.splitext(file)
if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS == "ALL": if fileExtension in USER_SCRIPT_MEDIAEXTENSIONS or USER_SCRIPT_MEDIAEXTENSIONS == "ALL":
num_files_new = num_files_new + 1 num_files_new = num_files_new + 1
if nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new == 0 and final_result == 0: if 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)) logger.info("All files have been processed. Cleaning outputDirectory %s" % (outputDestination))
shutil.rmtree(outputDestination) shutil.rmtree(outputDestination)
elif nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new != 0: elif 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." % ( logger.info("%s files were processed, but %s still remain. outputDirectory will not be cleaned." % (
num_files, num_files_new)) num_files, num_files_new))
return final_result return final_result

View file

@ -240,25 +240,26 @@
mac = 00:01:2e:2D:64:e1 mac = 00:01:2e:2D:64:e1
[UserScript] [UserScript]
#Use user_script for uncategorized download? #Use user_script for uncategorized downloads
#Set the categories to use external script, comma separated. #Set the categories to use external script.
#Use "UNCAT" to process non-category downloads, and "ALL" for all. Set to "NONE" to disable external script. #Use "UNCAT" to process non-category downloads, and "ALL" for all. Set to "NONE" to disable external script.
user_script_categories = NONE [[UNCAT]]
#What extension do you want to process? Specify all the extension, or use "ALL" to process all files. enabled = 0
user_script_mediaExtensions = .mkv,.avi,.divx,.xvid,.mov,.wmv,.mp4,.mpg,.mpeg #What extension do you want to process? Specify all the extension, or use "ALL" to process all files.
#Specify the path to your custom script user_script_mediaExtensions = .mkv,.avi,.divx,.xvid,.mov,.wmv,.mp4,.mpg,.mpeg
user_script_path = /nzbToMedia/userscripts/script.sh #Specify the path to your custom script
#Specify the argument(s) passed to script, comma separated in order. user_script_path = /nzbToMedia/userscripts/script.sh
#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. #Specify the argument(s) passed to script, comma separated in order.
#So the result is /media/test/script/script.sh FP FN DN TN TL. Add other arguments as needed eg -f, -r #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.
user_script_param = FN #So the result is /media/test/script/script.sh FP FN DN TN TL. Add other arguments as needed eg -f, -r
#Set user_script_runOnce = 0 to run for each file, or 1 to only run once (presumably on teh entire directory). user_script_param = FN
user_script_runOnce = 0 #Set user_script_runOnce = 0 to run for each file, or 1 to only run once (presumably on teh entire directory).
#Specify the successcodes returned by the user script as a comma separated list. Linux default is 0 user_script_runOnce = 0
user_script_successCodes = 0 #Specify the successcodes returned by the user script as a comma separated list. Linux default is 0
#Clean after? Note that delay function is used to prevent possible mistake :) Delay is intended as seconds user_script_successCodes = 0
user_script_clean = 1 #Clean after? Note that delay function is used to prevent possible mistake :) Delay is intended as seconds
delay = 120 user_script_clean = 1
delay = 120
[ASCII] [ASCII]
#Set convert =1 if you want to convert any "foreign" characters to ASCII before passing to SB/CP etc. Default is disabled (0). #Set convert =1 if you want to convert any "foreign" characters to ASCII before passing to SB/CP etc. Default is disabled (0).

View file

@ -85,7 +85,6 @@ TORRENT_CLIENTAGENT = None
TORRENT_CLASS = None TORRENT_CLASS = None
USELINK = None USELINK = None
OUTPUTDIRECTORY = None OUTPUTDIRECTORY = None
CATEGORIES = []
NOFLATTEN = [] NOFLATTEN = []
DELETE_ORIGINAL = None DELETE_ORIGINAL = None
@ -103,15 +102,15 @@ DELUGEPORT = None
DELUGEUSR = None DELUGEUSR = None
DELUGEPWD = None DELUGEPWD = None
EXTCONTAINER = None EXTCONTAINER = []
COMPRESSEDCONTAINER = None COMPRESSEDCONTAINER = []
MEDIACONTAINER = None MEDIACONTAINER = []
AUDIOCONTAINER = None AUDIOCONTAINER = []
METACONTAINER = None METACONTAINER = []
MINSAMPLESIZE = None
SAMPLEIDS = None
SECTIONS = [] SECTIONS = []
CATEGORIES = []
USER_SCRIPT_CATEGORIES = []
TRANSCODE = None TRANSCODE = None
FFMPEG_PATH = None FFMPEG_PATH = None
@ -131,15 +130,6 @@ FFMPEG = None
FFPROBE = None FFPROBE = None
NICENESS = None NICENESS = None
USER_SCRIPT_CATEGORIES = None
USER_SCRIPT_MEDIAEXTENSIONS = None
USER_SCRIPT = None
USER_SCRIPT_PARAM = None
USER_SCRIPT_SUCCESSCODES = None
USER_SCRIPT_CLEAN = None
USER_DELAY = None
USER_SCRIPT_RUNONCE = None
PASSWORDSFILE = None PASSWORDSFILE = None
__INITIALIZED__ = False __INITIALIZED__ = False
@ -150,14 +140,13 @@ def initialize(section=None):
NZBTOMEDIA_BRANCH, NZBTOMEDIA_VERSION, NEWEST_VERSION, NEWEST_VERSION_STRING, VERSION_NOTIFY, SYS_ARGV, CFG, \ NZBTOMEDIA_BRANCH, NZBTOMEDIA_VERSION, NEWEST_VERSION, NEWEST_VERSION_STRING, VERSION_NOTIFY, SYS_ARGV, CFG, \
SABNZB_NO_OF_ARGUMENTS, SABNZB_0717_NO_OF_ARGUMENTS, CATEGORIES, TORRENT_CLIENTAGENT, USELINK, OUTPUTDIRECTORY, NOFLATTEN, \ SABNZB_NO_OF_ARGUMENTS, SABNZB_0717_NO_OF_ARGUMENTS, CATEGORIES, TORRENT_CLIENTAGENT, USELINK, OUTPUTDIRECTORY, NOFLATTEN, \
UTORRENTPWD, UTORRENTUSR, UTORRENTWEBUI, DELUGEHOST, DELUGEPORT, DELUGEUSR, DELUGEPWD, TRANSMISSIONHOST, TRANSMISSIONPORT, \ UTORRENTPWD, UTORRENTUSR, UTORRENTWEBUI, DELUGEHOST, DELUGEPORT, DELUGEUSR, DELUGEPWD, TRANSMISSIONHOST, TRANSMISSIONPORT, \
TRANSMISSIONPWD, TRANSMISSIONUSR, COMPRESSEDCONTAINER, MEDIACONTAINER, METACONTAINER, MINSAMPLESIZE, SAMPLEIDS, \ TRANSMISSIONPWD, TRANSMISSIONUSR, COMPRESSEDCONTAINER, MEDIACONTAINER, METACONTAINER, SECTIONS, USER_SCRIPT_CATEGORIES, \
SECTIONS, USER_SCRIPT_CATEGORIES, __INITIALIZED__, AUTO_UPDATE, APP_FILENAME, USER_DELAY, USER_SCRIPT_RUNONCE, \ __INITIALIZED__, AUTO_UPDATE, APP_FILENAME, USER_DELAY, APP_NAME, TRANSCODE, GIT_PATH, GIT_USER, \
APP_NAME, USER_SCRIPT_MEDIAEXTENSIONS, USER_SCRIPT, USER_SCRIPT_PARAM, USER_SCRIPT_SUCCESSCODES, USER_SCRIPT_CLEAN, \ GIT_BRANCH, GIT_REPO, SYS_ENCODING, NZB_CLIENTAGENT, SABNZBDHOST, SABNZBDPORT, SABNZBDAPIKEY, \
TRANSCODE, GIT_PATH, GIT_USER, GIT_BRANCH, GIT_REPO, SYS_ENCODING, NZB_CLIENTAGENT, SABNZBDHOST, SABNZBDPORT, SABNZBDAPIKEY, \
DUPLICATE, IGNOREEXTENSIONS, OUTPUTVIDEOEXTENSION, OUTPUTVIDEOCODEC, OUTPUTVIDEOPRESET, OUTPUTVIDEOFRAMERATE, LOG_DB, \ DUPLICATE, IGNOREEXTENSIONS, OUTPUTVIDEOEXTENSION, OUTPUTVIDEOCODEC, OUTPUTVIDEOPRESET, OUTPUTVIDEOFRAMERATE, LOG_DB, \
OUTPUTVIDEOBITRATE, OUTPUTAUDIOCODEC, OUTPUTAUDIOBITRATE, OUTPUTSUBTITLECODEC, OUTPUTFASTSTART, OUTPUTQUALITYPERCENT, \ OUTPUTVIDEOBITRATE, OUTPUTAUDIOCODEC, OUTPUTAUDIOBITRATE, OUTPUTSUBTITLECODEC, OUTPUTFASTSTART, OUTPUTQUALITYPERCENT, \
NICENESS, LOG_DEBUG, FORCE_CLEAN, FFMPEG_PATH, FFMPEG, FFPROBE, AUDIOCONTAINER, EXTCONTAINER, TORRENT_CLASS, DELETE_ORIGINAL,\ NICENESS, LOG_DEBUG, FORCE_CLEAN, FFMPEG_PATH, FFMPEG, FFPROBE, AUDIOCONTAINER, EXTCONTAINER, TORRENT_CLASS, \
PASSWORDSFILE DELETE_ORIGINAL, PASSWORDSFILE
if __INITIALIZED__: if __INITIALIZED__:
return False return False
@ -323,18 +312,11 @@ def initialize(section=None):
logger.warning("Failed to locate ffprobe, video corruption detection disabled!") logger.warning("Failed to locate ffprobe, video corruption detection disabled!")
logger.warning("Install ffmpeg with x264 support to enable this feature ...") logger.warning("Install ffmpeg with x264 support to enable this feature ...")
USER_SCRIPT_CATEGORIES = CFG["UserScript"]["user_script_categories"] # userscript
if not "NONE" in USER_SCRIPT_CATEGORIES: map(USER_SCRIPT_CATEGORIES.append, ([subsections[0] for subsections in CFG['UserScript'].items()]))
USER_SCRIPT_MEDIAEXTENSIONS = (CFG["UserScript"]["user_script_mediaExtensions"])
USER_SCRIPT = CFG["UserScript"]["user_script_path"]
USER_SCRIPT_PARAM = (CFG["UserScript"]["user_script_param"])
USER_SCRIPT_SUCCESSCODES = (CFG["UserScript"]["user_script_successCodes"])
USER_SCRIPT_CLEAN = int(CFG["UserScript"]["user_script_clean"])
USER_DELAY = int(CFG["UserScript"]["delay"])
USER_SCRIPT_RUNONCE = int(CFG["UserScript"]["user_script_runOnce"])
# check for script-defied section and if None set to allow sections # check for script-defied section and if None set to allow sections
SECTIONS = CFG[tuple(x for x in CFG if CFG[x].sections) if not section else (section,)] SECTIONS = CFG[tuple(x for x in CFG if CFG[x].sections and CFG[x].isenabled()) if not section else (section,)]
map(CATEGORIES.extend,([subsection.sections for section,subsection in SECTIONS.items()])) map(CATEGORIES.extend,([subsection.sections for section,subsection in SECTIONS.items()]))
CATEGORIES = list(set(CATEGORIES)) CATEGORIES = list(set(CATEGORIES))

View file

@ -10,18 +10,21 @@ class Section(configobj.Section):
def isenabled(section): def isenabled(section):
# checks if subsection enabled, returns true/false if subsection specified otherwise returns true/false in {} # checks if subsection enabled, returns true/false if subsection specified otherwise returns true/false in {}
if not section.sections: if not section.sections:
if int(section['enabled']) == 1: try:
value = list(ConfigObj.find_key(section, 'enabled'))[0]
except:value = 0
if int(value) == 1:
return section return section
else: else:
to_return = copy.deepcopy(section) to_return = copy.deepcopy(section)
for section_name, subsections in to_return.items(): for section_name, subsections in to_return.items():
if subsections.sections: for subsection in subsections:
for subsection in subsections: try:
if not int(subsections[subsection]['enabled']) == 1: value = list(ConfigObj.find_key(subsections, 'enabled'))[0]
del subsections[subsection] except:value = 0
else:
if not int(subsections['enabled']) == 1: if int(value) != 1:
del to_return[section_name] del to_return[section_name][subsection]
# cleanout empty sections and subsections # cleanout empty sections and subsections
for key in [k for (k, v) in to_return.items() if not v]: for key in [k for (k, v) in to_return.items() if not v]:
@ -32,7 +35,11 @@ class Section(configobj.Section):
def findsection(section, key): def findsection(section, key):
to_return = copy.deepcopy(section) to_return = copy.deepcopy(section)
for subsection in to_return: for subsection in to_return:
if key not in to_return[subsection]: try:
value = list(ConfigObj.find_key(to_return[subsection], key))[0]
except:value = None
if not value:
del to_return[subsection] del to_return[subsection]
# cleanout empty sections and subsections # cleanout empty sections and subsections
@ -74,6 +81,19 @@ class ConfigObj(configobj.ConfigObj, Section):
super(configobj.ConfigObj, self).__init__(*args, **kw) super(configobj.ConfigObj, self).__init__(*args, **kw)
self.interpolation = False self.interpolation = False
@staticmethod
def find_key(node, kv):
if isinstance(node, list):
for i in node:
for x in ConfigObj.find_key(i, kv):
yield x
elif isinstance(node, dict):
if kv in node:
yield node[kv]
for j in node.values():
for x in ConfigObj.find_key(j, kv):
yield x
@staticmethod @staticmethod
def migrate(): def migrate():
global CFG_NEW, CFG_OLD global CFG_NEW, CFG_OLD
@ -152,19 +172,7 @@ class ConfigObj(configobj.ConfigObj, Section):
values.pop(option) values.pop(option)
# remove any options that we no longer need so they don't migrate into our new config # remove any options that we no longer need so they don't migrate into our new config
def find_key(node, kv): if not list(ConfigObj.find_key(CFG_NEW, option)):
if isinstance(node, list):
for i in node:
for x in find_key(i, kv):
yield x
elif isinstance(node, dict):
if kv in node:
yield node[kv]
for j in node.values():
for x in find_key(j, kv):
yield x
if not list(find_key(CFG_NEW, option)):
values.pop(option) values.pop(option)
return values return values

View file

@ -436,13 +436,17 @@ def getDirs(section, subsection):
os.path.isdir(os.path.join(path, o))]) os.path.isdir(os.path.join(path, o))])
return folders return folders
watch_dir = nzbtomedia.CFG[section][subsection]["watch_dir"] try:
if os.path.exists(watch_dir): watch_dir = nzbtomedia.CFG[section][subsection]["watch_dir"]
to_return.extend(processDir(watch_dir)) if os.path.exists(watch_dir):
to_return.extend(processDir(watch_dir))
except:pass
outputDirectory = os.path.join(nzbtomedia.OUTPUTDIRECTORY, subsection) try:
if os.path.exists(outputDirectory): outputDirectory = os.path.join(nzbtomedia.OUTPUTDIRECTORY, subsection)
to_return.extend(processDir(outputDirectory)) if os.path.exists(outputDirectory):
to_return.extend(processDir(outputDirectory))
except:pass
if not to_return: if not to_return:
logger.debug("No directories identified in %s:%s for post-processing" % (section,subsection)) logger.debug("No directories identified in %s:%s for post-processing" % (section,subsection))

View file

@ -8,6 +8,6 @@ from nzbtomedia.nzbToMediaUtil import get_downloadInfo
# Initialize the config # Initialize the config
nzbtomedia.initialize() nzbtomedia.initialize()
test = nzbtomedia.CFG['SickBeard','NzbDrone']['tv'] test = nzbtomedia.CFG['SickBeard','NzbDrone']['tv'].isenabled()
section = nzbtomedia.CFG.findsection('tv').isenabled() section = nzbtomedia.CFG.findsection('tv').isenabled()
print section print section