Removed extraneous loop branching, fixed TorrentToMedia for PEP standards

This commit is contained in:
Berkona 2013-02-26 18:33:08 -05:00
commit 054e9bf59c

View file

@ -23,12 +23,8 @@ nzbtomedia_configure_logging(os.path.dirname(sys.argv[0]))
Logger = logging.getLogger(__name__) Logger = logging.getLogger(__name__)
def category_search_recurs(inputDirectory, inputName, root, categories):
pass
def category_search(inputDirectory, inputName, inputCategory, root, categories): def category_search(inputDirectory, inputName, inputCategory, root, categories):
categorySearch = [os.path.normpath(inputDirectory),""] #initializie categorySearch = [os.path.normpath(inputDirectory), ""] # initializie
notfound = 0 notfound = 0
for x in range(10): # loop up through 10 directories looking for category. for x in range(10): # loop up through 10 directories looking for category.
try: try:
@ -40,17 +36,18 @@ def category_search(inputDirectory, inputName, inputCategory, root, categories):
Logger.warn("SEARCH: You should change settings to download torrents to their own directory if possible") 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") Logger.info("SEARCH: We will try and determine which files to process, individually")
root = 1 root = 1
break #we are done 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. 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: 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.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.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") Logger.info("SEARCH: We will try and determine which files to process, individually")
root = 2 root = 2
break #we are done break # we are done
else: else:
Logger.error("SEARCH: Could not identify Category of Torrent Name in the directory structure. Please check downloader settings. Exiting") 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: if categorySearch2[1] in categories:
Logger.debug("SEARCH: Found Category: %s in directory structure", categorySearch2[1]) Logger.debug("SEARCH: Found Category: %s in directory structure", categorySearch2[1])
if not inputCategory: if not inputCategory:
@ -62,8 +59,8 @@ def category_search(inputDirectory, inputName, inputCategory, root, categories):
inputName = 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) 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 break # we are done
elif categorySearch[1] and not inputName: #assume the the next directory deep is the torrent name. 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: Found torrent directory %s in category directory %s", os.path.join(categorySearch[0], categorySearch[1]), categorySearch[0])
inputName = categorySearch[1] inputName = categorySearch[1]
break # we are done 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. 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.
@ -73,35 +70,35 @@ def category_search(inputDirectory, inputName, inputCategory, root, categories):
elif os.path.isdir(os.path.join(categorySearch[0], inputName)) and inputName: # testing for torrent name in first sub directory 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]) 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 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. inputDirectory = os.path.join(categorySearch[0], inputName) # we only want to search this next dir up.
break #we are done break # we are done
elif inputName: # if these exists, we are ok to proceed, but we are in a root/common directory. 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: 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.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.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") Logger.info("SEARCH: We will try and determine which files to process, individually")
root = 1 root = 1
break #we are done 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. 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: 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.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") Logger.info("SEARCH: We will try and determine which files to process, individually")
root = 2 root = 2
break 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") Logger.info("SEARCH: Files appear to be in their own directory")
if inputCategory: #we are ok to proceed. if inputCategory: # we are ok to proceed.
break # we are done break # we are done
else: else:
Logger.debug("SEARCH: Continuing scan to determin category.") Logger.debug("SEARCH: Continuing scan to determin category.")
categorySearch=categorySearch2 #ready for next loop categorySearch = categorySearch2 # ready for next loop
continue # keep going continue # keep going
else: 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 notfound = 1
break # we are done break # we are done
else: else:
categorySearch=categorySearch2 #ready for next loop categorySearch = categorySearch2 # ready for next loop
continue # keep going continue # keep going
if notfound == 1: if notfound == 1:
@ -117,12 +114,13 @@ def category_search(inputDirectory, inputName, inputCategory, root, categories):
Logger.warn("SEARCH: You should change settings to download torrents to their own directory if possible") 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") Logger.info("SEARCH: We will try and determine which files to process, individually")
root = 2 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") 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! sys.exit(-1) # Oh yeah.... WE ARE DONE!
return inputDirectory, inputName, inputCategory, root return inputDirectory, inputName, inputCategory, root
def is_sample(filePath, inputName): def is_sample(filePath, inputName):
# 200 MB in bytes # 200 MB in bytes
SIZE_CUTOFF = 200 * 1024 * 1024 SIZE_CUTOFF = 200 * 1024 * 1024
@ -132,6 +130,7 @@ def is_sample(filePath, inputName):
else: else:
return False return False
def copy_link(source, target, useLink, outputDestination): def copy_link(source, target, useLink, outputDestination):
# Create destination folder # Create destination folder
if not os.path.exists(outputDestination): if not os.path.exists(outputDestination):
@ -158,19 +157,18 @@ def copy_link(source, target, useLink, outputDestination):
shutil.copy(source, target) shutil.copy(source, target)
return True return True
def unpack(dirpath, file, destination): def unpack(dirpath, file, destination):
# Using Windows # Using Windows
if os.name == 'nt': if os.name == 'nt':
Logger.info("EXTRACTOR: We are using Windows") Logger.info("EXTRACTOR: We are using Windows")
cmd_7zip = [extractionTool, 'x -y'] 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) EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip)
# Using linux # Using linux
elif os.name == 'posix': elif os.name == 'posix':
Logger.info("EXTRACTOR: We are using *nix") 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.. # Need to add a check for which commands that can be utilized in *nix systems..
else: else:
Logger.error("EXTRACTOR: Unknown OS, exiting") Logger.error("EXTRACTOR: Unknown OS, exiting")
@ -228,6 +226,7 @@ def unpack(dirpath, file, destination):
os.chdir(pwd) # Go back to our Original Working Directory os.chdir(pwd) # Go back to our Original Working Directory
return True return True
def flatten(outputDestination): def flatten(outputDestination):
Logger.info("FLATTEN: 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 for dirpath, dirnames, filenames in os.walk(outputDestination): # Flatten out the directory to make postprocessing easier
@ -242,6 +241,7 @@ def flatten(outputDestination):
Logger.info("FLATTEN: Could not flatten %s", source) Logger.info("FLATTEN: Could not flatten %s", source)
removeEmptyFolders(outputDestination) # Cleanup empty directories removeEmptyFolders(outputDestination) # Cleanup empty directories
def removeEmptyFolders(path): def removeEmptyFolders(path):
Logger.info("REMOVER: Removing empty folders in: %s", path) Logger.info("REMOVER: Removing empty folders in: %s", path)
if not os.path.isdir(path): if not os.path.isdir(path):
@ -261,6 +261,7 @@ def removeEmptyFolders(path):
Logger.debug("REMOVER: Removing empty folder: %s", path) Logger.debug("REMOVER: Removing empty folder: %s", path)
os.rmdir(path) os.rmdir(path)
Logger.info("TorrentToMedia %s", VERSION) Logger.info("TorrentToMedia %s", VERSION)
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg")
@ -303,7 +304,7 @@ mediaContainer = (config.get("Torrent", "mediaExtentions")).split(',')
metaContainer = (config.get("Torrent", "metaExtentions")).split(',') metaContainer = (config.get("Torrent", "metaExtentions")).split(',')
categories = (config.get("Torrent", "categories")).split(',') categories = (config.get("Torrent", "categories")).split(',')
categories.append(movieCategory) 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) root = int(0)
@ -323,23 +324,21 @@ else:
sys.exit(-1) sys.exit(-1)
Logger.debug("MAIN: Scanning files in directory: %s", inputDirectory) 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() now = datetime.datetime.now()
for dirpath, dirnames, filenames in os.walk(inputDirectory): for dirpath, dirnames, filenames in os.walk(inputDirectory):
for file in filenames: for file in filenames:
if root == 1: 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): 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) Logger.debug("Found file %s that matches Torrent Name %s", file, inputName)
else: 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 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))) 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))) 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)): if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)):
@ -391,17 +390,17 @@ for dirpath, dirnames, filenames in os.walk(outputDestination):
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): if is_sample(filePath, inputName):
Logger.info("file %s is a sample file. Removing", filePath) Logger.info("file %s is a sample file. Removing", filePath)
os.unlink(filePath) #remove samples os.unlink(filePath) # remove samples
else: else:
video2 = video2 + 1 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 = 0
status = int(status) #just to be safe. status = int(status) # just to be safe.
if status == 0: if status == 0:
Logger.info("MAIN: Successful run") Logger.info("MAIN: Successful run")
Logger.debug("MAIN: Calling autoProcess script for successful download.") 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.info("MAIN: Failed to extract a packed file.")
Logger.debug("MAIN: Assume this to be password protected file.") Logger.debug("MAIN: Assume this to be password protected file.")
Logger.debug("MAIN: Calling autoProcess script for failed download.") Logger.debug("MAIN: Calling autoProcess script for failed download.")
@ -412,7 +411,7 @@ else:
# Log this output # Log this output
old_stdout = sys.stdout # Still crude, but we wat to capture this for now 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") 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 sys.stdout = log_file
# Hardlink solution with uTorrent # Hardlink solution with uTorrent
@ -432,5 +431,6 @@ elif inputCategory == tvCategory:
if inputHash and useLink: if inputHash and useLink:
Logger.debug("MAIN: We are using hardlinks with uTorrent, calling uTorrent to resume download") Logger.debug("MAIN: We are using hardlinks with uTorrent, calling uTorrent to resume download")
utorrentClass.start(inputHash) utorrentClass.start(inputHash)
sys.stdout = old_stdout sys.stdout = old_stdout
log_file.close() log_file.close()