Added function to return a list of archive files.

Cleaned and fixed code in TorrentToMedia, should resolve issue #314
This commit is contained in:
echel0n 2014-04-19 18:52:43 -07:00
commit 06c2dae5ce
2 changed files with 67 additions and 43 deletions

View file

@ -17,15 +17,16 @@ from nzbtomedia.autoProcess.autoProcessMusic import autoProcessMusic
from nzbtomedia.autoProcess.autoProcessTV import autoProcessTV from nzbtomedia.autoProcess.autoProcessTV import autoProcessTV
from nzbtomedia.extractor import extractor from nzbtomedia.extractor import extractor
from nzbtomedia.nzbToMediaUtil import category_search, sanitizeFileName, is_sample, copy_link, parse_args, flatten, get_dirnames, \ 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 remove_read_only, cleanup_directories, create_torrent_class, pause_torrent, resume_torrent, listMediaFiles, \
listArchiveFiles
from nzbtomedia import logger from nzbtomedia import logger
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent): def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
status = int(1) # 1 = failed | 0 = success status = int(1) # 1 = failed | 0 = success
root = int(0) root = 0
video = int(0) video = 0
archive = int(0) archive = 0
foundFile = int(0) foundFile = 0
extracted_folder = [] extracted_folder = []
copy_list = [] copy_list = []
@ -87,7 +88,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
if single: inputDirectory,filename = os.path.split(inputDirectory) if single: inputDirectory,filename = os.path.split(inputDirectory)
for dirpath, dirnames, filenames in os.walk(inputDirectory): for dirpath, dirnames, filenames in os.walk(inputDirectory):
if single: if single:
dirnames[:] = [] dirnames[:] = []
filenames[:] = [filenames] # we just want to work with this one file if single = True 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)) logger.debug("Found %s files in %s" % (str(len(filenames)), dirpath))
for file in filenames: for file in filenames:
@ -104,7 +105,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
targetDirectory = os.path.join(outputDestination, file) targetDirectory = os.path.join(outputDestination, file)
if root == 1: if root == 1:
if foundFile == int(0): if foundFile == 0:
logger.debug("Looking for %s in: %s" % (inputName, file)) logger.debug("Looking for %s in: %s" % (inputName, file))
if (sanitizeFileName(inputName) in sanitizeFileName(file)) or (sanitizeFileName(fileName) in sanitizeFileName(inputName)): if (sanitizeFileName(inputName) in sanitizeFileName(file)) or (sanitizeFileName(fileName) in sanitizeFileName(inputName)):
#pass # This file does match the Torrent name #pass # This file does match the Torrent name
@ -114,7 +115,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
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 foundFile == int(0): if foundFile == 0:
logger.debug("Looking for files with modified/created dates less than 5 minutes old.") 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))) 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)))
@ -164,36 +165,30 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
# Now check if video files exist in destination: # Now check if video files exist in destination:
if nzbtomedia.CFG["SickBeard","NzbDrone", "CouchPotato"][inputCategory]: if nzbtomedia.CFG["SickBeard","NzbDrone", "CouchPotato"][inputCategory]:
for dirpath, dirnames, filenames in os.walk(outputDestination): for videofile in listMediaFiles(outputDestination):
for file in filenames: logger.debug("Found media file: %s" % (videofile))
filePath = os.path.join(dirpath, file) video += 1
fileName, fileExtension = os.path.splitext(file) for archivefile in listArchiveFiles(outputDestination):
if fileExtension in nzbtomedia.MEDIACONTAINER: # If the file is a video file logger.debug("Found archive file: %s" % (archivefile))
logger.debug("Found media file: %s" % (filePath)) archive += 1
video += 1
if fileExtension in nzbtomedia.COMPRESSEDCONTAINER: # If the file is an archive file
archive += 1
if video > int(0): # Check that media files exist
logger.debug("Found %s media files" % (str(video)))
status = int(0)
elif not (nzbtomedia.CFG["SickBeard"][inputCategory] and nzbtomedia.CFG["SickBeard"][inputCategory]["nzbExtractionBy"] == "Destination") and archive > int(0):
logger.debug("Found %s archive files to be extracted by SickBeard" % (str(archive)))
status = int(0)
else:
logger.warning("Found no media files in output.")
if video > 0:
logger.debug("Found %s media files" % (str(video)))
status = 0
elif archive > 0 and not nzbtomedia.CFG["SickBeard"][inputCategory]["nzbExtractionBy"] == "Destination":
logger.debug("Found %s archive files to be extracted by SickBeard" % (str(archive)))
status = 0
else:
logger.warning("Found no media files in %s" % outputDestination)
result = 0
if (inputCategory in nzbtomedia.USER_SCRIPT_CATEGORIES and not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES) or ("ALL" in nzbtomedia.USER_SCRIPT_CATEGORIES and not inputCategory in processCategories): if (inputCategory in nzbtomedia.USER_SCRIPT_CATEGORIES and not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES) or ("ALL" in nzbtomedia.USER_SCRIPT_CATEGORIES and not inputCategory in processCategories):
logger.info("Processing user script %s." % (nzbtomedia.USER_SCRIPT)) logger.info("Processing user script %s." % (nzbtomedia.USER_SCRIPT))
result = external_script(outputDestination,inputName,inputCategory) result = external_script(outputDestination,inputName,inputCategory)
elif status == int(0) or (nzbtomedia.CFG['HeadPhones','Mylar','Gamez'][inputCategory]): # if movies linked/extracted or for other categories. elif status != 0:
logger.debug("Calling autoProcess script for successful download.")
status = int(0) # hp, my, gz don't support failed.
else:
logger.error("Something failed! Please check logs. Exiting") logger.error("Something failed! Please check logs. Exiting")
return status return status
result = 0
# Check video files for corruption # Check video files for corruption
for video in listMediaFiles(inputDirectory): for video in listMediaFiles(inputDirectory):
if not Transcoder().isVideoGood(video): if not Transcoder().isVideoGood(video):
@ -210,19 +205,20 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
logger.info("Calling NzbDrone:" + inputCategory + " to post-process: %s" % (inputName)) logger.info("Calling NzbDrone:" + inputCategory + " to post-process: %s" % (inputName))
result = autoProcessTV().processEpisode(outputDestination, inputName, status, clientAgent, inputCategory) result = autoProcessTV().processEpisode(outputDestination, inputName, status, clientAgent, inputCategory)
elif nzbtomedia.CFG['HeadPhones'][inputCategory]: elif nzbtomedia.CFG['HeadPhones'][inputCategory]:
status = 0 #Failed Handling Not Supported
logger.info("Calling HeadPhones:" + inputCategory + " to post-process: %s" % (inputName)) logger.info("Calling HeadPhones:" + inputCategory + " to post-process: %s" % (inputName))
result = autoProcessMusic().process(outputDestination, inputName, status, clientAgent, inputCategory) result = autoProcessMusic().process(outputDestination, inputName, status, clientAgent, inputCategory)
elif nzbtomedia.CFG['Mylar'][inputCategory]: elif nzbtomedia.CFG['Mylar'][inputCategory]:
status = 0 #Failed Handling Not Supported
logger.info("Calling Mylar:" + inputCategory + " to post-process: %s" % (inputName)) logger.info("Calling Mylar:" + inputCategory + " to post-process: %s" % (inputName))
result = autoProcessComics().processEpisode(outputDestination, inputName, status, clientAgent, inputCategory) result = autoProcessComics().processEpisode(outputDestination, inputName, status, clientAgent, inputCategory)
elif nzbtomedia.CFG['Gamez'][inputCategory]: elif nzbtomedia.CFG['Gamez'][inputCategory]:
status = 0 #Failed Handling Not Supported
logger.info("Calling Gamez:" + inputCategory + " to post-process: %s" % (inputName)) logger.info("Calling Gamez:" + inputCategory + " to post-process: %s" % (inputName))
result = autoProcessGames().process(outputDestination, inputName, status, clientAgent, inputCategory) result = autoProcessGames().process(outputDestination, inputName, status, clientAgent, inputCategory)
if result == 1 and clientAgent != 'manual': if result != 0 and clientAgent != 'manual':
logger.error("A problem was reported in the autoProcess* script. If torrent was paused we will resume seeding") logger.error("A problem was reported in the autoProcess* script. If torrent was paused we will resume seeding")
if clientAgent != 'manual':
resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputName) resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputName)
cleanup_directories(inputCategory, processCategories, result, outputDestination) cleanup_directories(inputCategory, processCategories, result, outputDestination)
@ -230,8 +226,8 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
def external_script(outputDestination, torrentName, torrentLabel): def external_script(outputDestination, torrentName, torrentLabel):
final_result = int(0) # start at 0. final_result = 0 # start at 0.
num_files = int(0) num_files = 0
for dirpath, dirnames, filenames in os.walk(outputDestination): for dirpath, dirnames, filenames in os.walk(outputDestination):
for file in filenames: for file in filenames:
@ -274,7 +270,7 @@ def external_script(outputDestination, torrentName, torrentLabel):
res = p.wait() res = p.wait()
if str(res) in nzbtomedia.USER_SCRIPT_SUCCESSCODES: # Linux returns 0 for successful. if str(res) in nzbtomedia.USER_SCRIPT_SUCCESSCODES: # Linux returns 0 for successful.
logger.info("UserScript %s was successfull" % (command[0])) logger.info("UserScript %s was successfull" % (command[0]))
result = int(0) result = 0
else: else:
logger.error("UserScript %s has failed with return code: %s" % (command[0], res)) logger.error("UserScript %s has failed with return code: %s" % (command[0], res))
logger.info("If the UserScript completed successfully you should add %s to the user_script_successCodes" % (res)) logger.info("If the UserScript completed successfully you should add %s to the user_script_successCodes" % (res))
@ -285,7 +281,7 @@ def external_script(outputDestination, torrentName, torrentLabel):
final_result = final_result + result final_result = final_result + result
time.sleep(nzbtomedia.USER_DELAY) time.sleep(nzbtomedia.USER_DELAY)
num_files_new = int(0) num_files_new = 0
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)
@ -294,10 +290,10 @@ def external_script(outputDestination, torrentName, torrentLabel):
if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS == "ALL": if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS == "ALL":
num_files_new = num_files_new + 1 num_files_new = num_files_new + 1
if nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new == int(0) and final_result == int(0): if nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new == 0 and final_result == 0:
logger.info("All files have been processed. Cleaning outputDirectory %s" % (outputDestination)) logger.info("All files have been processed. Cleaning outputDirectory %s" % (outputDestination))
shutil.rmtree(outputDestination) shutil.rmtree(outputDestination)
elif nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new != int(0): elif nzbtomedia.USER_SCRIPT_CLEAN == int(1) and num_files_new != 0:
logger.info("%s files were processed, but %s still remain. outputDirectory will not be cleaned." % (num_files, num_files_new)) logger.info("%s files were processed, but %s still remain. outputDirectory will not be cleaned." % (num_files, num_files_new))
return final_result return final_result
@ -318,7 +314,6 @@ def main(args):
# Post-Processing Result # Post-Processing Result
result = 0 result = 0
try: try:
inputDirectory, inputName, inputCategory, inputHash, inputID = parse_args(clientAgent, args) inputDirectory, inputName, inputCategory, inputHash, inputID = parse_args(clientAgent, args)
except: except:

