diff --git a/nzbtomedia/Transcoder.py b/nzbtomedia/Transcoder.py index 9d59fcb9..fa734674 100644 --- a/nzbtomedia/Transcoder.py +++ b/nzbtomedia/Transcoder.py @@ -2,8 +2,7 @@ import errno import os import platform import nzbtomedia -from subprocess import call, Popen -from lib import guessit +from subprocess import call from nzbtomedia import logger from nzbToMediaUtil import listMediaFiles diff --git a/nzbtomedia/autoProcess/autoProcessMovie.py b/nzbtomedia/autoProcess/autoProcessMovie.py index f223c936..7b45b1a3 100644 --- a/nzbtomedia/autoProcess/autoProcessMovie.py +++ b/nzbtomedia/autoProcess/autoProcessMovie.py @@ -4,7 +4,7 @@ import nzbtomedia from lib import requests from nzbtomedia.Transcoder import Transcoder from nzbtomedia.nzbToMediaSceneExceptions import process_all_exceptions -from nzbtomedia.nzbToMediaUtil import convert_to_ascii, delete, find_imdbid, find_download, joinPath +from nzbtomedia.nzbToMediaUtil import convert_to_ascii, rmDir, find_imdbid, find_download, joinPath from nzbtomedia import logger @@ -96,7 +96,7 @@ class autoProcessMovie: port = nzbtomedia.CFG[section][inputCategory]["port"] apikey = nzbtomedia.CFG[section][inputCategory]["apikey"] method = nzbtomedia.CFG[section][inputCategory]["method"] - delete_failed = int(nzbtomedia.CFG[section][inputCategory]["delete_failed"]) + rmDir_failed = int(nzbtomedia.CFG[section][inputCategory]["rmDir_failed"]) wait_for = int(nzbtomedia.CFG[section][inputCategory]["wait_for"]) try: @@ -186,9 +186,9 @@ class autoProcessMovie: else: logger.postprocess("FAILED DOWNLOAD DETECTED FOR %s" % (nzbName), section) - if delete_failed and os.path.isdir(dirName) and not os.path.dirname(dirName) == dirName: + if rmDir_failed and os.path.isdir(dirName) and not os.path.dirname(dirName) == dirName: logger.postprocess("Deleting failed files and folder %s" % dirName, section) - delete(dirName) + rmDir(dirName) if not download_id: logger.error("Could not find a downloaded movie in the database matching %s, exiting!" % nzbName, diff --git a/nzbtomedia/autoProcess/autoProcessTV.py b/nzbtomedia/autoProcess/autoProcessTV.py index 4e5d8f95..c4c6c441 100644 --- a/nzbtomedia/autoProcess/autoProcessTV.py +++ b/nzbtomedia/autoProcess/autoProcessTV.py @@ -5,7 +5,7 @@ from lib import requests from nzbtomedia.Transcoder import Transcoder from nzbtomedia.nzbToMediaAutoFork import autoFork from nzbtomedia.nzbToMediaSceneExceptions import process_all_exceptions -from nzbtomedia.nzbToMediaUtil import convert_to_ascii, is_sample, flatten, delete, joinPath +from nzbtomedia.nzbToMediaUtil import convert_to_ascii, flatten, rmDir, joinPath, listMediaFiles from nzbtomedia import logger class autoProcessTV: @@ -40,9 +40,9 @@ class autoProcessTV: except: web_root = "" try: - delete_failed = int(nzbtomedia.CFG[section][inputCategory]["delete_failed"]) + rmDir_failed = int(nzbtomedia.CFG[section][inputCategory]["rmDir_failed"]) except: - delete_failed = 0 + rmDir_failed = 0 try: nzbExtractionBy = nzbtomedia.CFG[section][inputCategory]["nzbExtractionBy"] except: @@ -76,17 +76,15 @@ class autoProcessTV: nzbName, dirName = convert_to_ascii(nzbName, dirName) # Now check if tv files exist in destination. Eventually extraction may be done here if nzbExtractionBy == TorrentToMedia - video = int(0) - for dirpath, dirnames, filenames in os.walk(dirName): - for file in filenames: - filePath = joinPath(dirpath, file) - fileExtension = os.path.splitext(file)[1] - if fileExtension in nzbtomedia.MEDIACONTAINER: # If the file is a video file - if is_sample(filePath, nzbName, nzbtomedia.MINSAMPLESIZE, nzbtomedia.SAMPLEIDS): - logger.debug("Removing sample file: %s" % (filePath), section) - os.unlink(filePath) # remove samples - else: - video = video + 1 + video = 0 + for dirFile in listMediaFiles(dirName): + fullFileName = os.path.basename(dirFile) + fileName, fileExt = os.path.splitext(fullFileName) + + if fileExt in nzbtomedia.MEDIACONTAINER: + logger.debug("Found media file: %s" % (fullFileName)) + video += 1 + if video > 0: # Check that a video exists. if not, assume failed. flatten(dirName) # to make sure SickBeard can find the video (not in sub-folder) elif clientAgent == "manual": @@ -94,7 +92,7 @@ class autoProcessTV: return 0 # Success (as far as this script is concerned) else: logger.warning("No media files found in directory %s. Processing this as a failed download" % (dirName), section) - status = int(1) + status = 1 failed = True # configure SB params to pass @@ -120,7 +118,7 @@ class autoProcessTV: else: del fork_params[param] - # delete any unused params so we don't pass them to SB by mistake + # rmDir any unused params so we don't pass them to SB by mistake [fork_params.pop(k) for k,v in fork_params.items() if v is None] if status == 0: @@ -130,9 +128,9 @@ class autoProcessTV: logger.postprocess("FAILED: The download failed. Sending 'failed' process request to %s branch" % (fork), section) else: logger.postprocess("FAILED: The download failed. %s branch does not handle failed downloads. Nothing to process" % (fork), section) - if delete_failed and os.path.isdir(dirName) and not os.path.dirname(dirName) == dirName: + if rmDir_failed and os.path.isdir(dirName) and not os.path.dirname(dirName) == dirName: logger.postprocess("Deleting failed files and folder %s" % (dirName), section) - delete(dirName) + rmDir(dirName) return 0 # Success (as far as this script is concerned) if status == 0 and nzbtomedia.TRANSCODE == 1: # only transcode successful downlaods @@ -170,7 +168,7 @@ class autoProcessTV: for line in r.iter_lines(): if line: logger.postprocess("%s" % (line), section) - if status != 0 and delete_failed and not os.path.dirname(dirName) == dirName: + if status != 0 and rmDir_failed and not os.path.dirname(dirName) == dirName: logger.postprocess("Deleting failed files and folder %s" % (dirName),section) - delete(dirName) + rmDir(dirName) return 0 # Success diff --git a/nzbtomedia/nzbToMediaUtil.py b/nzbtomedia/nzbToMediaUtil.py index ad340eca..4745a11b 100644 --- a/nzbtomedia/nzbToMediaUtil.py +++ b/nzbtomedia/nzbToMediaUtil.py @@ -428,14 +428,13 @@ def get_dirnames(section, subsections=None): return list(set(dirNames)) -def delete(dirName): +def rmDir(dirName): logger.info("Deleting %s" % (dirName)) try: shutil.rmtree(dirName, True) except: logger.error("Unable to delete folder %s" % (dirName)) - def cleanup_directories(inputCategory, processCategories, result, directory): if inputCategory in processCategories and result == 0 and os.path.isdir(directory): num_files_new = int(0) @@ -612,6 +611,10 @@ def listMediaFiles(path, ignoreSample=True): elif isMediaFile(curFile): # Optionally ignore sample files if ignoreSample and is_sample(fullCurFile, nzbtomedia.MINSAMPLESIZE, nzbtomedia.SAMPLEIDS): + try: + os.unlink(fullCurFile) + logger.debug('Sample file %s has been removed.' % (curFile)) + except:continue continue files.append(fullCurFile)