mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-22 06:13:19 -07:00
cleanup
cleaner variables restructure, we dont need to exectute code thats not needed
This commit is contained in:
parent
2ba29b4541
commit
141eaf9456
1 changed files with 117 additions and 121 deletions
|
@ -23,27 +23,6 @@ fileHandler.formatter = logging.Formatter('%(asctime)s|%(levelname)-7.7s %(messa
|
||||||
fileHandler.level = logging.DEBUG
|
fileHandler.level = logging.DEBUG
|
||||||
Logger.addHandler(fileHandler)
|
Logger.addHandler(fileHandler)
|
||||||
|
|
||||||
Logger.info("TorrentToMedia %s", VERSION)
|
|
||||||
|
|
||||||
config = ConfigParser.ConfigParser()
|
|
||||||
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg")
|
|
||||||
|
|
||||||
Logger.info("Loading config from %s", configFilename)
|
|
||||||
|
|
||||||
if not os.path.isfile(configFilename):
|
|
||||||
Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?")
|
|
||||||
sys.exit(-1)
|
|
||||||
else:
|
|
||||||
config.read(configFilename)
|
|
||||||
# Sick-Beard
|
|
||||||
tvCategory = config.get("SickBeard", "category")
|
|
||||||
tvDestination = os.path.normpath(config.get("SickBeard", "outputDirectory"))
|
|
||||||
# CouchPotato
|
|
||||||
movieDestination = os.path.normpath(config.get("CouchPotato", "outputDirectory"))
|
|
||||||
movieCategory = config.get("CouchPotato", "category")
|
|
||||||
useLink = int(config.get("Torrent", "uselink"))
|
|
||||||
extractionTool = os.path.normpath(config.get("Torrent", "extractiontool"))
|
|
||||||
|
|
||||||
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:
|
||||||
|
@ -81,11 +60,11 @@ def category_search(inputDirectory, inputCategory, root):
|
||||||
root = 1
|
root = 1
|
||||||
return inputDirectory, inputCategory, root
|
return inputDirectory, inputCategory, root
|
||||||
|
|
||||||
def is_sample(file_path, 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 file_path.lower()) and (not 'sample' in inputName) and (os.path.getsize(file_path) < 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
|
||||||
|
@ -117,16 +96,16 @@ 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']
|
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)
|
||||||
Logger.info("We are using Windows")
|
Logger.info("We are using Windows")
|
||||||
|
|
||||||
## Using linux?
|
## Using linux
|
||||||
elif os.name == 'posix':
|
elif os.name == 'posix':
|
||||||
required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"]
|
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", ""],
|
||||||
|
@ -140,10 +119,8 @@ def unpack(dirpath, file, outputDestination):
|
||||||
".7z": ["7zr", "x"],
|
".7z": ["7zr", "x"],
|
||||||
}
|
}
|
||||||
Logger.info("We are using *nix")
|
Logger.info("We are using *nix")
|
||||||
|
|
||||||
## Need to add a check for which commands that can be utilized in *nix systems..
|
|
||||||
else:
|
else:
|
||||||
Logger.error("Unknown OS, 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)
|
||||||
|
@ -169,11 +146,11 @@ def unpack(dirpath, file, outputDestination):
|
||||||
|
|
||||||
Logger.info("Extracting %s to %s", fp, outputDestination)
|
Logger.info("Extracting %s to %s", fp, outputDestination)
|
||||||
|
|
||||||
## Running..
|
## 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)
|
||||||
|
@ -229,96 +206,115 @@ def removeEmptyFolders(path):
|
||||||
Logger.debug("Removing empty folder: %s", path)
|
Logger.debug("Removing empty folder: %s", path)
|
||||||
os.rmdir(path)
|
os.rmdir(path)
|
||||||
|
|
||||||
if len(sys.argv) == 3:
|
Logger.info("TorrentToMedia %s", VERSION)
|
||||||
|
config = ConfigParser.ConfigParser()
|
||||||
|
configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg")
|
||||||
|
|
||||||
|
if not os.path.isfile(configFilename):
|
||||||
|
Logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?")
|
||||||
|
sys.exit(-1)
|
||||||
|
else:
|
||||||
|
Logger.info("Loading config from %s", configFilename)
|
||||||
|
config.read(configFilename)
|
||||||
|
|
||||||
|
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
|
||||||
inputDirectory = os.path.normpath(sys.argv[1])
|
inputDirectory = os.path.normpath(sys.argv[1])
|
||||||
inputName = sys.argv[2]
|
inputName = sys.argv[2]
|
||||||
inputCategory = '' # We dont have a category, so assume the last directory is the category for now.
|
|
||||||
else:
|
|
||||||
Logger.error("There was a problem loading variables: Exiting")
|
|
||||||
sys.exit(-1)
|
|
||||||
|
|
||||||
Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName)
|
# Sick-Beard
|
||||||
|
tvCategory = config.get("SickBeard", "category")
|
||||||
|
tvDestination = os.path.normpath(config.get("SickBeard", "outputDirectory"))
|
||||||
|
# CouchPotatoServer
|
||||||
|
movieCategory = config.get("CouchPotato", "category")
|
||||||
|
movieDestination = os.path.normpath(config.get("CouchPotato", "outputDirectory"))
|
||||||
|
# Torrent specific
|
||||||
|
useLink = int(config.get("Torrent", "uselink"))
|
||||||
|
extractionTool = os.path.normpath(config.get("Torrent", "extractiontool"))
|
||||||
|
inputCategory = '' # We dont have a category yet
|
||||||
|
|
||||||
status = 1 # we start as "failed" until we verify movie file in destination
|
status = 1 # we start as "failed" until we verify movie file in destination
|
||||||
root = 0
|
root = 0
|
||||||
video = 0
|
video = 0
|
||||||
video2 = 0
|
video2 = 0
|
||||||
|
compressedContainer = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj']
|
||||||
|
mediaContainer = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.vob', '.iso']
|
||||||
|
metaFile = ['.nfo', '.sub', '.srt', '.jpg', '.gif']
|
||||||
|
|
||||||
inputDirectory, inputCategory, root = category_search(inputDirectory, inputCategory, root) # confirm the catgeogy by parsing directory structure.
|
Logger.debug("Received Directory: %s | Name: %s", inputDirectory, inputName)
|
||||||
|
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.info("Category of %s does not match either %s or %s: Exiting", inputCategory, movieCategory, tvCategory)
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
packed_files = ['.zip', '.rar', '.7z', '.gz', '.bz', '.tar', '.arj']
|
Logger.debug("Scanning files in directory: %s", inputDirectory)
|
||||||
video_files = ['.mkv', '.avi', '.divx', '.xvid', '.mov', '.wmv', '.mp4', '.mpg', '.mpeg', '.vob', '.iso']
|
for dirpath, dirnames, filenames in os.walk(inputDirectory):
|
||||||
meta_files = ['.nfo', '.sub', '.srt', '.jpg', '.gif']
|
|
||||||
|
|
||||||
Logger.debug("Scanning files in directory: %s", 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("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
|
||||||
file_path = os.path.join(dirpath, file)
|
filePath = os.path.join(dirpath, file)
|
||||||
file_ext = os.path.splitext(file)[1]
|
fileExtention = os.path.splitext(file)[1]
|
||||||
if file_ext in video_files: #if the file is a video file.
|
if fileExtention in mediaContainer: #if the file is a video file.
|
||||||
if is_sample(file_path, inputName):
|
if is_sample(filePath, inputName): # Ignore samples
|
||||||
Logger.info("file %s is a sample file. Ignoring", file_path)
|
Logger.info("File %s is a sample file. Ignoring", filePath)
|
||||||
continue #ignore samples
|
continue
|
||||||
|
else:
|
||||||
video = video + 1
|
video = video + 1
|
||||||
source = file_path
|
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 file_ext in meta_files:
|
elif fileExtention in metaFile:
|
||||||
source = file_path
|
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 file_ext in packed_files:
|
elif fileExtention in compressedContainer:
|
||||||
Logger.info("Found packed file %s.", file)
|
Logger.info("Found compressed archive %s.", file)
|
||||||
source = file_path
|
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", file_ext, file_path)
|
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:
|
||||||
file_path = os.path.join(dirpath, file)
|
filePath = os.path.join(dirpath, file)
|
||||||
file_ext = os.path.splitext(file)[1]
|
fileExtention = os.path.splitext(file)[1]
|
||||||
if file_ext in video_files: #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
|
||||||
|
|
||||||
status = int(status)
|
if status == 0:
|
||||||
if status == 0:
|
Logger.info("Successful download")
|
||||||
Logger.info("calling autoProcess script for successful download")
|
## Now we pass off to CouchPotato or SickBeard.
|
||||||
else:
|
# still need to figure out how to log this output.
|
||||||
Logger.info("calling autoProcess script for failed download")
|
if inputCategory == movieCategory:
|
||||||
## Now we pass off to CouchPotato or SickBeard.
|
Logger.info("Calling postprocessing script for CouchPotatoServer")
|
||||||
# still need to figure out how to log this output.
|
|
||||||
if inputCategory == movieCategory:
|
|
||||||
autoProcessMovie.process(outputDestination, inputName, status)
|
autoProcessMovie.process(outputDestination, inputName, status)
|
||||||
elif inputCategory == tvCategory:
|
elif inputCategory == tvCategory:
|
||||||
|
Logger.info("Calling postprocessing script for Sick-Beard")
|
||||||
autoProcessTV.processEpisode(outputDestination, inputName, status)
|
autoProcessTV.processEpisode(outputDestination, inputName, status)
|
||||||
|
else:
|
||||||
|
Logger.info("Postprocessing failed")
|
||||||
|
else:
|
||||||
|
Logger.error("There was a problem loading variables: Exiting")
|
||||||
|
sys.exit(-1)
|
Loading…
Add table
Add a link
Reference in a new issue