View file

@ -581,6 +581,37 @@ def clean_nzbname(nzbname):
return nzbname.strip() return nzbname.strip()
def isArchiveFile(filename):
# ignore samples
# ignore MAC OS's retarded "resource fork" files
if filename.startswith('._'):
return False
sepFile = filename.rpartition(".")
if sepFile[2].lower() in [i.replace(".","") for i in nzbtomedia.COMPRESSEDCONTAINER]:
return True
else:
return False
def listArchiveFiles(path):
if not dir or not os.path.isdir(path):
return []
files = []
for curFile in os.listdir(path):
fullCurFile = os.path.join(path, curFile).replace("\\","/")
# if it's a folder do it recursively
if os.path.isdir(fullCurFile) and not curFile.startswith('.'):
files += listMediaFiles(fullCurFile)
elif isMediaFile(curFile):
files.append(fullCurFile)
return files
def isMediaFile(filename): def isMediaFile(filename):
# ignore samples # ignore samples
if re.search('(^|[\W_])(sample\d*)[\W_]', filename, re.I): if re.search('(^|[\W_])(sample\d*)[\W_]', filename, re.I):
@ -600,7 +631,6 @@ def isMediaFile(filename):
else: else:
return False return False
def listMediaFiles(path): def listMediaFiles(path):
if not dir or not os.path.isdir(path): if not dir or not os.path.isdir(path):
return [] return []
@ -618,7 +648,6 @@ def listMediaFiles(path):
return files return files
def find_imdbid(dirName, nzbName): def find_imdbid(dirName, nzbName):
imdbid = None imdbid = None