From 054e9bf59c2e84371c59b24653c91e5454c42bfa Mon Sep 17 00:00:00 2001 From: Berkona Date: Tue, 26 Feb 2013 18:33:08 -0500 Subject: [PATCH] Removed extraneous loop branching, fixed TorrentToMedia for PEP standards --- TorrentToMedia.py | 184 +++++++++++++++++++++++----------------------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 6b9e7f57..aa2a5eaa 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -23,105 +23,103 @@ nzbtomedia_configure_logging(os.path.dirname(sys.argv[0])) Logger = logging.getLogger(__name__) -def category_search_recurs(inputDirectory, inputName, root, categories): - pass - - def category_search(inputDirectory, inputName, inputCategory, root, categories): - categorySearch = [os.path.normpath(inputDirectory),""] #initializie - notfound = 0 - for x in range(10): # loop up through 10 directories looking for category. - try: - categorySearch2 = os.path.split(os.path.normpath(categorySearch[0])) - except: # this might happen when we can't go higher. - if inputCategory and inputName: # if these exists, we are ok to proceed, but assume we are in a root/common directory. - Logger.info("SEARCH: Could not find a Torrent Name or category in the directory structure") - Logger.info("SEARCH: We assume the directory passed is the root directory for your downlaoder") + categorySearch = [os.path.normpath(inputDirectory), ""] # initializie + notfound = 0 + for x in range(10): # loop up through 10 directories looking for category. + try: + categorySearch2 = os.path.split(os.path.normpath(categorySearch[0])) + except: # this might happen when we can't go higher. + if inputCategory and inputName: # if these exists, we are ok to proceed, but assume we are in a root/common directory. + Logger.info("SEARCH: Could not find a Torrent Name or category in the directory structure") + Logger.info("SEARCH: We assume the directory passed is the root directory for your downlaoder") 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 - break #we are done - elif inputCategory: # if this exists, we are ok to proceed, but assume we are in a root/common directory and we have to check file dates. + break # we are done + elif inputCategory: # if this exists, we are ok to proceed, but assume we are in a root/common directory and we have to check file dates. Logger.info("SEARCH: Could not find a Torrent Name or Category in the directory structure") Logger.info("SEARCH: We assume the directory passed is the root directory for your downlaoder") 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 = 2 - break #we are done + break # we are done else: Logger.error("SEARCH: Could not identify Category of Torrent Name in the directory structure. Please check downloader settings. Exiting") - sys.exit(-1) + sys.exit(-1) + if categorySearch2[1] in categories: Logger.debug("SEARCH: Found Category: %s in directory structure", categorySearch2[1]) if not inputCategory: Logger.info("SEARCH: Determined Category to be: %s", categorySearch2[1]) inputCategory = categorySearch2[1] - if inputName and categorySearch[0] != os.path.normpath(inputDirectory): # if we are not in the root directory and we have inputName we can continue. - if ('.cp(tt' in categorySearch[1]) and (not '.cp(tt' in inputName): # if the directory was created by CouchPotato, and this tag is not in Torrent name, we want to add it. + if inputName and categorySearch[0] != os.path.normpath(inputDirectory): # if we are not in the root directory and we have inputName we can continue. + if ('.cp(tt' in categorySearch[1]) and (not '.cp(tt' in inputName): # if the directory was created by CouchPotato, and this tag is not in Torrent name, we want to add it. Logger.info("SEARCH: Changing Torrent Name to %s to preserve imdb id.", categorySearch[1]) inputName = categorySearch[1] - Logger.info("SEARCH: Identified Category: %s and Torrent Name: %s. We are in a unique directory, so we can proceed.", inputCategory, inputName) - break # we are done - elif categorySearch[1] and not inputName: #assume the the next directory deep is the torrent name. - Logger.info("SEARCH: Found torrent directory %s in category directory %s", os.path.join(categorySearch[0],categorySearch[1]), categorySearch[0]) + Logger.info("SEARCH: Identified Category: %s and Torrent Name: %s. We are in a unique directory, so we can proceed.", inputCategory, inputName) + break # we are done + elif categorySearch[1] and not inputName: # assume the the next directory deep is the torrent name. + Logger.info("SEARCH: Found torrent directory %s in category directory %s", os.path.join(categorySearch[0], categorySearch[1]), categorySearch[0]) inputName = categorySearch[1] - break # we are done - elif ('.cp(tt' in categorySearch[1]) and (not '.cp(tt' in inputName): # if the directory was created by CouchPotato, and this tag is not in Torrent name, we want to add it. + break # we are done + elif ('.cp(tt' in categorySearch[1]) and (not '.cp(tt' in inputName): # if the directory was created by CouchPotato, and this tag is not in Torrent name, we want to add it. Logger.info("SEARCH: Changing Torrent Name to %s to preserve imdb id.", categorySearch[1]) inputName = categorySearch[1] - break # we are done - elif os.path.isdir(os.path.join(categorySearch[0], inputName)) and inputName: # testing for torrent name in first sub directory + break # we are done + elif os.path.isdir(os.path.join(categorySearch[0], inputName)) and inputName: # testing for torrent name in first sub directory Logger.info("SEARCH: Found torrent directory %s in category directory %s", os.path.join(categorySearch[0], inputName), categorySearch[0]) - if categorySearch[0] == os.path.normpath(inputDirectory): #only true on first pass, x =0 - inputDirectory = os.path.join(categorySearch[0], inputName) #we only want to search this next dir up. - break #we are done - elif inputName: # if these exists, we are ok to proceed, but we are in a root/common directory. + if categorySearch[0] == os.path.normpath(inputDirectory): #only true on first pass, x =0 + inputDirectory = os.path.join(categorySearch[0], inputName) # we only want to search this next dir up. + break # we are done + elif inputName: # if these exists, we are ok to proceed, but we are in a root/common directory. Logger.info("SEARCH: Could not find a unique torrent folder in the directory structure") Logger.info("SEARCH: The directory passed is the root directory for category %s", categorySearch2[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 - break #we are done - else: #this is a problem! if we don't have Torrent name and are in the root category dir, we can't proceed. + break # we are done + else: # this is a problem! if we don't have Torrent name and are in the root category dir, we can't proceed. Logger.warn("SEARCH: Could not identify a torrent name and the directory passed is common to all downloads 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 = 2 break - elif categorySearch2[1] == inputName and inputName: #we have identified a unique directory. + elif categorySearch2[1] == inputName and inputName: # we have identified a unique directory. Logger.info("SEARCH: Files appear to be in their own directory") - if inputCategory: #we are ok to proceed. - break # we are done + if inputCategory: # we are ok to proceed. + break # we are done else: Logger.debug("SEARCH: Continuing scan to determin category.") - categorySearch=categorySearch2 #ready for next loop - continue # keep going + categorySearch = categorySearch2 # ready for next loop + continue # keep going else: - if x == 9: # This is the last pass in the loop and we didn't find anything. + if x == 9: # This is the last pass in the loop and we didn't find anything. notfound = 1 - break # we are done + break # we are done else: - categorySearch=categorySearch2 #ready for next loop - continue # keep going - + categorySearch = categorySearch2 # ready for next loop + continue # keep going + if notfound == 1: - if inputCategory and inputName: # if these exists, we are ok to proceed, but assume we are in a root/common directory. + if inputCategory and inputName: # if these exists, we are ok to proceed, but assume we are in a root/common directory. Logger.info("SEARCH: Could not find a category in the directory structure") Logger.info("SEARCH: We assume the directory passed is the root directory for your downlaoder") 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 - elif inputCategory: # if this exists, we are ok to proceed, but assume we are in a root/common directory and we have to check file dates. + elif inputCategory: # if this exists, we are ok to proceed, but assume we are in a root/common directory and we have to check file dates. Logger.info("SEARCH: Could not find a Torrent Name or Category in the directory structure") Logger.info("SEARCH: We assume the directory passed is the root directory for your downlaoder") 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 = 2 - if not inputCategory: #we didn't find this after 10 loops. This is a problem. + if not inputCategory: # we didn't find this after 10 loops. This is a problem. Logger.error("SEARCH: Could not identify category and torrent name from the directory structure. Please check downloader settings. Exiting") sys.exit(-1) # Oh yeah.... WE ARE DONE! - return inputDirectory, inputName, inputCategory, root + return inputDirectory, inputName, inputCategory, root + def is_sample(filePath, inputName): # 200 MB in bytes @@ -132,6 +130,7 @@ def is_sample(filePath, inputName): else: return False + def copy_link(source, target, useLink, outputDestination): # Create destination folder if not os.path.exists(outputDestination): @@ -158,19 +157,18 @@ def copy_link(source, target, useLink, outputDestination): shutil.copy(source, target) return True + def unpack(dirpath, file, destination): # Using Windows if os.name == 'nt': Logger.info("EXTRACTOR: We are using Windows") cmd_7zip = [extractionTool, 'x -y'] - ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"] + 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("EXTRACTOR: We are using *nix") - EXTRACT_COMMANDS = {".rar": ["unrar", "e"], ".zip": ["unzip", ""], ".tar.gz": ["tar", "xzf"], ".tgz": ["tar", "xzf"], ".tar.bz2": ["tar", "xjf"], ".tbz": ["tar", "xjf"], ".tar.lzma": ["tar", "--lzma xf"], ".tlz": ["tar", "--lzma xf"], ".txz": ["tar", "--xz xf"], ".7z": ["7zr", "x"],} - + EXTRACT_COMMANDS = {".rar": ["unrar", "e"], ".zip": ["unzip", ""], ".tar.gz": ["tar", "xzf"], ".tgz": ["tar", "xzf"], ".tar.bz2": ["tar", "xjf"], ".tbz": ["tar", "xjf"], ".tar.lzma": ["tar", "--lzma xf"], ".tlz": ["tar", "--lzma xf"], ".txz": ["tar", "--xz xf"], ".7z": ["7zr", "x"], } # Need to add a check for which commands that can be utilized in *nix systems.. else: Logger.error("EXTRACTOR: Unknown OS, exiting") @@ -201,11 +199,11 @@ def unpack(dirpath, file, destination): # Running Logger.debug("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 + 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 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("EXTRACTOR: Extraction was successful for %s to %s", fp, destination) @@ -215,7 +213,7 @@ def unpack(dirpath, file, destination): Logger.error("EXTRACTOR: 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 "" + 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]) @@ -224,15 +222,16 @@ def unpack(dirpath, file, destination): else: Logger.error("EXTRACTOR: Extraction failed for %s. 7zip result was %s", fp, res) except: - 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 + 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("FLATTEN: 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: source = os.path.join(dirpath, filename) target = os.path.join(outputDestination, filename) @@ -240,7 +239,8 @@ def flatten(outputDestination): shutil.move(source, target) except OSError: Logger.info("FLATTEN: Could not flatten %s", source) - removeEmptyFolders(outputDestination) # Cleanup empty directories + removeEmptyFolders(outputDestination) # Cleanup empty directories + def removeEmptyFolders(path): Logger.info("REMOVER: Removing empty folders in: %s", path) @@ -261,6 +261,7 @@ def removeEmptyFolders(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") @@ -277,10 +278,10 @@ config.read(configFilename) clientAgent = config.get("Torrent", "clientAgent") try: - inputDirectory, inputName, inputCategory = parse_args(clientAgent) + inputDirectory, inputName, inputCategory = parse_args(clientAgent) except: - Logger.error("MAIN: There was a problem loading variables: Exiting") - sys.exit(-1) + Logger.error("MAIN: There was a problem loading variables: Exiting") + sys.exit(-1) #### Main routine starts here. @@ -303,16 +304,16 @@ mediaContainer = (config.get("Torrent", "mediaExtentions")).split(',') metaContainer = (config.get("Torrent", "metaExtentions")).split(',') categories = (config.get("Torrent", "categories")).split(',') categories.append(movieCategory) -categories.append(tvCategory) #now have a list of all categories in use. +categories.append(tvCategory) # now have a list of all categories in use. -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) failed_link = int(0) failed_extract = int(0) -inputDirectory, inputName, inputCategory, root = category_search(inputDirectory, inputName, inputCategory, root, categories) # Confirm the category by parsing directory structure +inputDirectory, inputName, inputCategory, root = category_search(inputDirectory, inputName, inputCategory, root, categories) # Confirm the category by parsing directory structure if inputCategory == movieCategory: outputDestination = os.path.normpath(os.path.join(movieDestination, inputName)) elif inputCategory == tvCategory: @@ -323,36 +324,34 @@ else: sys.exit(-1) Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory) -numloop = 0 +if root == 1: + Logger.debug("MAIN: Looking for %s in filename", inputName) +elif root == 2: + Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.") + now = datetime.datetime.now() for dirpath, dirnames, filenames in os.walk(inputDirectory): for file in filenames: if root == 1: - if numloop == 0: # only log this once. - Logger.debug("MAIN: Looking for %s in filename", inputName) - numloop = numloop + 1 if (inputName in file) or (os.path.splitext(file)[0] in inputName): - pass # This file does match the Torrent name + pass # This file does match the Torrent name Logger.debug("Found file %s that matches Torrent Name %s", file, inputName) else: - continue # This file does not match the Torrent name, skip it + continue # This file does not match the Torrent name, skip it if root == 2: - if numloop == 0: # only log this once. - Logger.debug("MAIN: Looking for files with modified/created dates less than 5 minutes old.") - numloop = numloop + 1 mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dirpath, file))) ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(os.path.join(dirpath, file))) if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)): - pass # This file does match the date time criteria + pass # This file does match the date time criteria Logger.debug("Found file %s with date modifed/created less than 5 minutes ago.", file) else: - continue # This file has not been recently moved or created, skip it + continue # This file has not been recently moved or created, 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 + if fileExtention in mediaContainer: # If the file is a video file + if is_sample(filePath, inputName): # Ignore samples Logger.info("MAIN: Ignoring %s sample file. Ignoring", filePath) - continue + continue else: video = video + 1 source = filePath @@ -388,20 +387,20 @@ 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 if is_sample(filePath, inputName): Logger.info("file %s is a sample file. Removing", filePath) - os.unlink(filePath) #remove samples + os.unlink(filePath) # remove samples else: 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) #just to be safe. +status = int(status) # just to be safe. if status == 0: Logger.info("MAIN: Successful run") Logger.debug("MAIN: Calling autoProcess script for successful download.") -elif failed_extract == 1 and failed_link == 0: #failed to extract files only. +elif failed_extract == 1 and failed_link == 0: # failed to extract files only. Logger.info("MAIN: Failed to extract a packed file.") Logger.debug("MAIN: Assume this to be password protected file.") Logger.debug("MAIN: Calling autoProcess script for failed download.") @@ -412,25 +411,26 @@ else: # Log this output old_stdout = sys.stdout # Still crude, but we wat to capture this for now logFile = os.path.join(os.path.dirname(sys.argv[0]), "postprocess.log") -log_file = open(logFile,"a+") +log_file = open(logFile, "a+") sys.stdout = log_file # Hardlink solution with uTorrent -if inputHash and useLink: +if inputHash and useLink: Logger.debug("MAIN: We are using hardlinks with uTorrent, calling uTorrent to pause download") utorrentClass.stop(inputHash) - time.sleep(5) # Give uTorrent some time to catch up with the change + time.sleep(5) # Give uTorrent some time to catch up with the change if inputCategory == movieCategory: Logger.info("MAIN: Calling postprocessing script for CouchPotatoServer") autoProcessMovie.process(outputDestination, inputName, status) elif inputCategory == tvCategory: - Logger.info("MAIN: Calling postprocessing script for Sick-Beard") - autoProcessTV.processEpisode(outputDestination, inputName, status) + Logger.info("MAIN: Calling postprocessing script for Sick-Beard") + autoProcessTV.processEpisode(outputDestination, inputName, status) # Hardlink solution with uTorrent if inputHash and useLink: Logger.debug("MAIN: We are using hardlinks with uTorrent, calling uTorrent to resume download") utorrentClass.start(inputHash) + sys.stdout = old_stdout -log_file.close() \ No newline at end of file +log_file.close()