a few more fixes

This commit is contained in:
Joel Kåberg 2013-02-18 16:09:17 +01:00
commit c1906a6b16

View file

@ -24,7 +24,7 @@ fileHandler.level = logging.DEBUG
Logger.addHandler(fileHandler) Logger.addHandler(fileHandler)
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("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]))
@ -33,7 +33,7 @@ def category_search(inputDirectory, inputCategory, root):
Logger.info("Determined Category to be: %s", categorySearch2[1]) Logger.info("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("Could not identify category from the directory structure. please check downlaoder settings")
sys.exit(-1) sys.exit(-1)
else: else:
pass pass
@ -51,11 +51,11 @@ def category_search(inputDirectory, inputCategory, root):
Logger.info("Determined Category to be: %s", categorySearch[1]) Logger.info("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("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("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") Logger.info("We will try and determine which files to process, individually")
root = 1 root = 1
return inputDirectory, inputCategory, root return inputDirectory, inputCategory, root
@ -63,14 +63,14 @@ def category_search(inputDirectory, 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
# 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): if ('sample' in filePath.lower()) and (not 'sample' in inputName) and (os.path.getsize(filePath) < SIZE_CUTOFF):
return True return True
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):
try: try:
Logger.debug("Creating destination folder: %s", outputDestination) Logger.debug("Creating destination folder: %s", outputDestination)
@ -96,16 +96,22 @@ def copy_link(source, target, useLink, outputDestination):
return True return True
def unpack(dirpath, file, outputDestination): def unpack(dirpath, file, outputDestination):
## Using Windows # Using Windows
if os.name == 'nt': if os.name == 'nt':
cmd_7zip = [extractionTool, 'x -y'] ## We need to add a check if 7zip is actully present, or exit Logger.info("We are using Windows")
ext_7zip = [".rar",".zip",".tar.gz","tgz",".tar.bz2",".tbz",".tar.lzma",".tlz",".7z",".xz"] if not os.path.exists(extractionTool):
EXTRACT_COMMANDS = dict.fromkeys(ext_7zip, cmd_7zip) Logger.error("Cant find 7-zip, Exiting")
Logger.info("We are using Windows") 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': 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 = { EXTRACT_COMMANDS = {
".rar": ["unrar", "x -o+ -y"], ".rar": ["unrar", "x -o+ -y"],
".zip": ["unzip", ""], ".zip": ["unzip", ""],
@ -118,14 +124,13 @@ def unpack(dirpath, file, outputDestination):
".txz": ["tar", "--xz xf"], ".txz": ["tar", "--xz xf"],
".7z": ["7zr", "x"], ".7z": ["7zr", "x"],
} }
Logger.info("We are using *nix")
else: else:
Logger.error("Cant determine host OS while extracting, Exiting") Logger.error("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)
if ext[1] in (".gz", ".bz2", ".lzma"): 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": if os.path.splitext(ext[0])[1] == ".tar":
cmd = EXTRACT_COMMANDS[".tar" + ext[1]] cmd = EXTRACT_COMMANDS[".tar" + ext[1]]
else: else:
@ -135,7 +140,7 @@ def unpack(dirpath, file, outputDestination):
Logger.debug("Unknown file type: %s", ext[1]) Logger.debug("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("Creating destination folder: %s", outputDestination)
@ -145,24 +150,23 @@ def unpack(dirpath, file, outputDestination):
return False return False
Logger.info("Extracting %s to %s", fp, outputDestination) 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) Logger.debug("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
try: 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) res = call(run)
if res == 0: if res == 0:
Logger.info("Extraction was successful for %s to %s", fp, outputDestination) Logger.info("Extraction was successful for %s to %s", fp, outputDestination)
else: 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: except:
Logger.error("Extraction failed for %s. Could not call command %s", fp, run) Logger.error("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 ""
res = call([cmd[0], fp]) res = call([cmd[0], fp])
else: else:
res = call([cmd[0], cmd[1], fp]) res = call([cmd[0], cmd[1], fp])
@ -177,22 +181,22 @@ def unpack(dirpath, file, outputDestination):
def flatten(outputDestination): def flatten(outputDestination):
Logger.info("Flattening directory: %s", 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: 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:
try: try:
shutil.move(os.path.join(dirpath, filename), outputDestination) shutil.move(os.path.join(dirpath, filename), outputDestination)
except OSError: except OSError:
Logger.info("Could not flatten %s", os.path.join(dirpath, filename)) Logger.info("Could not flatten %s", os.path.join(dirpath, filename))
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("Removing empty folders in: %s", path)
if not os.path.isdir(path): if not os.path.isdir(path):
return return
# remove empty subfolders # Remove empty subfolders
files = os.listdir(path) files = os.listdir(path)
if len(files): if len(files):
for f in files: for f in files:
@ -200,7 +204,7 @@ def removeEmptyFolders(path):
if os.path.isdir(fullpath): if os.path.isdir(fullpath):
removeEmptyFolders(fullpath) removeEmptyFolders(fullpath)
# 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("Removing empty folder: %s", path)
@ -218,7 +222,8 @@ else:
config.read(configFilename) config.read(configFilename)
if len(sys.argv) == 3: 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]) 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("Received Directory: %s | Name: %s", inputDirectory, inputName)
@ -236,19 +241,19 @@ else:
mediaContainer = config.get("Torrent", "mediaExtentions") mediaContainer = config.get("Torrent", "mediaExtentions")
metaContainer = config.get("Torrent", "metaExtentions") 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) root = int(0)
video = int(0) video = int(0)
video2 = int(0) video2 = int(0)
inputCategory = '' # We dont have a category yet 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: if inputCategory == movieCategory:
outputDestination = os.path.normpath(os.path.join(movieDestination, inputName)) outputDestination = os.path.normpath(os.path.join(movieDestination, inputName))
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.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) sys.exit(-1)
Logger.debug("Scanning files in directory: %s", inputDirectory) Logger.debug("Scanning files in directory: %s", inputDirectory)
@ -257,12 +262,12 @@ else:
if root == 1: if root == 1:
Logger.debug("Looking for %s in filename", inputName) Logger.debug("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:
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) filePath = os.path.join(dirpath, file)
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("File %s is a sample file. Ignoring", filePath)
continue continue
@ -270,43 +275,43 @@ 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("Found video file %s", file)
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("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("Found metadata file %s.", file)
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("Failed to link file %s", file)
elif fileExtention in compressedContainer: elif fileExtention in compressedContainer:
Logger.info("Found compressed archive %s.", file) Logger.info("Found compressed archive %s", file)
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("Failed to unpack file %s", file)
else: else:
Logger.info("Unknown file type %s for file %s. Ignoring", fileExtention, filePath) Logger.info("Unknown file type %s for file %s. Ignoring", fileExtention, filePath)
continue continue
flatten(outputDestination) 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 dirpath, dirnames, filenames in os.walk(outputDestination):
for file in filenames: for file in filenames:
filePath = os.path.join(dirpath, file) filePath = os.path.join(dirpath, file)
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
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
if status == 0: if status == 0:
Logger.info("Successful download") Logger.info("Successful download")
## Now we pass off to CouchPotato or SickBeard. # 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("Calling postprocessing script for CouchPotatoServer")
autoProcessMovie.process(outputDestination, inputName, status) autoProcessMovie.process(outputDestination, inputName, status)