Complete re-code of linking in TorrentToMedia

This commit is contained in:
echel0n 2014-04-19 22:47:10 -07:00
commit 6a0158d801
14 changed files with 291 additions and 208 deletions

View file

@ -16,9 +16,8 @@ from nzbtomedia.autoProcess.autoProcessMovie import autoProcessMovie
from nzbtomedia.autoProcess.autoProcessMusic import autoProcessMusic
from nzbtomedia.autoProcess.autoProcessTV import autoProcessTV
from nzbtomedia.extractor import extractor
from nzbtomedia.nzbToMediaUtil import category_search, sanitizeFileName, is_sample, copy_link, parse_args, flatten, get_dirnames, \
remove_read_only, cleanup_directories, create_torrent_class, pause_torrent, resume_torrent, listMediaFiles, \
listArchiveFiles
from nzbtomedia.nzbToMediaUtil import category_search, sanitizeFileName, copy_link, parse_args, flatten, get_dirnames, \
remove_read_only, cleanup_directories, create_torrent_class, pause_torrent, resume_torrent, listMediaFiles, joinPath
from nzbtomedia import logger
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
@ -45,7 +44,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
if inputCategory == "":
inputCategory = "UNCAT"
outputDestination = os.path.normpath(os.path.join(nzbtomedia.OUTPUTDIRECTORY, inputCategory, sanitizeFileName(inputName)))
outputDestination = os.path.normpath(joinPath(nzbtomedia.OUTPUTDIRECTORY, inputCategory, sanitizeFileName(inputName)))
logger.info("Output directory set to: %s" % (outputDestination))
if nzbtomedia.CFG["SickBeard"][inputCategory]:
@ -85,92 +84,87 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
outputDestinationMaster = outputDestination # Save the original, so we can change this within the loop below, and reset afterwards.
now = datetime.datetime.now()
if single: inputDirectory,filename = os.path.split(inputDirectory)
for dirpath, dirnames, filenames in os.walk(inputDirectory):
if single:
dirnames[:] = []
filenames[:] = [filenames] # we just want to work with this one file if single = True
logger.debug("Found %s files in %s" % (str(len(filenames)), dirpath))
for file in filenames:
filePath = os.path.join(dirpath, file)
fileName, fileExtension = os.path.splitext(file)
if inputCategory in nzbtomedia.NOFLATTEN and not single:
newDir = dirpath # find the full path
newDir = newDir.replace(inputDirectory, "") #find the extra-depth directory
if len(newDir) > 0 and newDir[0] == "/":
newDir = newDir[1:] # remove leading "/" to enable join to work.
outputDestination = os.path.join(outputDestinationMaster, newDir) # join this extra directory to output.
inputFiles = listMediaFiles(inputDirectory)
logger.debug("Found %s files in %s" % (str(len(inputFiles)), inputDirectory))
for inputFile in inputFiles:
fileDirPath = os.path.dirname(inputFile)
fileName, fileExt = os.path.splitext(os.path.basename(inputFile))
fullFileName = os.path.basename(inputFile)
if inputCategory in nzbtomedia.NOFLATTEN:
if not fileDirPath == outputDestinationMaster:
outputDestination = joinPath(outputDestinationMaster, fileDirPath) # join this extra directory to output.
logger.debug("Setting outputDestination to %s to preserve folder structure" % (outputDestination))
targetDirectory = os.path.join(outputDestination, file)
targetDirectory = joinPath(outputDestination, fullFileName)
if root == 1:
if foundFile == 0:
logger.debug("Looking for %s in: %s" % (inputName, file))
if (sanitizeFileName(inputName) in sanitizeFileName(file)) or (sanitizeFileName(fileName) in sanitizeFileName(inputName)):
#pass # This file does match the Torrent name
foundFile = 1
logger.debug("Found file %s that matches Torrent Name %s" % (file, inputName))
else:
continue # This file does not match the Torrent name, skip it
if root == 2:
if foundFile == 0:
logger.debug("Looking for files with modified/created dates less than 5 minutes old.")
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
foundFile = 1
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
if fileExtension in nzbtomedia.MEDIACONTAINER and is_sample(filePath, inputName, nzbtomedia.MINSAMPLESIZE,
nzbtomedia.SAMPLEIDS) and not nzbtomedia.CFG["HeadPhones"][inputCategory]: # Ignore samples
logger.info("Ignoring sample file: %s " % (filePath))
if root == 1:
if not foundFile:
logger.debug("Looking for %s in: %s" % (inputName, fullFileName))
if (sanitizeFileName(inputName) in sanitizeFileName(fullFileName)) or (sanitizeFileName(fileName) in sanitizeFileName(inputName)):
foundFile = True
logger.debug("Found file %s that matches Torrent Name %s" % (fullFileName, inputName))
else:
continue
if fileExtension in nzbtomedia.COMPRESSEDCONTAINER:
if not (nzbtomedia.CFG["SickBeard"][inputCategory] and nzbtomedia.CFG["SickBeard"][inputCategory]["nzbExtractionBy"] == "Destination"):
# find part numbers in second "extension" from right, if we have more than 1 compressed file in the same directory.
if re.search(r'\d+', os.path.splitext(fileName)[1]) and os.path.dirname(filePath) in extracted_folder and not any(item in os.path.splitext(fileName)[1] for item in ['.720p','.1080p','.x264']):
part = int(re.search(r'\d+', os.path.splitext(fileName)[1]).group())
if part == 1: # we only want to extract the primary part.
logger.debug("Found primary part of a multi-part archive %s. Extracting" % (file))
else:
logger.debug("Found part %s of a multi-part archive %s. Ignoring" % (part, file))
continue
logger.info("Found compressed archive %s for file %s" % (fileExtension, filePath))
try:
extractor.extract(filePath, outputDestination)
extractionSuccess = True # we use this variable to determine if we need to pause a torrent or not in uTorrent (don't need to pause archived content)
extracted_folder.append(os.path.dirname(filePath))
except:
logger.error("Extraction failed for: %s" % (file))
continue
if root == 2:
mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(inputFile))
ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(inputFile))
try:
copy_link(filePath, targetDirectory, nzbtomedia.USELINK, outputDestination)
copy_list.append([filePath, os.path.join(outputDestination, file)])
except:
logger.error("Failed to link file: %s" % (file))
if not foundFile:
logger.debug("Looking for files with modified/created dates less than 5 minutes old.")
if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)):
#pass # This file does match the date time criteria
foundFile = True
logger.debug("Found file %s with date modifed/created less than 5 minutes ago." % (fullFileName))
else:
continue # This file has not been recently moved or created, skip it
if fileExt in nzbtomedia.COMPRESSEDCONTAINER:
if not (nzbtomedia.CFG["SickBeard"][inputCategory] and nzbtomedia.CFG["SickBeard"][inputCategory]["nzbExtractionBy"] == "Destination"):
# find part numbers in second "extension" from right, if we have more than 1 compressed file in the same directory.
if re.search(r'\d+', os.path.splitext(fileName)[1]) and fileDirPath in extracted_folder and not any(item in os.path.splitext(fileName)[1] for item in ['.720p','.1080p','.x264']):
part = int(re.search(r'\d+', os.path.splitext(fileName)[1]).group())
if part == 1: # we only want to extract the primary part.
logger.debug("Found primary part of a multi-part archive %s. Extracting" % (fullFileName))
else:
logger.debug("Found part %s of a multi-part archive %s. Ignoring" % (part, fullFileName))
continue
logger.info("Found compressed archive %s for file %s" % (fileExt, inputFile))
try:
extractor.extract(inputFile, outputDestination)
extractionSuccess = True # we use this variable to determine if we need to pause a torrent or not in uTorrent (don't need to pause archived content)
extracted_folder.append(fileDirPath)
except:
logger.error("Extraction failed for: %s" % (fullFileName))
continue
try:
copy_link(inputFile, targetDirectory, nzbtomedia.USELINK, outputDestination)
copy_list.append([inputFile, joinPath(outputDestination, fullFileName)])
except:
logger.error("Failed to link file: %s" % (fullFileName))
outputDestination = outputDestinationMaster # Reset here.
if not inputCategory in nzbtomedia.NOFLATTEN: #don't flatten hp in case multi cd albums, and we need to copy this back later.
flatten(outputDestination)
if platform.system().lower() == 'windows': # remove Read Only flag from files in Windows.
if platform.system() == 'Windows': # remove Read Only flag from files in Windows.
remove_read_only(outputDestination)
# Now check if video files exist in destination:
if nzbtomedia.CFG["SickBeard","NzbDrone", "CouchPotato"][inputCategory]:
for videofile in listMediaFiles(outputDestination):
logger.debug("Found media file: %s" % (videofile))
video += 1
for archivefile in listArchiveFiles(outputDestination):
logger.debug("Found archive file: %s" % (archivefile))
archive += 1
for outputFile in listMediaFiles(outputDestination):
fullFileName = os.path.basename(outputFile)
fileName, fileExt = os.path.splitext(fullFileName)
if fileExt in nzbtomedia.MEDIACONTAINER:
logger.debug("Found media file: %s" % (fullFileName))
video += 1
if fileExt in nzbtomedia.COMPRESSEDCONTAINER:
logger.debug("Found archive file: %s" % (fullFileName))
archive += 1
if video > 0:
logger.debug("Found %s media files" % (str(video)))
@ -231,7 +225,7 @@ def external_script(outputDestination, torrentName, torrentLabel):
for dirpath, dirnames, filenames in os.walk(outputDestination):
for file in filenames:
filePath = os.path.join(dirpath, file)
filePath = joinPath(dirpath, file)
fileName, fileExtension = os.path.splitext(file)
if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or "ALL" in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS:
@ -284,7 +278,7 @@ def external_script(outputDestination, torrentName, torrentLabel):
num_files_new = 0
for dirpath, dirnames, filenames in os.walk(outputDestination):
for file in filenames:
filePath = os.path.join(dirpath, file)
filePath = joinPath(dirpath, file)
fileName, fileExtension = os.path.splitext(file)
if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS == "ALL":