From 8df349bcdbca6003497133b0496e9e25a689704a Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Tue, 29 Oct 2013 13:55:20 +1030 Subject: [PATCH] Added external userscript for "other" cat Torrents. #185 --- TorrentToMedia.py | 46 +++++++++++++++++++++++++++++++++++++ autoProcessMedia.cfg.sample | 18 ++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 2b0ecbc4..16e74eab 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -177,6 +177,8 @@ def main(inputDirectory, inputName, inputCategory, inputHash, inputID): time.sleep(5) # Give Torrent client some time to catch up with the change processCategories = Set([cpsCategory, sbCategory, hpCategory, mlCategory, gzCategory]) + if inputCategory == "": + inputCategory = "UNCAT" if inputCategory and not (inputCategory in processCategories): # no extra processing to be done... yet. Logger.info("MAIN: No further processing to be done for category %s.", inputCategory) @@ -184,6 +186,9 @@ def main(inputDirectory, inputName, inputCategory, inputHash, inputID): elif status == 0 or (inputCategory in [hpCategory, mlCategory, gzCategory]): # if movies linked/extracted or for other categories. Logger.debug("MAIN: Calling autoProcess script for successful download.") status = 0 # hp, my, gz don't support failed. + elif (user_script_categories != "NONE" and inputCategory in user_script_categories) or user_script_categories == "ALL": + Logger.info("MAIN: Processing user script %s.", user_script) + result, deleteOriginal = external_script(outputDestination) else: Logger.error("MAIN: Something failed! Please check logs. Exiting") sys.exit(-1) @@ -233,6 +238,38 @@ def main(inputDirectory, inputName, inputCategory, inputHash, inputID): Logger.info("MAIN: All done.") +def external_script(outputDestination): + + result_final = int(0) # start at 0. + for dirpath, dirnames, filenames in os.walk(outputDestination): + for file in filenames: + + filePath = os.path.join(dirpath, file) + fileName, fileExtension = os.path.splitext(file) + + if fileExtension in user_script_mediaExtensions or user_script_mediaExtensions == "ALL": + command = [user_script] + for param in user_script_param: + if param == "FN": + command.append(file) + continue + if param == "FP": + command.append(filePath) + continue + if param == "DN": + command.append(dirpath) + continue + Logger.info("Running script %s on file %s.", command, filePath) + try: + result = call(command) + except: + Logger.exception("UserScript %s has failed", command) + result = 1 + final_result = final_result + result + + time.sleep(user_delay) + return final_result, user_script_clean + if __name__ == "__main__": #check to migrate old cfg before trying to load. @@ -291,6 +328,15 @@ if __name__ == "__main__": categories.append(hpCategory) categories.append(mlCategory) categories.append(gzCategory) + + user_script_categories = config.get("UserScript", "user_script_categories") # NONE + if user_script_categories != "None" + user_manage_compressed = int(config.get("UserScript", "user_manage_compressed")) + user_script_mediaExtensions = (config.get("UserScript", "user_script_mediaExtensions")).split(',') + user_script = config.get("UserScript", "user_script_path") + user_script_param = (config.get("UserScript", "user_script_param")).split(',') + user_script_clean = int(config.get("UserScript", "user_script_clean")) + user_delay = int(config.get("UserScript", "delay")) transcode = int(config.get("Transcoder", "transcode")) diff --git a/autoProcessMedia.cfg.sample b/autoProcessMedia.cfg.sample index c3f8d1f4..51c30b57 100644 --- a/autoProcessMedia.cfg.sample +++ b/autoProcessMedia.cfg.sample @@ -83,7 +83,7 @@ useLink = hard ###### outputDirectory - Default output directory (categories will be appended as sub directory to outputDirectory) outputDirectory = /abs/path/to/complete/ ###### Other categories/labels defined for your downloader. Does not include CouchPotato, SickBeard, HeadPhones, Mylar categories. -categories = music_videos,pictures,software +categories = music_videos,pictures,software, ###### uTorrent Hardlink solution (You must edit this if your using TorrentToMedia.py with uTorrent) uTorrentWEBui = http://localhost:8090/gui/ uTorrentUSR = your username @@ -126,6 +126,22 @@ host = 192.168.1.37 port = 80 mac = 00:01:2e:2D:64:e1 +[UserScript] +#Use user_script for uncategorized download? +#Set the categories to use external script, comma separated. +#Use "UNCAT" to process non-category downloads, and "ALL" for all. Set to "NONE" to disable external script. +user_script_categories = NONE +#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 of the script +user_script_path = /media/test/script/script.sh +#Specify the argument(s) passed to script, comma separated in order. +#for example FP,FN,DN for file path (absolute file name with path), file anme, absolute directory name (with path). +#So the result is /media/test/script/script.sh FP FN DN +user_script_param = FN +#Clean after? Note that delay function is used to prevent possible mistake :) Delay is intended as seconds +user_script_clean = 1 +delay = 120 # Logging configuration [loggers]