From 2ba29b45415aa09b2cb07a5977a97c581adbdc72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 14:35:47 +0100 Subject: [PATCH 01/10] fixed some variables, small refactor if you're using Transmission in Windows use the .bat file --- TorrentToMedia.bat | 4 + TorrentToMedia.py | 225 ++++++++++++++---------------------- autoProcessMedia.cfg.sample | 6 +- 3 files changed, 96 insertions(+), 139 deletions(-) create mode 100644 TorrentToMedia.bat diff --git a/TorrentToMedia.bat b/TorrentToMedia.bat new file mode 100644 index 00000000..ca7b05b7 --- /dev/null +++ b/TorrentToMedia.bat @@ -0,0 +1,4 @@ +@echo off +echo "Executing nzbToMedia..." +C:\Python27\python.exe C:\Downloaders\nzbToMedia\TorrentToMedia.py %TR_TORRENT_DIR% %TR_TORRENT_NAME% +pause \ No newline at end of file diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 6d9b9b60..1883d050 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -16,7 +16,7 @@ import autoProcessTV from nzbToMediaEnv import * Logger = logging.getLogger() -logFile = os.path.join(os.path.dirname(sys.argv[0]), "postprocess.log") +logFile = os.path.normpath(os.path.join(os.path.dirname(sys.argv[0]), "postprocess.log")) logging.config.fileConfig(os.path.join(os.path.dirname(sys.argv[0]), "logger.conf")) fileHandler = logging.FileHandler(logFile, encoding='utf-8', delay=True) fileHandler.formatter = logging.Formatter('%(asctime)s|%(levelname)-7.7s %(message)s', '%H:%M:%S') @@ -33,44 +33,45 @@ Logger.info("Loading config from %s", configFilename) if not os.path.isfile(configFilename): Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?") sys.exit(-1) +else: + config.read(configFilename) + # Sick-Beard + tvCategory = config.get("SickBeard", "category") + tvDestination = os.path.normpath(config.get("SickBeard", "outputDirectory")) + # CouchPotato + movieDestination = os.path.normpath(config.get("CouchPotato", "outputDirectory")) + movieCategory = config.get("CouchPotato", "category") + useLink = int(config.get("Torrent", "uselink")) + extractionTool = os.path.normpath(config.get("Torrent", "extractiontool")) -config.read(configFilename) - -TV_Cat = config.get("SickBeard", "category") -TV_dest = os.path.normpath(config.get("SickBeard", "destination")) -Movie_dest = os.path.normpath(config.get("CouchPotato", "destination")) -Movie_Cat = config.get("CouchPotato", "category") -useLink = int(config.get("Torrent", "uselink")) -extractionTool = config.get("Torrent", "extractiontool") - -def category_search(Directory, Category, root): - DirBase = os.path.split(os.path.normpath(Directory)) #Test for blackhole sub-directory. - if DirBase[1] == Name: +def category_search(inputDirectory, inputCategory, root): + categorySearch = os.path.split(os.path.normpath(inputDirectory)) #Test for blackhole sub-directory. + if categorySearch[1] == inputName: Logger.info("Files appear to be in their own directory") - DirBase2 = os.path.split(os.path.normpath(DirBase[0])) - if DirBase2[1] == Movie_Cat or DirBase2[1] == TV_Cat: - if not Category: - Logger.info("Determined Category to be: %s", DirBase2[1]) - Category = DirBase2[1] - elif not Category: + categorySearch2 = os.path.split(os.path.normpath(categorySearch[0])) + if categorySearch2[1] == movieCategory or categorySearch2[1] == tvCategory: + if not inputCategory: + Logger.info("Determined Category to be: %s", categorySearch2[1]) + inputCategory = categorySearch2[1] + elif not inputCategory: Logger.error("Could not identify category from the directory structure. please check downlaoder settings.") sys.exit(-1) else: pass - elif DirBase[1] == Movie_Cat or DirBase[1] == TV_Cat: - if os.path.isdir(os.path.join(Directory, Name)): - Logger.info("Found torrent directory %s in category directory %s", os.path.join(Directory, Name), Directory) - Directory = os.path.join(Directory, Name) + elif categorySearch[1] == movieCategory or categorySearch[1] == tvCategory: + if os.path.isdir(os.path.join(inputDirectory, inputName)): + Logger.info("Found torrent directory %s in category directory %s", os.path.join(inputDirectory, inputName), inputDirectory) + inputDirectory = os.path.join(inputDirectory, inputName) else: - Logger.info("The directory passed is the root directory for category %s", DirBase[1]) + Logger.info("The directory passed is the root directory for category %s", categorySearch[1]) Logger.warn("You should change settings to download torrents to their own directory if possible") Logger.info("We will try and determine which files to process, individually") root = 1 - if not Category: - Logger.info("Determined Category to be: %s", DirBase[1]) - Category = DirBase[1] - elif not Category: + if not inputCategory: + Logger.info("Determined Category to be: %s", categorySearch[1]) + inputCategory = categorySearch[1] + elif not inputCategory: Logger.error("Could not identify category from the directory structure. please check downlaoder settings.") sys.exit(-1) else: @@ -78,35 +79,36 @@ def category_search(Directory, Category, root): Logger.warn("You should change settings to download torrents to their own directory if possible and include Label/Category directories.") Logger.info("We will try and determine which files to process, individually") root = 1 - return Directory, Category, root + return inputDirectory, inputCategory, root -def is_sample(file_path, Name): +def is_sample(file_path, inputName): # 200 MB in bytes SIZE_CUTOFF = 200 * 1024 * 1024 # ignore 'sample' in files unless 'sample' in Torrent Name - if ('sample' in file_path.lower()) and (not 'sample' in Name) and (os.path.getsize(file_path) < SIZE_CUTOFF): + if ('sample' in file_path.lower()) and (not 'sample' in inputName) and (os.path.getsize(file_path) < SIZE_CUTOFF): return True else: return False -def copy_link(source, target, useLink, destination): +def copy_link(source, target, useLink, outputDestination): ## Create destination folder - if not os.path.exists(destination): + if not os.path.exists(outputDestination): try: - Logger.debug("Creating destination folder: %s", destination) - os.makedirs(destination) + Logger.debug("Creating destination folder: %s", outputDestination) + os.makedirs(outputDestination) except Exception, e: Logger.error("Not possible to create destination folder: %s", e) return False + if useLink: try: Logger.debug("Linking %s to %s", source, target) linktastic.link(source, target) except: if os.path.isfile(target): - Logger.info("something went wrong in linktastic.link, but the destination file was created") + Logger.info("Something went wrong in linktastic.link, but the destination file was created") else: - Logger.info("something went wrong in linktastic.link. trying to create a copy") + Logger.info("Something went wrong in linktastic.link. trying to create a copy") Logger.debug("Copying %s to %s", source, target) shutil.copy(source, target) else: @@ -114,7 +116,7 @@ def copy_link(source, target, useLink, destination): shutil.copy(source, target) return True -def unpack(dirpath, file, destination): +def unpack(dirpath, file, outputDestination): ## Using Windows? if os.name == 'nt': cmd_7zip = [extractionTool, 'x -y'] @@ -157,30 +159,30 @@ def unpack(dirpath, file, destination): return False ## Create destination folder - if not os.path.exists(destination): + if not os.path.exists(outputDestination): try: - Logger.debug("Creating destination folder: %s", destination) - os.makedirs(destination) + Logger.debug("Creating destination folder: %s", outputDestination) + os.makedirs(outputDestination) except Exception, e: Logger.error("Not possible to create destination folder: %s", e) return False - Logger.info("Extracting %s to %s", fp, destination) + Logger.info("Extracting %s to %s", fp, outputDestination) ## Running.. - Logger.debug("Extracting %s %s %s %s", cmd[0], cmd[1], fp, destination) + Logger.debug("Extracting %s %s %s %s", cmd[0], cmd[1], fp, outputDestination) pwd = os.getcwd() # Get our Present Working Directory - os.chdir(destination) #not all unpack commands accept full paths, so just extract into this directory. + os.chdir(outputDestination) #not all unpack commands accept full paths, so just extract into this directory. if os.name == 'nt': #Windows needs quotes around directory structure try: run = "\"" + cmd[0] + "\" " + cmd[1] + " \"" + fp + "\"" #windows needs quotes around directories. res = call(run) if res == 0: - Logger.info("Extraction was successful for %s to %s", fp, destination) + Logger.info("Extraction was successful for %s to %s", fp, outputDestination) else: Logger.info("Extraction failed for %s. 7zip result was %s", fp, res) except: - Logger.error("Extraction failed for %s. Could not call command %s %s", fp, run) + Logger.error("Extraction failed for %s. Could not call command %s", fp, run) else: try: if cmd[1] == "": #if calling unzip, we dont want to pass the "" @@ -188,7 +190,7 @@ def unpack(dirpath, file, destination): else: res = call([cmd[0], cmd[1], fp]) if res == 0: - Logger.info("Extraction was successful for %s to %s", fp, destination) + Logger.info("Extraction was successful for %s to %s", fp, outputDestination) else: Logger.error("Extraction failed for %s. 7zip result was %s", fp, res) except: @@ -196,17 +198,17 @@ def unpack(dirpath, file, destination): os.chdir(pwd) # Go back to our Original Working Directory return True -def flatten(destination): - Logger.info("Flattening directory: %s", destination) - for dirpath, dirnames, filenames in os.walk(destination): #flatten out the directory to make postprocessing easier. - if dirpath == destination: +def flatten(outputDestination): + Logger.info("Flattening directory: %s", outputDestination) + for dirpath, dirnames, filenames in os.walk(outputDestination): #flatten out the directory to make postprocessing easier. + if dirpath == outputDestination: continue #no need to try and move files in the root destination directory. for filename in filenames: try: - shutil.move(os.path.join(dirpath, filename), destination) + shutil.move(os.path.join(dirpath, filename), outputDestination) except OSError: Logger.info("Could not flatten %s", os.path.join(dirpath, filename)) - removeEmptyFolders(destination) #cleanup empty directories. + removeEmptyFolders(outputDestination) #cleanup empty directories. def removeEmptyFolders(path): Logger.info("Removing empty folders in: %s", path) @@ -227,135 +229,86 @@ def removeEmptyFolders(path): Logger.debug("Removing empty folder: %s", path) os.rmdir(path) -if len(sys.argv) == 4: - ##You can use the following parameters (UTORRENT): - ## - ##%F - Name of downloaded file (for single file torrents) - ##%D - Directory where files are saved - ##%N - Title of torrent - ##%P - Previous state of torrent - ##%L - Label - ##%T - Tracker - ##%M - Status message string (same as status column) - ##%I - hex encoded info-hash - ##%S - State of torrent - ##%K - kind of torrent (single|multi) - ## - ##Where State is one of: - ## - ##Error - 1 - ##Checked - 2 - ##Paused - 3 - ##Super seeding - 4 - ##Seeding - 5 - ##Downloading - 6 - ##Super seed [F] - 7 - ##Seeding [F] - 8 - ##Downloading [F] - 9 - ##Queued seed - 10 - ##Finished - 11 - ##Queued - 12 - ##Stopped - 13 - - ## We will pass in %D, %N, %L from uTorrent - Logger.info("Script called from utorrent") - Directory = os.path.normpath(sys.argv[1]) ## %D -- Example output: F:\path\to\dir\My.Series.S01E01.720p.HDTV.x264-2HD - Name = sys.argv[2] ## %N -- Example output: My.Series.S01E01.720p.HDTV.x264-2HD - Category = sys.argv[3] ## %L -- Example output: tvseries ## This is the label in uTorrent - -elif len(sys.argv) > 1: #Doesn't match Transmission (1) or uTorrent (4). - Logger.error("The number of arguments passed is %s. Unable to determin the arguments to use; Exiting", len(sys.argv)) +if len(sys.argv) == 3: + ## We will pass in %D, %N from uTorrent, or %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission + inputDirectory = os.path.normpath(sys.argv[1]) + inputName = sys.argv[2] + inputCategory = '' # We dont have a category, so assume the last directory is the category for now. +else: + Logger.error("There was a problem loading variables: Exiting") sys.exit(-1) -else: - ##test for Transmission here. - #TR_APP_VERSION - #TR_TIME_LOCALTIME - #TR_TORRENT_DIR - #TR_TORRENT_HASH - #TR_TORRENT_ID - #TR_TORRENT_NAME - try: - Directory = os.path.normpath(os.getenv('TR_TORRENT_DIR')) - Name = os.getenv('TR_TORRENT_NAME') - Logger.info("Script called from Transmission") - except: - Logger.error("There was a problem loading variables from Transmission: Exiting") - sys.exit(-1) - Category = '' #We dont have a category, so assume the last directory is the category for now. - -Logger.debug("Received Directory: %s", Directory) -Logger.debug("Received Torrent Name: %s", Name) -Logger.debug("Received Category: %s", Category) +Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName) status = 1 # we start as "failed" until we verify movie file in destination root = 0 video = 0 video2 = 0 -Directory, Category, root = category_search(Directory, Category, root) # confirm the catgeogy by parsing directory structure. - -if Category == Movie_Cat: - destination = os.path.join(Movie_dest, Name) -elif Category == TV_Cat: - destination = os.path.join(TV_dest, Name) +inputDirectory, inputCategory, root = category_search(inputDirectory, inputCategory, root) # confirm the catgeogy by parsing directory structure. + +if inputCategory == movieCategory: + outputDestination = os.path.normpath(os.path.join(movieDestination, inputName)) +elif inputCategory == tvCategory: + outputDestination = os.path.normpath(os.path.join(tvDestination, inputName)) else: - Logger.info("Category of %s does not match either %s or %s: Exiting", Category, Movie_Cat, TV_Cat) + Logger.info("Category of %s does not match either %s or %s: Exiting", inputCategory, movieCategory, tvCategory) sys.exit(-1) packed_files = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj'] video_files = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.vob', '.iso'] meta_files = ['.nfo', '.sub', '.srt', '.jpg', '.gif'] -Logger.debug("scanning files in directory: %s", Directory) -for dirpath, dirnames, filenames in os.walk(Directory): +Logger.debug("Scanning files in directory: %s", inputDirectory) + +for dirpath, dirnames, filenames in os.walk(inputDirectory): for file in filenames: if root == 1: - Logger.debug("Looking for %s in filename", Name) - if (Name in file) or (file in Name): + Logger.debug("Looking for %s in filename", inputName) + if (inputName in file) or (file in inputName): pass #This file does match the Torrent name else: continue #This file does not match the Torrent name. Skip it file_path = os.path.join(dirpath, file) file_ext = os.path.splitext(file)[1] if file_ext in video_files: #if the file is a video file. - if is_sample(file_path, Name): + if is_sample(file_path, inputName): Logger.info("file %s is a sample file. Ignoring", file_path) continue #ignore samples video = video + 1 source = file_path - target = os.path.join(destination, file) + target = os.path.join(outputDestination, file) Logger.info("Found video file %s.", file) - state = copy_link(source, target, useLink, destination) + state = copy_link(source, target, useLink, outputDestination) if state == False: Logger.info("Failed to link file %s.", file) elif file_ext in meta_files: source = file_path - target = os.path.join(destination, file) + target = os.path.join(outputDestination, file) Logger.info("Found metadata file %s.", file) - state = copy_link(source, target, useLink, destination) + state = copy_link(source, target, useLink, outputDestination) if state == False: Logger.info("Failed to link file %s.", file) elif file_ext in packed_files: Logger.info("Found packed file %s.", file) source = file_path - target = os.path.join(destination, file) - state = unpack(dirpath, file, destination) + target = os.path.join(outputDestination, file) + state = unpack(dirpath, file, outputDestination) if state == False: Logger.info("Failed to unpack file %s.", file) else: Logger.info("Unknown file type %s for file %s. Ignoring", file_ext, file_path) continue -flatten(destination) +flatten(outputDestination) #now check if movie files exist in destination: -for dirpath, dirnames, filenames in os.walk(destination): +for dirpath, dirnames, filenames in os.walk(outputDestination): for file in filenames: file_path = os.path.join(dirpath, file) file_ext = os.path.splitext(file)[1] if file_ext in video_files: #if the file is a video file. video2 = video2 + 1 -if video2 >= video and video2 > 0: #check that all video files were moved. +if video2 >= video and video2 > 0: #check that all video files were moved. status = 0 status = int(status) @@ -365,7 +318,7 @@ else: Logger.info("calling autoProcess script for failed download") ## Now we pass off to CouchPotato or SickBeard. # still need to figure out how to log this output. -if Category == Movie_Cat: - autoProcessMovie.process(destination, Name, status) -elif Category == TV_Cat: - autoProcessTV.processEpisode(destination, Name, status) +if inputCategory == movieCategory: + autoProcessMovie.process(outputDestination, inputName, status) +elif inputCategory == tvCategory: + autoProcessTV.processEpisode(outputDestination, inputName, status) diff --git a/autoProcessMedia.cfg.sample b/autoProcessMedia.cfg.sample index c92230f3..e385f252 100644 --- a/autoProcessMedia.cfg.sample +++ b/autoProcessMedia.cfg.sample @@ -10,7 +10,7 @@ delay = 60 method = renamer delete_failed = 0 category = movies -destination = /abs/path/to/complete/movies +outputDirectory = /abs/path/to/complete/movies [SickBeard] host=localhost @@ -22,9 +22,9 @@ ssl=0 watch_dir= failed_fork=0 category = tv -destination = /abs/path/to/complete/tv +outputDirectory = /abs/path/to/complete/tv [Torrent] uselink = 0 -extractiontool = 'C:\\Program Files\\7-Zip\\7z.exe' +extractiontool = C:\Program Files\7-Zip\7z.exe From 141eaf945696f029ecf80eb7a5a86d1520e32c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 15:30:08 +0100 Subject: [PATCH 02/10] cleanup cleaner variables restructure, we dont need to exectute code thats not needed --- TorrentToMedia.py | 238 +++++++++++++++++++++++----------------------- 1 file changed, 117 insertions(+), 121 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 1883d050..5a9c455a 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -23,27 +23,6 @@ fileHandler.formatter = logging.Formatter('%(asctime)s|%(levelname)-7.7s %(messa fileHandler.level = logging.DEBUG Logger.addHandler(fileHandler) -Logger.info("TorrentToMedia %s", VERSION) - -config = ConfigParser.ConfigParser() -configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") - -Logger.info("Loading config from %s", configFilename) - -if not os.path.isfile(configFilename): - Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?") - sys.exit(-1) -else: - config.read(configFilename) - # Sick-Beard - tvCategory = config.get("SickBeard", "category") - tvDestination = os.path.normpath(config.get("SickBeard", "outputDirectory")) - # CouchPotato - movieDestination = os.path.normpath(config.get("CouchPotato", "outputDirectory")) - movieCategory = config.get("CouchPotato", "category") - useLink = int(config.get("Torrent", "uselink")) - extractionTool = os.path.normpath(config.get("Torrent", "extractiontool")) - def category_search(inputDirectory, inputCategory, root): categorySearch = os.path.split(os.path.normpath(inputDirectory)) #Test for blackhole sub-directory. if categorySearch[1] == inputName: @@ -81,11 +60,11 @@ def category_search(inputDirectory, inputCategory, root): root = 1 return inputDirectory, inputCategory, root -def is_sample(file_path, inputName): +def is_sample(filePath, inputName): # 200 MB in bytes SIZE_CUTOFF = 200 * 1024 * 1024 # ignore 'sample' in files unless 'sample' in Torrent Name - if ('sample' in file_path.lower()) and (not 'sample' in inputName) and (os.path.getsize(file_path) < SIZE_CUTOFF): + if ('sample' in filePath.lower()) and (not 'sample' in inputName) and (os.path.getsize(filePath) < SIZE_CUTOFF): return True else: return False @@ -117,16 +96,16 @@ def copy_link(source, target, useLink, outputDestination): return True def unpack(dirpath, file, outputDestination): - ## Using Windows? + ## Using Windows if os.name == 'nt': - cmd_7zip = [extractionTool, 'x -y'] + cmd_7zip = [extractionTool, 'x -y'] ## We need to add a check if 7zip is actully present, or exit ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"] EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip) Logger.info("We are using Windows") - - ## Using linux? + + ## Using linux elif os.name == 'posix': - required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] + required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] ## Need to add a check for which commands that can be utilized in *nix systems EXTRACT_COMMANDS = { ".rar": ["unrar", "x -o+ -y"], ".zip": ["unzip", ""], @@ -140,10 +119,8 @@ def unpack(dirpath, file, outputDestination): ".7z": ["7zr", "x"], } Logger.info("We are using *nix") - - ## Need to add a check for which commands that can be utilized in *nix systems.. else: - Logger.error("Unknown OS, exiting") + Logger.error("Cant determine host OS while extracting, Exiting") ext = os.path.splitext(file) fp = os.path.join(dirpath, file) @@ -169,11 +146,11 @@ def unpack(dirpath, file, outputDestination): Logger.info("Extracting %s to %s", fp, outputDestination) - ## Running.. + ## Running extraction process Logger.debug("Extracting %s %s %s %s", cmd[0], cmd[1], fp, outputDestination) - pwd = os.getcwd() # Get our Present Working Directory - os.chdir(outputDestination) #not all unpack commands accept full paths, so just extract into this directory. - if os.name == 'nt': #Windows needs quotes around directory structure + pwd = os.getcwd() # Get our present working directory + os.chdir(outputDestination) # Not all unpack commands accept full paths, so just extract into this directory. + if os.name == 'nt': # Windows needs quotes around directory structure try: run = "\"" + cmd[0] + "\" " + cmd[1] + " \"" + fp + "\"" #windows needs quotes around directories. res = call(run) @@ -229,96 +206,115 @@ def removeEmptyFolders(path): Logger.debug("Removing empty folder: %s", path) os.rmdir(path) -if len(sys.argv) == 3: - ## We will pass in %D, %N from uTorrent, or %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission - inputDirectory = os.path.normpath(sys.argv[1]) - inputName = sys.argv[2] - inputCategory = '' # We dont have a category, so assume the last directory is the category for now. -else: - Logger.error("There was a problem loading variables: Exiting") +Logger.info("TorrentToMedia %s", VERSION) +config = ConfigParser.ConfigParser() +configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") + +if not os.path.isfile(configFilename): + Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?") sys.exit(-1) - -Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName) - -status = 1 # we start as "failed" until we verify movie file in destination -root = 0 -video = 0 -video2 = 0 - -inputDirectory, inputCategory, root = category_search(inputDirectory, inputCategory, root) # confirm the catgeogy by parsing directory structure. - -if inputCategory == movieCategory: - outputDestination = os.path.normpath(os.path.join(movieDestination, inputName)) -elif inputCategory == tvCategory: - outputDestination = os.path.normpath(os.path.join(tvDestination, inputName)) else: - Logger.info("Category of %s does not match either %s or %s: Exiting", inputCategory, movieCategory, tvCategory) - sys.exit(-1) + Logger.info("Loading config from %s", configFilename) + config.read(configFilename) -packed_files = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj'] -video_files = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.vob', '.iso'] -meta_files = ['.nfo', '.sub', '.srt', '.jpg', '.gif'] + if len(sys.argv) == 3: + ## We will pass in %D, %N from uTorrent, or %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission + inputDirectory = os.path.normpath(sys.argv[1]) + inputName = sys.argv[2] + + # Sick-Beard + tvCategory = config.get("SickBeard", "category") + tvDestination = os.path.normpath(config.get("SickBeard", "outputDirectory")) + # CouchPotatoServer + movieCategory = config.get("CouchPotato", "category") + movieDestination = os.path.normpath(config.get("CouchPotato", "outputDirectory")) + # Torrent specific + useLink = int(config.get("Torrent", "uselink")) + extractionTool = os.path.normpath(config.get("Torrent", "extractiontool")) + inputCategory = '' # We dont have a category yet -Logger.debug("Scanning files in directory: %s", inputDirectory) - -for dirpath, dirnames, filenames in os.walk(inputDirectory): - for file in filenames: - if root == 1: - Logger.debug("Looking for %s in filename", inputName) - if (inputName in file) or (file in inputName): - pass #This file does match the Torrent name - else: - continue #This file does not match the Torrent name. Skip it - file_path = os.path.join(dirpath, file) - file_ext = os.path.splitext(file)[1] - if file_ext in video_files: #if the file is a video file. - if is_sample(file_path, inputName): - Logger.info("file %s is a sample file. Ignoring", file_path) - continue #ignore samples - video = video + 1 - source = file_path - target = os.path.join(outputDestination, file) - Logger.info("Found video file %s.", file) - state = copy_link(source, target, useLink, outputDestination) - if state == False: - Logger.info("Failed to link file %s.", file) - elif file_ext in meta_files: - source = file_path - target = os.path.join(outputDestination, file) - Logger.info("Found metadata file %s.", file) - state = copy_link(source, target, useLink, outputDestination) - if state == False: - Logger.info("Failed to link file %s.", file) - elif file_ext in packed_files: - Logger.info("Found packed file %s.", file) - source = file_path - target = os.path.join(outputDestination, file) - state = unpack(dirpath, file, outputDestination) - if state == False: - Logger.info("Failed to unpack file %s.", file) + status = 1 # we start as "failed" until we verify movie file in destination + root = 0 + video = 0 + video2 = 0 + compressedContainer = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj'] + mediaContainer = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.vob', '.iso'] + metaFile = ['.nfo', '.sub', '.srt', '.jpg', '.gif'] + + Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName) + inputDirectory, inputCategory, root = category_search(inputDirectory, inputCategory, root) # confirm the catgeogy by parsing directory structure. + if inputCategory == movieCategory: + outputDestination = os.path.normpath(os.path.join(movieDestination, inputName)) + elif inputCategory == tvCategory: + outputDestination = os.path.normpath(os.path.join(tvDestination, inputName)) else: - Logger.info("Unknown file type %s for file %s. Ignoring", file_ext, file_path) - continue -flatten(outputDestination) + Logger.info("Category of %s does not match either %s or %s: Exiting", inputCategory, movieCategory, tvCategory) + sys.exit(-1) -#now check if movie files exist in destination: -for dirpath, dirnames, filenames in os.walk(outputDestination): - for file in filenames: - file_path = os.path.join(dirpath, file) - file_ext = os.path.splitext(file)[1] - if file_ext in video_files: #if the file is a video file. - video2 = video2 + 1 -if video2 >= video and video2 > 0: #check that all video files were moved. - status = 0 + Logger.debug("Scanning files in directory: %s", inputDirectory) + for dirpath, dirnames, filenames in os.walk(inputDirectory): + for file in filenames: + if root == 1: + Logger.debug("Looking for %s in filename", inputName) + if (inputName in file) or (file in inputName): + pass #This file does match the Torrent name + else: + continue #This file does not match the Torrent name, skip it + filePath = os.path.join(dirpath, file) + fileExtention = os.path.splitext(file)[1] + if fileExtention in mediaContainer: #if the file is a video file. + if is_sample(filePath, inputName): # Ignore samples + Logger.info("File %s is a sample file. Ignoring", filePath) + continue + else: + video = video + 1 + source = filePath + target = os.path.join(outputDestination, file) + Logger.info("Found video file %s.", file) + state = copy_link(source, target, useLink, outputDestination) + if state == False: + Logger.info("Failed to link file %s.", file) + elif fileExtention in metaFile: + source = filePath + target = os.path.join(outputDestination, file) + Logger.info("Found metadata file %s.", file) + state = copy_link(source, target, useLink, outputDestination) + if state == False: + Logger.info("Failed to link file %s.", file) + elif fileExtention in compressedContainer: + Logger.info("Found compressed archive %s.", file) + source = filePath + target = os.path.join(outputDestination, file) + state = unpack(dirpath, file, outputDestination) + if state == False: + Logger.info("Failed to unpack file %s.", file) + else: + Logger.info("Unknown file type %s for file %s. Ignoring", fileExtention, filePath) + continue + flatten(outputDestination) -status = int(status) -if status == 0: - Logger.info("calling autoProcess script for successful download") -else: - Logger.info("calling autoProcess script for failed download") -## Now we pass off to CouchPotato or SickBeard. -# still need to figure out how to log this output. -if inputCategory == movieCategory: - autoProcessMovie.process(outputDestination, inputName, status) -elif inputCategory == tvCategory: - autoProcessTV.processEpisode(outputDestination, inputName, status) + #now check if movie files exist in destination: + for dirpath, dirnames, filenames in os.walk(outputDestination): + for file in filenames: + filePath = os.path.join(dirpath, file) + fileExtention = os.path.splitext(file)[1] + if fileExtention in mediaContainer: #if the file is a video file. + video2 = video2 + 1 + if video2 >= video and video2 > 0: #check that all video files were moved. + status = 0 + + if status == 0: + Logger.info("Successful download") + ## Now we pass off to CouchPotato or SickBeard. + # still need to figure out how to log this output. + if inputCategory == movieCategory: + Logger.info("Calling postprocessing script for CouchPotatoServer") + autoProcessMovie.process(outputDestination, inputName, status) + elif inputCategory == tvCategory: + Logger.info("Calling postprocessing script for Sick-Beard") + autoProcessTV.processEpisode(outputDestination, inputName, status) + else: + Logger.info("Postprocessing failed") + else: + Logger.error("There was a problem loading variables: Exiting") + sys.exit(-1) \ No newline at end of file From dc175f98183af910a97976901f22e304ef2c651e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 15:46:52 +0100 Subject: [PATCH 03/10] extentions moved to config file --- TorrentToMedia.py | 22 +++++++++++----------- autoProcessMedia.cfg.sample | 22 ++++++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 5a9c455a..9583d9bf 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -221,7 +221,8 @@ else: ## We will pass in %D, %N from uTorrent, or %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission inputDirectory = os.path.normpath(sys.argv[1]) inputName = sys.argv[2] - + Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName) + # Sick-Beard tvCategory = config.get("SickBeard", "category") tvDestination = os.path.normpath(config.get("SickBeard", "outputDirectory")) @@ -231,17 +232,16 @@ else: # Torrent specific useLink = int(config.get("Torrent", "uselink")) extractionTool = os.path.normpath(config.get("Torrent", "extractiontool")) + compressedContainer = config.get("Torrent", "compressedExtentions") + mediaContainer = config.get("Torrent", "mediaExtentions") + metaContainer = config.get("Torrent", "metaExtentions") + + status = int(1) # we start as "failed" until we verify movie file in destination + root = int(0) + video = int(0) + video2 = int(0) inputCategory = '' # We dont have a category yet - status = 1 # we start as "failed" until we verify movie file in destination - root = 0 - video = 0 - video2 = 0 - compressedContainer = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj'] - mediaContainer = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.vob', '.iso'] - metaFile = ['.nfo', '.sub', '.srt', '.jpg', '.gif'] - - Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName) inputDirectory, inputCategory, root = category_search(inputDirectory, inputCategory, root) # confirm the catgeogy by parsing directory structure. if inputCategory == movieCategory: outputDestination = os.path.normpath(os.path.join(movieDestination, inputName)) @@ -274,7 +274,7 @@ else: state = copy_link(source, target, useLink, outputDestination) if state == False: Logger.info("Failed to link file %s.", file) - elif fileExtention in metaFile: + elif fileExtention in metaContainer: source = filePath target = os.path.join(outputDestination, file) Logger.info("Found metadata file %s.", file) diff --git a/autoProcessMedia.cfg.sample b/autoProcessMedia.cfg.sample index e385f252..70b7ddb5 100644 --- a/autoProcessMedia.cfg.sample +++ b/autoProcessMedia.cfg.sample @@ -1,18 +1,23 @@ [CouchPotato] +category = movie +outputDirectory = /abs/path/to/complete/movies +apikey = +###### ADVANCED USE - ONLY EDIT IF YOU KNOW WHAT YOU'RE DOING ###### host = localhost port = 5050 username = password = ssl = 0 web_root = -apikey = xxxxxxxx delay = 60 method = renamer delete_failed = 0 -category = movies -outputDirectory = /abs/path/to/complete/movies + [SickBeard] +category = tv +outputDirectory = /abs/path/to/complete/tv +###### ADVANCED USE - ONLY EDIT IF YOU KNOW WHAT YOU'RE DOING ###### host=localhost port=8081 username= @@ -21,10 +26,11 @@ web_root= ssl=0 watch_dir= failed_fork=0 -category = tv -outputDirectory = /abs/path/to/complete/tv [Torrent] -uselink = 0 -extractiontool = C:\Program Files\7-Zip\7z.exe - +uselink = 1 +###### ADVANCED USE - ONLY EDIT IF YOU KNOW WHAT YOU'RE DOING ###### +compressedExtentions = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj'] +mediaExtentions = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.vob', '.iso'] +metaExtentions = ['.nfo', '.sub', '.srt', '.jpg', '.gif'] +extractionTool = C:\Program Files\7-Zip\7z.exe \ No newline at end of file From 13e7f25c44d5f2cde48058dbc0b4460f53c3091c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 15:49:01 +0100 Subject: [PATCH 04/10] dont use link with default config --- TorrentToMedia.py | 4 ++-- autoProcessMedia.cfg.sample | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 9583d9bf..37bb7981 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -230,8 +230,8 @@ else: movieCategory = config.get("CouchPotato", "category") movieDestination = os.path.normpath(config.get("CouchPotato", "outputDirectory")) # Torrent specific - useLink = int(config.get("Torrent", "uselink")) - extractionTool = os.path.normpath(config.get("Torrent", "extractiontool")) + useLink = int(config.get("Torrent", "useLink")) + extractionTool = os.path.normpath(config.get("Torrent", "extractionTool")) compressedContainer = config.get("Torrent", "compressedExtentions") mediaContainer = config.get("Torrent", "mediaExtentions") metaContainer = config.get("Torrent", "metaExtentions") diff --git a/autoProcessMedia.cfg.sample b/autoProcessMedia.cfg.sample index 70b7ddb5..b74ebd8f 100644 --- a/autoProcessMedia.cfg.sample +++ b/autoProcessMedia.cfg.sample @@ -28,9 +28,9 @@ watch_dir= failed_fork=0 [Torrent] -uselink = 1 +useLink = 0 +extractionTool = C:\Program Files\7-Zip\7z.exe ###### ADVANCED USE - ONLY EDIT IF YOU KNOW WHAT YOU'RE DOING ###### compressedExtentions = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj'] mediaExtentions = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.vob', '.iso'] metaExtentions = ['.nfo', '.sub', '.srt', '.jpg', '.gif'] -extractionTool = C:\Program Files\7-Zip\7z.exe \ No newline at end of file From c1906a6b1687fd5a685df99ad525ea41aa9d7e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 16:09:17 +0100 Subject: [PATCH 05/10] a few more fixes --- TorrentToMedia.py | 93 +++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 37bb7981..e218f8c2 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -24,7 +24,7 @@ fileHandler.level = logging.DEBUG Logger.addHandler(fileHandler) def category_search(inputDirectory, inputCategory, root): - categorySearch = os.path.split(os.path.normpath(inputDirectory)) #Test for blackhole sub-directory. + categorySearch = os.path.split(os.path.normpath(inputDirectory)) # Test for blackhole sub-directory if categorySearch[1] == inputName: Logger.info("Files appear to be in their own directory") categorySearch2 = os.path.split(os.path.normpath(categorySearch[0])) @@ -33,7 +33,7 @@ def category_search(inputDirectory, inputCategory, root): Logger.info("Determined Category to be: %s", categorySearch2[1]) inputCategory = categorySearch2[1] elif not inputCategory: - Logger.error("Could not identify category from the directory structure. please check downlaoder settings.") + Logger.error("Could not identify category from the directory structure. please check downlaoder settings") sys.exit(-1) else: pass @@ -51,11 +51,11 @@ def category_search(inputDirectory, inputCategory, root): Logger.info("Determined Category to be: %s", categorySearch[1]) inputCategory = categorySearch[1] elif not inputCategory: - Logger.error("Could not identify category from the directory structure. please check downlaoder settings.") + Logger.error("Could not identify category from the directory structure. please check downlaoder settings") sys.exit(-1) else: Logger.info("The directory passed does not appear to include a category or the torrent name") - Logger.warn("You should change settings to download torrents to their own directory if possible and include Label/Category directories.") + Logger.warn("You should change settings to download torrents to their own directory if possible and include Label/Category directories") Logger.info("We will try and determine which files to process, individually") root = 1 return inputDirectory, inputCategory, root @@ -63,14 +63,14 @@ def category_search(inputDirectory, inputCategory, root): def is_sample(filePath, inputName): # 200 MB in bytes SIZE_CUTOFF = 200 * 1024 * 1024 - # ignore 'sample' in files unless 'sample' in Torrent Name + # Ignore 'sample' in files unless 'sample' in Torrent Name if ('sample' in filePath.lower()) and (not 'sample' in inputName) and (os.path.getsize(filePath) < SIZE_CUTOFF): return True else: return False def copy_link(source, target, useLink, outputDestination): - ## Create destination folder + # Create destination folder if not os.path.exists(outputDestination): try: Logger.debug("Creating destination folder: %s", outputDestination) @@ -96,16 +96,22 @@ def copy_link(source, target, useLink, outputDestination): return True def unpack(dirpath, file, outputDestination): - ## Using Windows + # Using Windows if os.name == 'nt': - cmd_7zip = [extractionTool, 'x -y'] ## We need to add a check if 7zip is actully present, or exit - ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"] - EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip) - Logger.info("We are using Windows") + Logger.info("We are using Windows") + if not os.path.exists(extractionTool): + Logger.error("Cant find 7-zip, Exiting") + sys.exit(-1) + else: + Logger.debug("7-zip found") + cmd_7zip = [extractionTool, 'x -y'] # We need to add a check if 7zip is actully present, or exit + ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"] + EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip) - ## Using linux + # Using Linux elif os.name == 'posix': - required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] ## Need to add a check for which commands that can be utilized in *nix systems + Logger.info("We are using *nix") + required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] # Need to add a check for which commands that can be utilized in *nix systems EXTRACT_COMMANDS = { ".rar": ["unrar", "x -o+ -y"], ".zip": ["unzip", ""], @@ -118,14 +124,13 @@ def unpack(dirpath, file, outputDestination): ".txz": ["tar", "--xz xf"], ".7z": ["7zr", "x"], } - Logger.info("We are using *nix") else: Logger.error("Cant determine host OS while extracting, Exiting") ext = os.path.splitext(file) fp = os.path.join(dirpath, file) if ext[1] in (".gz", ".bz2", ".lzma"): - ## Check if this is a tar + # Check if this is a tar if os.path.splitext(ext[0])[1] == ".tar": cmd = EXTRACT_COMMANDS[".tar" + ext[1]] else: @@ -135,7 +140,7 @@ def unpack(dirpath, file, outputDestination): Logger.debug("Unknown file type: %s", ext[1]) return False - ## Create destination folder + # Create destination folder if not os.path.exists(outputDestination): try: Logger.debug("Creating destination folder: %s", outputDestination) @@ -145,24 +150,23 @@ def unpack(dirpath, file, outputDestination): return False Logger.info("Extracting %s to %s", fp, outputDestination) - - ## Running extraction process + # Running extraction process Logger.debug("Extracting %s %s %s %s", cmd[0], cmd[1], fp, outputDestination) pwd = os.getcwd() # Get our present working directory - os.chdir(outputDestination) # Not all unpack commands accept full paths, so just extract into this directory. + os.chdir(outputDestination) # Not all unpack commands accept full paths, so just extract into this directory if os.name == 'nt': # Windows needs quotes around directory structure try: - run = "\"" + cmd[0] + "\" " + cmd[1] + " \"" + fp + "\"" #windows needs quotes around directories. + run = "\"" + cmd[0] + "\" " + cmd[1] + " \"" + fp + "\"" # Windows needs quotes around directories res = call(run) if res == 0: Logger.info("Extraction was successful for %s to %s", fp, outputDestination) else: - Logger.info("Extraction failed for %s. 7zip result was %s", fp, res) + Logger.error("Extraction failed for %s. 7zip result was %s", fp, res) except: Logger.error("Extraction failed for %s. Could not call command %s", fp, run) else: try: - if cmd[1] == "": #if calling unzip, we dont want to pass the "" + if cmd[1] == "": # If calling unzip, we dont want to pass the "" res = call([cmd[0], fp]) else: res = call([cmd[0], cmd[1], fp]) @@ -177,22 +181,22 @@ def unpack(dirpath, file, outputDestination): def flatten(outputDestination): Logger.info("Flattening directory: %s", outputDestination) - for dirpath, dirnames, filenames in os.walk(outputDestination): #flatten out the directory to make postprocessing easier. + for dirpath, dirnames, filenames in os.walk(outputDestination): #flatten out the directory to make postprocessing easier if dirpath == outputDestination: - continue #no need to try and move files in the root destination directory. + continue # No need to try and move files in the root destination directory for filename in filenames: try: shutil.move(os.path.join(dirpath, filename), outputDestination) except OSError: Logger.info("Could not flatten %s", os.path.join(dirpath, filename)) - removeEmptyFolders(outputDestination) #cleanup empty directories. + removeEmptyFolders(outputDestination) # Cleanup empty directories def removeEmptyFolders(path): Logger.info("Removing empty folders in: %s", path) if not os.path.isdir(path): return - # remove empty subfolders + # Remove empty subfolders files = os.listdir(path) if len(files): for f in files: @@ -200,7 +204,7 @@ def removeEmptyFolders(path): if os.path.isdir(fullpath): removeEmptyFolders(fullpath) - # if folder empty, delete it + # If folder empty, delete it files = os.listdir(path) if len(files) == 0: Logger.debug("Removing empty folder: %s", path) @@ -218,7 +222,8 @@ else: config.read(configFilename) if len(sys.argv) == 3: - ## We will pass in %D, %N from uTorrent, or %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission + # We will pass in %D, %N from uTorrent, or %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission (Transmission needs additional script, see TorrentToMedia.sh and TorrentToMedia.bat + # In short pass "/path/to/downloaded/torrent/ name" to TorrentToMedia.py, eg >>>> TorrentToMedia.py /Downloaded/MovieName.2013.BluRay.1080p.x264-10bit.DTS MovieName.2013.BluRay.1080p.x264-10bit.DTS <<<< inputDirectory = os.path.normpath(sys.argv[1]) inputName = sys.argv[2] Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName) @@ -236,19 +241,19 @@ else: mediaContainer = config.get("Torrent", "mediaExtentions") metaContainer = config.get("Torrent", "metaExtentions") - status = int(1) # we start as "failed" until we verify movie file in destination + status = int(1) # We start as "failed" until we verify movie file in destination root = int(0) video = int(0) video2 = int(0) inputCategory = '' # We dont have a category yet - inputDirectory, inputCategory, root = category_search(inputDirectory, inputCategory, root) # confirm the catgeogy by parsing directory structure. + inputDirectory, inputCategory, root = category_search(inputDirectory, inputCategory, root) # Confirm the catgeogy by parsing directory structure if inputCategory == movieCategory: outputDestination = os.path.normpath(os.path.join(movieDestination, inputName)) elif inputCategory == tvCategory: outputDestination = os.path.normpath(os.path.join(tvDestination, inputName)) else: - Logger.info("Category of %s does not match either %s or %s: Exiting", inputCategory, movieCategory, tvCategory) + Logger.error("Category of %s does not match either %s or %s: Exiting", inputCategory, movieCategory, tvCategory) sys.exit(-1) Logger.debug("Scanning files in directory: %s", inputDirectory) @@ -257,12 +262,12 @@ else: if root == 1: Logger.debug("Looking for %s in filename", inputName) if (inputName in file) or (file in inputName): - pass #This file does match the Torrent name + pass # This file does match the Torrent name else: - continue #This file does not match the Torrent name, skip it + continue # This file does not match the Torrent name, skip it filePath = os.path.join(dirpath, file) fileExtention = os.path.splitext(file)[1] - if fileExtention in mediaContainer: #if the file is a video file. + if fileExtention in mediaContainer: # If the file is a video file if is_sample(filePath, inputName): # Ignore samples Logger.info("File %s is a sample file. Ignoring", filePath) continue @@ -270,43 +275,43 @@ else: video = video + 1 source = filePath target = os.path.join(outputDestination, file) - Logger.info("Found video file %s.", file) + Logger.info("Found video file %s", file) state = copy_link(source, target, useLink, outputDestination) if state == False: - Logger.info("Failed to link file %s.", file) + Logger.info("Failed to link file %s", file) elif fileExtention in metaContainer: source = filePath target = os.path.join(outputDestination, file) Logger.info("Found metadata file %s.", file) state = copy_link(source, target, useLink, outputDestination) if state == False: - Logger.info("Failed to link file %s.", file) + Logger.info("Failed to link file %s", file) elif fileExtention in compressedContainer: - Logger.info("Found compressed archive %s.", file) + Logger.info("Found compressed archive %s", file) source = filePath target = os.path.join(outputDestination, file) state = unpack(dirpath, file, outputDestination) if state == False: - Logger.info("Failed to unpack file %s.", file) + Logger.info("Failed to unpack file %s", file) else: Logger.info("Unknown file type %s for file %s. Ignoring", fileExtention, filePath) continue flatten(outputDestination) - #now check if movie files exist in destination: + # Now check if movie files exist in destination: for dirpath, dirnames, filenames in os.walk(outputDestination): for file in filenames: filePath = os.path.join(dirpath, file) fileExtention = os.path.splitext(file)[1] - if fileExtention in mediaContainer: #if the file is a video file. + if fileExtention in mediaContainer: # If the file is a video file video2 = video2 + 1 - if video2 >= video and video2 > 0: #check that all video files were moved. + if video2 >= video and video2 > 0: # Check that all video files were moved status = 0 if status == 0: Logger.info("Successful download") - ## Now we pass off to CouchPotato or SickBeard. - # still need to figure out how to log this output. + # Now we pass off to CouchPotato or Sick-Beard + # Still need to figure out how to log this output if inputCategory == movieCategory: Logger.info("Calling postprocessing script for CouchPotatoServer") autoProcessMovie.process(outputDestination, inputName, status) From b535d119042ac7460186e8d12e869771c7902c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 17:04:01 +0100 Subject: [PATCH 06/10] indentation error --- TorrentToMedia.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index e218f8c2..b66154a4 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -98,7 +98,7 @@ def copy_link(source, target, useLink, outputDestination): def unpack(dirpath, file, outputDestination): # Using Windows if os.name == 'nt': - Logger.info("We are using Windows") + Logger.info("We are using Windows") if not os.path.exists(extractionTool): Logger.error("Cant find 7-zip, Exiting") sys.exit(-1) @@ -110,7 +110,7 @@ def unpack(dirpath, file, outputDestination): # Using Linux elif os.name == 'posix': - Logger.info("We are using *nix") + Logger.info("We are using *nix") required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] # Need to add a check for which commands that can be utilized in *nix systems EXTRACT_COMMANDS = { ".rar": ["unrar", "x -o+ -y"], @@ -309,7 +309,7 @@ else: status = 0 if status == 0: - Logger.info("Successful download") + Logger.info("Successful run") # Now we pass off to CouchPotato or Sick-Beard # Still need to figure out how to log this output if inputCategory == movieCategory: @@ -319,7 +319,7 @@ else: Logger.info("Calling postprocessing script for Sick-Beard") autoProcessTV.processEpisode(outputDestination, inputName, status) else: - Logger.info("Postprocessing failed") + Logger.info("Something failed! :(") else: Logger.error("There was a problem loading variables: Exiting") sys.exit(-1) \ No newline at end of file From 25c3b3d2b72d91aeddf839a9c65d3f35b8b15dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 17:24:33 +0100 Subject: [PATCH 07/10] test extractor commands on unix before executing --- TorrentToMedia.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index b66154a4..7a80924c 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -23,6 +23,22 @@ fileHandler.formatter = logging.Formatter('%(asctime)s|%(levelname)-7.7s %(messa fileHandler.level = logging.DEBUG Logger.addHandler(fileHandler) + +def which(program): # Test if command exists + def is_exe(fpath): + return os.path.isfile(fpath) and os.access(fpath, os.X_OK) + fpath, fname = os.path.split(program) + if fpath: + if is_exe(program): + return program + else: + for path in os.environ["PATH"].split(os.pathsep): + path = path.strip('"') + exe_file = os.path.join(path, program) + if is_exe(exe_file): + return exe_file + return None + def category_search(inputDirectory, inputCategory, root): categorySearch = os.path.split(os.path.normpath(inputDirectory)) # Test for blackhole sub-directory if categorySearch[1] == inputName: @@ -124,6 +140,12 @@ def unpack(dirpath, file, outputDestination): ".txz": ["tar", "--xz xf"], ".7z": ["7zr", "x"], } + for cmd in required_cmds: + if not which(cmd): + for k,v in EXTRACT_COMMANDS.items(): + if cmd in v[0]: + Logger.debug("Extraction command %s not found, disabling support for %s", cmd, k) + del EXTRACT_COMMANDS[k] else: Logger.error("Cant determine host OS while extracting, Exiting") From 6cebf4a466a09685601b30d4e81695c4c0044a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 19:08:39 +0100 Subject: [PATCH 08/10] cleaner console --- TorrentToMedia.py | 124 ++++++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 59 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 7a80924c..803322ec 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -42,37 +42,37 @@ def which(program): # Test if command exists def category_search(inputDirectory, inputCategory, root): categorySearch = os.path.split(os.path.normpath(inputDirectory)) # Test for blackhole sub-directory if categorySearch[1] == inputName: - Logger.info("Files appear to be in their own directory") + Logger.info("CATEGORY SEARCH: Files appear to be in their own directory") categorySearch2 = os.path.split(os.path.normpath(categorySearch[0])) if categorySearch2[1] == movieCategory or categorySearch2[1] == tvCategory: if not inputCategory: - Logger.info("Determined Category to be: %s", categorySearch2[1]) + Logger.info("CATEGORY SEARCH: Determined Category to be: %s", categorySearch2[1]) inputCategory = categorySearch2[1] elif not inputCategory: - Logger.error("Could not identify category from the directory structure. please check downlaoder settings") + Logger.error("CATEGORY SEARCH: Could not identify category from the directory structure. please check downlaoder settings") sys.exit(-1) else: pass elif categorySearch[1] == movieCategory or categorySearch[1] == tvCategory: if os.path.isdir(os.path.join(inputDirectory, inputName)): - Logger.info("Found torrent directory %s in category directory %s", os.path.join(inputDirectory, inputName), inputDirectory) + Logger.info("SEARCH: Found torrent directory %s in category directory %s", os.path.join(inputDirectory, inputName), inputDirectory) inputDirectory = os.path.join(inputDirectory, inputName) else: - Logger.info("The directory passed is the root directory for category %s", categorySearch[1]) - Logger.warn("You should change settings to download torrents to their own directory if possible") - Logger.info("We will try and determine which files to process, individually") + Logger.info("SEARCH: The directory passed is the root directory for category %s", categorySearch[1]) + Logger.warn("SEARCH: You should change settings to download torrents to their own directory if possible") + Logger.info("SEARCH: We will try and determine which files to process, individually") root = 1 if not inputCategory: - Logger.info("Determined Category to be: %s", categorySearch[1]) + Logger.info("SEARCH: Determined Category to be: %s", categorySearch[1]) inputCategory = categorySearch[1] elif not inputCategory: - Logger.error("Could not identify category from the directory structure. please check downlaoder settings") + Logger.error("SEARCH: Could not identify category from the directory structure. please check downlaoder settings") sys.exit(-1) else: - Logger.info("The directory passed does not appear to include a category or the torrent name") - Logger.warn("You should change settings to download torrents to their own directory if possible and include Label/Category directories") - Logger.info("We will try and determine which files to process, individually") + Logger.info("SEARCH: The directory passed does not appear to include a category or the torrent name") + Logger.warn("SEARCH: You should change settings to download torrents to their own directory if possible and include Label/Category directories") + Logger.info("SEARCH: We will try and determine which files to process, individually") root = 1 return inputDirectory, inputCategory, root @@ -89,22 +89,22 @@ def copy_link(source, target, useLink, outputDestination): # Create destination folder if not os.path.exists(outputDestination): try: - Logger.debug("Creating destination folder: %s", outputDestination) + Logger.debug("COPYLINK: Creating destination folder: %s", outputDestination) os.makedirs(outputDestination) except Exception, e: - Logger.error("Not possible to create destination folder: %s", e) + Logger.error("COPYLINK: Not possible to create destination folder: %s", e) return False if useLink: try: - Logger.debug("Linking %s to %s", source, target) + Logger.debug("COPYLINK: Linking %s to %s", source, target) linktastic.link(source, target) except: if os.path.isfile(target): - Logger.info("Something went wrong in linktastic.link, but the destination file was created") + Logger.info("COPYLINK: Something went wrong in linktastic.link, but the destination file was created") else: - Logger.info("Something went wrong in linktastic.link. trying to create a copy") - Logger.debug("Copying %s to %s", source, target) + Logger.info("COPYLINK: Something went wrong in linktastic.link, copying instead") + Logger.debug("COPYLINK: Copying %s to %s", source, target) shutil.copy(source, target) else: Logger.debug("Copying %s to %s", source, target) @@ -114,19 +114,19 @@ def copy_link(source, target, useLink, outputDestination): def unpack(dirpath, file, outputDestination): # Using Windows if os.name == 'nt': - Logger.info("We are using Windows") + Logger.info("EXTRACTOR: We are using Windows") if not os.path.exists(extractionTool): Logger.error("Cant find 7-zip, Exiting") sys.exit(-1) else: - Logger.debug("7-zip found") + Logger.debug("EXTRACTOR: 7-zip found") cmd_7zip = [extractionTool, 'x -y'] # We need to add a check if 7zip is actully present, or exit ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"] EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip) # Using Linux elif os.name == 'posix': - Logger.info("We are using *nix") + Logger.info("EXTRACTOR: We are using *nix") required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] # Need to add a check for which commands that can be utilized in *nix systems EXTRACT_COMMANDS = { ".rar": ["unrar", "x -o+ -y"], @@ -144,10 +144,10 @@ def unpack(dirpath, file, outputDestination): if not which(cmd): for k,v in EXTRACT_COMMANDS.items(): if cmd in v[0]: - Logger.debug("Extraction command %s not found, disabling support for %s", cmd, k) + Logger.debug("EXTRACTOR: Command %s not found, disabling support for %s", cmd, k) del EXTRACT_COMMANDS[k] else: - Logger.error("Cant determine host OS while extracting, Exiting") + Logger.error("EXTRACTOR: Cant determine host OS while extracting, Exiting") ext = os.path.splitext(file) fp = os.path.join(dirpath, file) @@ -159,21 +159,21 @@ def unpack(dirpath, file, outputDestination): if ext[1] in EXTRACT_COMMANDS: cmd = EXTRACT_COMMANDS[ext[1]] else: - Logger.debug("Unknown file type: %s", ext[1]) + Logger.debug("EXTRACTOR: Unknown file type: %s", ext[1]) return False # Create destination folder if not os.path.exists(outputDestination): try: - Logger.debug("Creating destination folder: %s", outputDestination) + Logger.debug("EXTRACTOR: Creating destination folder: %s", outputDestination) os.makedirs(outputDestination) except Exception, e: - Logger.error("Not possible to create destination folder: %s", e) + Logger.error("EXTRACTOR: Not possible to create destination folder: %s", e) return False - Logger.info("Extracting %s to %s", fp, outputDestination) + Logger.info("EXTRACTOR: Extracting %s to %s", fp, outputDestination) # Running extraction process - Logger.debug("Extracting %s %s %s %s", cmd[0], cmd[1], fp, outputDestination) + Logger.debug("EXTRACTOR: Extracting %s %s %s %s", cmd[0], cmd[1], fp, outputDestination) pwd = os.getcwd() # Get our present working directory os.chdir(outputDestination) # Not all unpack commands accept full paths, so just extract into this directory if os.name == 'nt': # Windows needs quotes around directory structure @@ -181,11 +181,11 @@ def unpack(dirpath, file, outputDestination): run = "\"" + cmd[0] + "\" " + cmd[1] + " \"" + fp + "\"" # Windows needs quotes around directories res = call(run) if res == 0: - Logger.info("Extraction was successful for %s to %s", fp, outputDestination) + Logger.info("EXTRACTOR: Extraction was successful for %s to %s", fp, outputDestination) else: - Logger.error("Extraction failed for %s. 7zip result was %s", fp, res) + Logger.error("EXTRACTOR: Extraction failed for %s. 7zip result was %s", fp, res) except: - Logger.error("Extraction failed for %s. Could not call command %s", fp, run) + Logger.error("EXTRACTOR: Extraction failed for %s. Could not call command %s", fp, run) else: try: if cmd[1] == "": # If calling unzip, we dont want to pass the "" @@ -193,28 +193,32 @@ def unpack(dirpath, file, outputDestination): else: res = call([cmd[0], cmd[1], fp]) if res == 0: - Logger.info("Extraction was successful for %s to %s", fp, outputDestination) + Logger.info("EXTRACTOR: Extraction was successful for %s to %s", fp, outputDestination) else: - Logger.error("Extraction failed for %s. 7zip result was %s", fp, res) + Logger.error("EXTRACTOR: Extraction failed for %s. 7zip result was %s", fp, res) except: - Logger.error("Extraction failed for %s. Could not call command %s %s %s %s", fp, cmd[0], cmd[1], fp) + Logger.error("EXTRACTOR: Extraction failed for %s. Could not call command %s %s %s %s", fp, cmd[0], cmd[1], fp) os.chdir(pwd) # Go back to our Original Working Directory return True def flatten(outputDestination): - Logger.info("Flattening directory: %s", outputDestination) + Logger.info("FLATTEN: Flattening directory: %s", outputDestination) for dirpath, dirnames, filenames in os.walk(outputDestination): #flatten out the directory to make postprocessing easier if dirpath == outputDestination: continue # No need to try and move files in the root destination directory for filename in filenames: - try: - shutil.move(os.path.join(dirpath, filename), outputDestination) - except OSError: - Logger.info("Could not flatten %s", os.path.join(dirpath, filename)) + source = os.path.join(dirpath, filename) + if not os.path.exists(source): + try: + shutil.move(source, outputDestination) + except OSError: + Logger.info("FLATTEN: Could not flatten %s", source) + else: + Logger.info("FLATTEN: Could not flatten %s", source) removeEmptyFolders(outputDestination) # Cleanup empty directories def removeEmptyFolders(path): - Logger.info("Removing empty folders in: %s", path) + Logger.info("REMOVER: Removing empty folders in: %s", path) if not os.path.isdir(path): return @@ -229,18 +233,20 @@ def removeEmptyFolders(path): # If folder empty, delete it files = os.listdir(path) if len(files) == 0: - Logger.debug("Removing empty folder: %s", path) + Logger.debug("REMOVER: Removing empty folder: %s", path) os.rmdir(path) Logger.info("TorrentToMedia %s", VERSION) config = ConfigParser.ConfigParser() configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") + +### TORREN TO MEDIA ### if not os.path.isfile(configFilename): Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?") sys.exit(-1) else: - Logger.info("Loading config from %s", configFilename) + Logger.info("MAIN: Loading config from %s", configFilename) config.read(configFilename) if len(sys.argv) == 3: @@ -248,7 +254,7 @@ else: # In short pass "/path/to/downloaded/torrent/ name" to TorrentToMedia.py, eg >>>> TorrentToMedia.py /Downloaded/MovieName.2013.BluRay.1080p.x264-10bit.DTS MovieName.2013.BluRay.1080p.x264-10bit.DTS <<<< inputDirectory = os.path.normpath(sys.argv[1]) inputName = sys.argv[2] - Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName) + Logger.debug("MAIN: Received Directory: %s | Name: %s", inputDirectory, inputName) # Sick-Beard tvCategory = config.get("SickBeard", "category") @@ -275,14 +281,14 @@ else: elif inputCategory == tvCategory: outputDestination = os.path.normpath(os.path.join(tvDestination, inputName)) else: - Logger.error("Category of %s does not match either %s or %s: Exiting", inputCategory, movieCategory, tvCategory) + Logger.error("MAIN: Category of %s does not match either %s or %s: Exiting", inputCategory, movieCategory, tvCategory) sys.exit(-1) - Logger.debug("Scanning files in directory: %s", inputDirectory) + Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory) for dirpath, dirnames, filenames in os.walk(inputDirectory): for file in filenames: if root == 1: - Logger.debug("Looking for %s in filename", inputName) + Logger.debug("MAIN: Looking for %s in filename", inputName) if (inputName in file) or (file in inputName): pass # This file does match the Torrent name else: @@ -291,32 +297,32 @@ else: fileExtention = os.path.splitext(file)[1] if fileExtention in mediaContainer: # If the file is a video file if is_sample(filePath, inputName): # Ignore samples - Logger.info("File %s is a sample file. Ignoring", filePath) + Logger.info("MAIN: Ignoring %s sample file. Ignoring", filePath) continue else: video = video + 1 source = filePath target = os.path.join(outputDestination, file) - Logger.info("Found video file %s", file) + Logger.info("MAIN: Found video file %s in %", fileExtention, filePath) state = copy_link(source, target, useLink, outputDestination) if state == False: - Logger.info("Failed to link file %s", file) + Logger.info("MAIN: Failed to link file %s", file) elif fileExtention in metaContainer: source = filePath target = os.path.join(outputDestination, file) - Logger.info("Found metadata file %s.", file) + Logger.info("MAIN: Found metadata file %s for file %s", fileExtention, filePath) state = copy_link(source, target, useLink, outputDestination) if state == False: - Logger.info("Failed to link file %s", file) + Logger.info("MAIN: Failed to link file %s", file) elif fileExtention in compressedContainer: - Logger.info("Found compressed archive %s", file) + Logger.info("MAIN: Found compressed archive %s for file %s", fileExtention, filePath) source = filePath target = os.path.join(outputDestination, file) state = unpack(dirpath, file, outputDestination) if state == False: - Logger.info("Failed to unpack file %s", file) + Logger.info("MAIN: Failed to unpack file %s", file) else: - Logger.info("Unknown file type %s for file %s. Ignoring", fileExtention, filePath) + Logger.info("MAIN: Ignoring unknown filetype %s for file %s", fileExtention, filePath) continue flatten(outputDestination) @@ -331,17 +337,17 @@ else: status = 0 if status == 0: - Logger.info("Successful run") + Logger.info("MAIN: Successful run") # Now we pass off to CouchPotato or Sick-Beard # Still need to figure out how to log this output if inputCategory == movieCategory: - Logger.info("Calling postprocessing script for CouchPotatoServer") + Logger.info("MAIN: Calling postprocessing script for CouchPotatoServer") autoProcessMovie.process(outputDestination, inputName, status) elif inputCategory == tvCategory: - Logger.info("Calling postprocessing script for Sick-Beard") + Logger.info("MAIN: Calling postprocessing script for Sick-Beard") autoProcessTV.processEpisode(outputDestination, inputName, status) else: - Logger.info("Something failed! :(") + Logger.info("MAIN: Something failed! :(") else: - Logger.error("There was a problem loading variables: Exiting") + Logger.error("MAIN: There was a problem loading variables: Exiting") sys.exit(-1) \ No newline at end of file From 4bde49b993c310fb56346a7aa797148a684342e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 19:09:34 +0100 Subject: [PATCH 09/10] missed a few --- TorrentToMedia.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 803322ec..945d7afe 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -42,14 +42,14 @@ def which(program): # Test if command exists def category_search(inputDirectory, inputCategory, root): categorySearch = os.path.split(os.path.normpath(inputDirectory)) # Test for blackhole sub-directory if categorySearch[1] == inputName: - Logger.info("CATEGORY SEARCH: Files appear to be in their own directory") + Logger.info("SEARCH: Files appear to be in their own directory") categorySearch2 = os.path.split(os.path.normpath(categorySearch[0])) if categorySearch2[1] == movieCategory or categorySearch2[1] == tvCategory: if not inputCategory: - Logger.info("CATEGORY SEARCH: Determined Category to be: %s", categorySearch2[1]) + Logger.info("SEARCH: Determined Category to be: %s", categorySearch2[1]) inputCategory = categorySearch2[1] elif not inputCategory: - Logger.error("CATEGORY SEARCH: Could not identify category from the directory structure. please check downlaoder settings") + Logger.error("SEARCH: Could not identify category from the directory structure. please check downlaoder settings") sys.exit(-1) else: pass From b9e9f9e2254e827658077be387803302f7b065e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20K=C3=A5berg?= Date: Mon, 18 Feb 2013 19:44:10 +0100 Subject: [PATCH 10/10] alternative transmission support --- TorrentToMedia.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 945d7afe..02693885 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -249,11 +249,16 @@ else: Logger.info("MAIN: Loading config from %s", configFilename) config.read(configFilename) - if len(sys.argv) == 3: - # We will pass in %D, %N from uTorrent, or %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission (Transmission needs additional script, see TorrentToMedia.sh and TorrentToMedia.bat + if len(sys.argv) == 3 or os.getenv('TR_TORRENT_DIR') != '': + # We will pass in %D, %N from uTorrent, or %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission # In short pass "/path/to/downloaded/torrent/ name" to TorrentToMedia.py, eg >>>> TorrentToMedia.py /Downloaded/MovieName.2013.BluRay.1080p.x264-10bit.DTS MovieName.2013.BluRay.1080p.x264-10bit.DTS <<<< - inputDirectory = os.path.normpath(sys.argv[1]) - inputName = sys.argv[2] + if os.getenv('TR_TORRENT_DIR') != '': + inputDirectory = os.path.normpath(os.getenv('TR_TORRENT_DIR')) + inputName = os.getenv('TR_TORRENT_NAME') + else: + inputDirectory = os.path.normpath(sys.argv[1]) + inputName = sys.argv[2] + inputCategory = '' # We dont have a category yet Logger.debug("MAIN: Received Directory: %s | Name: %s", inputDirectory, inputName) # Sick-Beard @@ -273,7 +278,6 @@ else: root = int(0) video = int(0) video2 = int(0) - inputCategory = '' # We dont have a category yet inputDirectory, inputCategory, root = category_search(inputDirectory, inputCategory, root) # Confirm the catgeogy by parsing directory structure if inputCategory == movieCategory: