cleaner console

This commit is contained in:
Joel Kåberg 2013-02-18 19:08:39 +01:00
commit 6cebf4a466

View file

@ -42,37 +42,37 @@ def which(program): # Test if command exists
def category_search(inputDirectory, inputCategory, root): 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: 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])) categorySearch2 = os.path.split(os.path.normpath(categorySearch[0]))
if categorySearch2[1] == movieCategory or categorySearch2[1] == tvCategory: if categorySearch2[1] == movieCategory or categorySearch2[1] == tvCategory:
if not inputCategory: 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] inputCategory = categorySearch2[1]
elif not inputCategory: 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) sys.exit(-1)
else: else:
pass pass
elif categorySearch[1] == movieCategory or categorySearch[1] == tvCategory: elif categorySearch[1] == movieCategory or categorySearch[1] == tvCategory:
if os.path.isdir(os.path.join(inputDirectory, inputName)): 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) inputDirectory = os.path.join(inputDirectory, inputName)
else: else:
Logger.info("The directory passed is the root directory for category %s", categorySearch[1]) Logger.info("SEARCH: 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.warn("SEARCH: 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: We will try and determine which files to process, individually")
root = 1 root = 1
if not inputCategory: 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] inputCategory = categorySearch[1]
elif not inputCategory: 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) sys.exit(-1)
else: else:
Logger.info("The directory passed does not appear to include a category or the torrent name") Logger.info("SEARCH: 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("SEARCH: 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: We will try and determine which files to process, individually")
root = 1 root = 1
return inputDirectory, inputCategory, root return inputDirectory, inputCategory, root
@ -89,22 +89,22 @@ 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):
try: try:
Logger.debug("Creating destination folder: %s", outputDestination) Logger.debug("COPYLINK: Creating destination folder: %s", outputDestination)
os.makedirs(outputDestination) os.makedirs(outputDestination)
except Exception, e: 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 return False
if useLink: if useLink:
try: try:
Logger.debug("Linking %s to %s", source, target) Logger.debug("COPYLINK: Linking %s to %s", source, target)
linktastic.link(source, target) linktastic.link(source, target)
except: except:
if os.path.isfile(target): 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: else:
Logger.info("Something went wrong in linktastic.link. trying to create a copy") Logger.info("COPYLINK: Something went wrong in linktastic.link, copying instead")
Logger.debug("Copying %s to %s", source, target) Logger.debug("COPYLINK: Copying %s to %s", source, target)
shutil.copy(source, target) shutil.copy(source, target)
else: else:
Logger.debug("Copying %s to %s", source, target) Logger.debug("Copying %s to %s", source, target)
@ -114,19 +114,19 @@ def copy_link(source, target, useLink, outputDestination):
def unpack(dirpath, file, outputDestination): def unpack(dirpath, file, outputDestination):
# Using Windows # Using Windows
if os.name == 'nt': if os.name == 'nt':
Logger.info("We are using Windows") Logger.info("EXTRACTOR: We are using Windows")
if not os.path.exists(extractionTool): if not os.path.exists(extractionTool):
Logger.error("Cant find 7-zip, Exiting") Logger.error("Cant find 7-zip, Exiting")
sys.exit(-1) sys.exit(-1)
else: 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 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"] 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("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 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 = { EXTRACT_COMMANDS = {
".rar": ["unrar", "x -o+ -y"], ".rar": ["unrar", "x -o+ -y"],
@ -144,10 +144,10 @@ def unpack(dirpath, file, outputDestination):
if not which(cmd): if not which(cmd):
for k,v in EXTRACT_COMMANDS.items(): for k,v in EXTRACT_COMMANDS.items():
if cmd in v[0]: 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] del EXTRACT_COMMANDS[k]
else: 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) ext = os.path.splitext(file)
fp = os.path.join(dirpath, file) fp = os.path.join(dirpath, file)
@ -159,21 +159,21 @@ def unpack(dirpath, file, outputDestination):
if ext[1] in EXTRACT_COMMANDS: if ext[1] in EXTRACT_COMMANDS:
cmd = EXTRACT_COMMANDS[ext[1]] cmd = EXTRACT_COMMANDS[ext[1]]
else: else:
Logger.debug("Unknown file type: %s", ext[1]) Logger.debug("EXTRACTOR: Unknown file type: %s", ext[1])
return False return False
# Create destination folder # Create destination folder
if not os.path.exists(outputDestination): if not os.path.exists(outputDestination):
try: try:
Logger.debug("Creating destination folder: %s", outputDestination) Logger.debug("EXTRACTOR: Creating destination folder: %s", outputDestination)
os.makedirs(outputDestination) os.makedirs(outputDestination)
except Exception, e: 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 return False
Logger.info("Extracting %s to %s", fp, outputDestination) Logger.info("EXTRACTOR: 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) Logger.debug("EXTRACTOR: Extracting %s %s %s %s", cmd[0], cmd[1], fp, outputDestination)
pwd = os.getcwd() # Get our present working directory 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 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 run = "\"" + cmd[0] + "\" " + cmd[1] + " \"" + fp + "\"" # Windows needs quotes around directories
res = call(run) res = call(run)
if res == 0: 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: 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: 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: else:
try: 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 ""
@ -193,28 +193,32 @@ def unpack(dirpath, file, outputDestination):
else: else:
res = call([cmd[0], cmd[1], fp]) res = call([cmd[0], cmd[1], fp])
if res == 0: 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: 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: 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 os.chdir(pwd) # Go back to our Original Working Directory
return True return True
def flatten(outputDestination): 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 for dirpath, dirnames, filenames in os.walk(outputDestination): #flatten out the directory to make postprocessing easier
if dirpath == outputDestination: 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: for filename in filenames:
source = os.path.join(dirpath, filename)
if not os.path.exists(source):
try: try:
shutil.move(os.path.join(dirpath, filename), outputDestination) shutil.move(source, outputDestination)
except OSError: except OSError:
Logger.info("Could not flatten %s", os.path.join(dirpath, filename)) Logger.info("FLATTEN: Could not flatten %s", source)
else:
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("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):
return return
@ -229,18 +233,20 @@ def removeEmptyFolders(path):
# If folder empty, delete it # If folder empty, delete it
files = os.listdir(path) files = os.listdir(path)
if len(files) == 0: if len(files) == 0:
Logger.debug("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")
### TORREN TO MEDIA ###
if not os.path.isfile(configFilename): if not os.path.isfile(configFilename):
Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?") Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?")
sys.exit(-1) sys.exit(-1)
else: else:
Logger.info("Loading config from %s", configFilename) Logger.info("MAIN: Loading config from %s", configFilename)
config.read(configFilename) config.read(configFilename)
if len(sys.argv) == 3: 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 <<<< # 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]) inputDirectory = os.path.normpath(sys.argv[1])
inputName = sys.argv[2] 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 # Sick-Beard
tvCategory = config.get("SickBeard", "category") tvCategory = config.get("SickBeard", "category")
@ -275,14 +281,14 @@ else:
elif inputCategory == tvCategory: elif inputCategory == tvCategory:
outputDestination = os.path.normpath(os.path.join(tvDestination, inputName)) outputDestination = os.path.normpath(os.path.join(tvDestination, inputName))
else: 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) 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 dirpath, dirnames, filenames in os.walk(inputDirectory):
for file in filenames: for file in filenames:
if root == 1: 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): 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: else:
@ -291,32 +297,32 @@ else:
fileExtention = os.path.splitext(file)[1] 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 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 continue
else: else:
video = video + 1 video = video + 1
source = filePath source = filePath
target = os.path.join(outputDestination, file) 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) state = copy_link(source, target, useLink, outputDestination)
if state == False: if state == False:
Logger.info("Failed to link file %s", file) Logger.info("MAIN: Failed to link file %s", file)
elif fileExtention in metaContainer: elif fileExtention in metaContainer:
source = filePath source = filePath
target = os.path.join(outputDestination, file) 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) state = copy_link(source, target, useLink, outputDestination)
if state == False: if state == False:
Logger.info("Failed to link file %s", file) Logger.info("MAIN: Failed to link file %s", file)
elif fileExtention in compressedContainer: 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 source = filePath
target = os.path.join(outputDestination, file) target = os.path.join(outputDestination, file)
state = unpack(dirpath, file, outputDestination) state = unpack(dirpath, file, outputDestination)
if state == False: if state == False:
Logger.info("Failed to unpack file %s", file) Logger.info("MAIN: Failed to unpack file %s", file)
else: 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 continue
flatten(outputDestination) flatten(outputDestination)
@ -331,17 +337,17 @@ else:
status = 0 status = 0
if status == 0: if status == 0:
Logger.info("Successful run") Logger.info("MAIN: Successful run")
# Now we pass off to CouchPotato or Sick-Beard # Now we pass off to CouchPotato or Sick-Beard
# Still need to figure out how to log this output # Still need to figure out how to log this output
if inputCategory == movieCategory: if inputCategory == movieCategory:
Logger.info("Calling postprocessing script for CouchPotatoServer") Logger.info("MAIN: Calling postprocessing script for CouchPotatoServer")
autoProcessMovie.process(outputDestination, inputName, status) autoProcessMovie.process(outputDestination, inputName, status)
elif inputCategory == tvCategory: 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) autoProcessTV.processEpisode(outputDestination, inputName, status)
else: else:
Logger.info("Something failed! :(") Logger.info("MAIN: Something failed! :(")
else: else:
Logger.error("There was a problem loading variables: Exiting") Logger.error("MAIN: There was a problem loading variables: Exiting")
sys.exit(-1) sys.exit(-1)