From c2b7a269210527181bada0a3c49e16fb1cd28465 Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Mon, 11 Feb 2013 18:58:22 -0800 Subject: [PATCH] Fix Torrent handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix logging move def removeEmptyFolders to top Change extension matching to use simple tools Change os.linkĀ  --- TorrentToMedia.py | 88 +++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index ece4176b..d7dac065 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -2,13 +2,31 @@ import autoProcessMovie import autoProcessTV -import sys, os, ConfigParser, shutil, re +import sys, os, ConfigParser, shutil from subprocess import call +def removeEmptyFolders(path): + if not os.path.isdir(path): + return + + # remove empty subfolders + files = os.listdir(path) + if len(files): + for f in files: + fullpath = os.path.join(path, f) + if os.path.isdir(fullpath): + removeEmptyFolders(fullpath) + + # if folder empty, delete it + files = os.listdir(path) + if len(files) == 0: + print "INFO: Removing empty folder: %s" % (path) + os.rmdir(path) + old_stdout = sys.stdout #backup the default stdout log_file = open(os.path.join(os.path.dirname(sys.argv[0]), "postprocess.log"),"a+") sys.stdout = log_file #create a local log file, and direct all "print" to the log. -print "INFO: TorrentToMedia V4.1" +print "INFO: TorrentToMedia V4.2" if len(sys.argv) == 4: ##You can use the following parameters (UTORRENT): ## @@ -46,7 +64,7 @@ if len(sys.argv) == 4: 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). - print ("Error: The number of arguments passed is %s. Unable to determin the arguments to use; Exiting" , len(sys.argv)) + print "Error: The number of arguments passed is %s. Unable to determin the arguments to use; Exiting" % (len(sys.argv)) sys.exit(-1) else: @@ -75,7 +93,7 @@ packed = 0 config = ConfigParser.ConfigParser() configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") -print ("INFO: Loading config from %s", configFilename) +print "INFO: Loading config from ", configFilename if not os.path.isfile(configFilename): print "ERROR: You need an autoProcessMedia.cfg file - did you rename and edit the .sample?" @@ -95,28 +113,33 @@ if Category == Movie_Cat: elif Category == TV_Cat: destination = os.path.join(TV_dest, Name) else: - print ("INFO: Not assigned a label of either %s or %s. Exiting", Movie_Cat, TV_Cat) + print "INFO: Not assigned a label of either %s or %s: Exiting" %(Movie_Cat, TV_Cat) sys.exit(-1) -test = re.compile('^(.*)\.((zip|rar|7z|gz|bz|tar|arj)|(r[0-9]{1,3})|([0-9]{1,3}))$', re.IGNORECASE|re.UNICODE); -test2 = re.compile('^(.*)\.(mkv|avi|divx|xvid|mov|wmv)$', re.IGNORECASE|re.UNICODE); -if test.match(Directory): +test = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj'] +test2 = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv'] +f = [filenames for dirpath, dirnames, filenames in os.walk(Directory)] +ext = [os.path.splitext(file)[1] for file in f[1]] +if set(ext).intersection(set(test)): print "INFO: Found compressed archives, extracting" packed = 1 ## Check that files actully is .mkv / .avi etc, and not packed files or anything else -elif test2.match(Directory): +elif set(ext).intersection(set(test2)): print "INFO: Found media files, moving" else: print "INFO: Didn't find any compressed archives or media files to process, exiting" sys.exit(-1) if useLink == 0 and packed == 0: ## copy - print ("INFO: Copying all files from %s to %s", Directory, destination) + print "INFO: Copying all files from %s to %s." % (Directory, destination) shutil.copytree(Directory, destination) elif useLink == 1 and packed == 0: ## hardlink - print ("INFO: Creating hard link from %s to %s", Directory, destination) - shutil.copytree(Directory, destination, copy_function=os.link) + print "INFO: Creating hard link from %s to %s." % (Directory, destination) + os.mkdir(destination) + for dirpath, dirnames, filenames in os.walk(Directory): + for file in filenames: + os.link(os.path.join(dirpath, file), os.path.join(destination, file)) elif packed == 1: ## unpack ## Using Windows? @@ -159,7 +182,7 @@ elif packed == 1: ## unpack if ext[1] in EXTRACT_COMMANDS: cmd = EXTRACT_COMMANDS[ext[1]] else: - print("ERROR: Unknown file type: %s", ext[1]) + print "ERROR: Unknown file type: %s" % (ext[1]) continue ## Create destination folder @@ -167,14 +190,13 @@ elif packed == 1: ## unpack try: os.makedirs(destination) except Exception, e: - print("ERROR: Not possible to create destination folder: %s", e) + print "ERROR: Not possible to create destination folder: %s" % (e) continue - print("INFO: Extracting to %s", destination) - + print"INFO: Extracting to %s" % (destination) ## Running.. - print("INFO: Extracting %s %s %s %s", cmd[0], cmd[1], fp, destination) + print "INFO: Extracting %s %s %s %s" % (cmd[0], cmd[1], fp, destination) pwd = os.getcwd() # Get our Present Working Directory os.chdir(destination) #not all unpack commands accept full paths, so just extract into this directory. if os.name == 'nt': #Windows needs quotes around directory structure @@ -183,11 +205,11 @@ elif packed == 1: ## unpack res = call(run) if res == 0: status = 0 - print ("INFO: Extraction was successful for %s to %s", fp, destination) + print "INFO: Extraction was successful for %s to %s" % (fp, destination) else: - print("ERROR: Extraction failed for %s. 7zip result was %s", fp, res) + print "ERROR: Extraction failed for %s. 7zip result was %s" % (fp, res) except: - print ("ERROR: Extraction failed for %s. Could not call command %s %s", fp, run) + print "ERROR: Extraction failed for %s. Could not call command %s %s" % (fp, run) else: try: if cmd[1] == "": #if calling unzip, we dont want to pass the "" @@ -196,11 +218,11 @@ elif packed == 1: ## unpack res = call([cmd[0], cmd[1], fp]) if res == 0: status = 0 - print ("INFO: Extraction was successful for %s to %s", fp, destination) + print "INFO: Extraction was successful for %s to %s" % (fp, destination) else: - print("ERROR: Extraction failed for %s. 7zip result was %s", fp, res) + print "ERROR: Extraction failed for %s. 7zip result was %s" % (fp, res) except: - print ("ERROR: Extraction failed for %s. Could not call command %s %s %s %s", fp, cmd[0], cmd[1], fp) + print "ERROR: 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 for dirpath, dirnames, filenames in os.walk(destination): #flatten out the directory to make postprocessing easier. @@ -210,7 +232,7 @@ for dirpath, dirnames, filenames in os.walk(destination): #flatten out the direc try: shutil.move(os.path.join(dirpath, filename), destination) except OSError: - print ("INFO: Could not flatten %s ", os.path.join(dirpath, filename)) + print "INFO: Could not flatten %s " % (os.path.join(dirpath, filename)) removeEmptyFolders(destination) #cleanup empty directories. status = int(status) @@ -222,21 +244,3 @@ elif Category == TV_Cat: sys.stdout = old_stdout #reset our stdout log_file.close() #close the log - -def removeEmptyFolders(path): - if not os.path.isdir(path): - return - - # remove empty subfolders - files = os.listdir(path) - if len(files): - for f in files: - fullpath = os.path.join(path, f) - if os.path.isdir(fullpath): - removeEmptyFolders(fullpath) - - # if folder empty, delete it - files = os.listdir(path) - if len(files) == 0: - print ("INFO: Removing empty folder: %s", path) - os.rmdir(path)