Re-coded our safeName function used for sanitizing filenames.

Changed code for checking final status of processed movies.
This commit is contained in:
echel0n 2014-04-18 19:31:00 -07:00
commit d5fe1c871e
3 changed files with 53 additions and 36 deletions

View file

@ -15,7 +15,7 @@ from nzbtomedia.autoProcess.autoProcessMovie import autoProcessMovie
from nzbtomedia.autoProcess.autoProcessMusic import autoProcessMusic 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, safeName, 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 remove_read_only, cleanup_directories, create_torrent_class, pause_torrent, resume_torrent
from nzbtomedia import logger from nzbtomedia import logger
@ -43,13 +43,13 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
if inputCategory == "": if inputCategory == "":
inputCategory = "UNCAT" inputCategory = "UNCAT"
outputDestination = os.path.normpath(os.path.join(nzbtomedia.OUTPUTDIRECTORY, inputCategory, safeName(inputName))) outputDestination = os.path.normpath(os.path.join(nzbtomedia.OUTPUTDIRECTORY, inputCategory, sanitizeFileName(inputName)))
logger.postprocess("Output directory set to: %s" % (outputDestination)) logger.info("Output directory set to: %s" % (outputDestination))
if nzbtomedia.CFG["SickBeard"][inputCategory]: if nzbtomedia.CFG["SickBeard"][inputCategory]:
Torrent_NoLink = int(nzbtomedia.CFG["SickBeard"][inputCategory]["Torrent_NoLink"]) # 0 Torrent_NoLink = int(nzbtomedia.CFG["SickBeard"][inputCategory]["Torrent_NoLink"]) # 0
if Torrent_NoLink == 1: if Torrent_NoLink == 1:
logger.postprocess("Calling autoProcessTV to post-process: %s",inputName) logger.info("Calling autoProcessTV to post-process: %s",inputName)
result = autoProcessTV().processEpisode(inputDirectory, inputName, 0, clientAgent=clientAgent, inputCategory=inputCategory) result = autoProcessTV().processEpisode(inputDirectory, inputName, 0, clientAgent=clientAgent, inputCategory=inputCategory)
if result != 0: if result != 0:
logger.error("A problem was reported in the autoProcessTV script.") logger.error("A problem was reported in the autoProcessTV script.")
@ -67,7 +67,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
processOnly.extend(nzbtomedia.USER_SCRIPT_CATEGORIES) # Adds all categories to be processed by userscript. processOnly.extend(nzbtomedia.USER_SCRIPT_CATEGORIES) # Adds all categories to be processed by userscript.
if not inputCategory in processOnly: if not inputCategory in processOnly:
logger.postprocess("No processing to be done for category: %s. Exiting" % (inputCategory)) logger.info("No processing to be done for category: %s. Exiting" % (inputCategory))
return return
logger.debug("Scanning files in directory: %s" % (inputDirectory)) logger.debug("Scanning files in directory: %s" % (inputDirectory))
@ -99,7 +99,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
if root == 1: if root == 1:
if foundFile == int(0): if foundFile == int(0):
logger.debug("Looking for %s in: %s" % (inputName, file)) logger.debug("Looking for %s in: %s" % (inputName, file))
if (safeName(inputName) in safeName(file)) or (safeName(fileName) in safeName(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
foundFile = 1 foundFile = 1
logger.debug("Found file %s that matches Torrent Name %s" % (file, inputName)) logger.debug("Found file %s that matches Torrent Name %s" % (file, inputName))
@ -120,7 +120,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
if fileExtension in nzbtomedia.MEDIACONTAINER and is_sample(filePath, inputName, nzbtomedia.MINSAMPLESIZE, if fileExtension in nzbtomedia.MEDIACONTAINER and is_sample(filePath, inputName, nzbtomedia.MINSAMPLESIZE,
nzbtomedia.SAMPLEIDS) and not nzbtomedia.CFG["HeadPhones"][inputCategory]: # Ignore samples nzbtomedia.SAMPLEIDS) and not nzbtomedia.CFG["HeadPhones"][inputCategory]: # Ignore samples
logger.postprocess("Ignoring sample file: %s " % (filePath)) logger.info("Ignoring sample file: %s " % (filePath))
continue continue
if fileExtension in nzbtomedia.COMPRESSEDCONTAINER: if fileExtension in nzbtomedia.COMPRESSEDCONTAINER:
@ -133,7 +133,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
else: else:
logger.debug("Found part %s of a multi-part archive %s. Ignoring" % (part, file)) logger.debug("Found part %s of a multi-part archive %s. Ignoring" % (part, file))
continue continue
logger.postprocess("Found compressed archive %s for file %s" % (fileExtension, filePath)) logger.info("Found compressed archive %s for file %s" % (fileExtension, filePath))
try: try:
extractor.extract(filePath, outputDestination) 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) 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)
@ -176,7 +176,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
logger.warning("Found no media files in output.") logger.warning("Found no media files in output.")
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.postprocess("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 == int(0) or (nzbtomedia.CFG['HeadPhones','Mylar','Gamez'][inputCategory]): # if movies linked/extracted or for other categories.
logger.debug("Calling autoProcess script for successful download.") logger.debug("Calling autoProcess script for successful download.")
@ -187,23 +187,23 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
result = 0 result = 0
if nzbtomedia.CFG['CouchPotato'][inputCategory]: if nzbtomedia.CFG['CouchPotato'][inputCategory]:
logger.postprocess("Calling CouchPotato:" + inputCategory + " to post-process: %s" % (inputName)) logger.info("Calling CouchPotato:" + inputCategory + " to post-process: %s" % (inputName))
download_id = inputHash download_id = inputHash
result = autoProcessMovie().process(outputDestination, inputName, status, clientAgent, download_id, inputCategory) result = autoProcessMovie().process(outputDestination, inputName, status, clientAgent, download_id, inputCategory)
elif nzbtomedia.CFG['SickBeard'][inputCategory]: elif nzbtomedia.CFG['SickBeard'][inputCategory]:
logger.postprocess("Calling Sick-Beard:" + inputCategory + " to post-process: %s" % (inputName)) logger.info("Calling Sick-Beard:" + 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['NzbDrone'][inputCategory]: elif nzbtomedia.CFG['NzbDrone'][inputCategory]:
logger.postprocess("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]:
logger.postprocess("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]:
logger.postprocess("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]:
logger.postprocess("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 == 1 and clientAgent != 'manual':
@ -255,16 +255,16 @@ def external_script(outputDestination, torrentName, torrentLabel):
cmd = "" cmd = ""
for item in command: for item in command:
cmd = cmd + " " + item cmd = cmd + " " + item
logger.postprocess("Running script %s on file %s." % (cmd, filePath)) logger.info("Running script %s on file %s." % (cmd, filePath))
try: try:
p = Popen(command) p = Popen(command)
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.postprocess("UserScript %s was successfull" % (command[0])) logger.info("UserScript %s was successfull" % (command[0]))
result = int(0) result = int(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.postprocess("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))
result = int(1) result = int(1)
except: except:
logger.error("UserScript %s has failed" % (command[0])) logger.error("UserScript %s has failed" % (command[0]))
@ -282,10 +282,10 @@ def external_script(outputDestination, torrentName, torrentLabel):
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 == int(0) and final_result == int(0):
logger.postprocess("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 != int(0):
logger.postprocess("%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
def main(args): def main(args):
@ -295,9 +295,9 @@ def main(args):
# clientAgent for Torrents # clientAgent for Torrents
clientAgent = nzbtomedia.TORRENT_CLIENTAGENT clientAgent = nzbtomedia.TORRENT_CLIENTAGENT
logger.postprocess("#########################################################") logger.info("#########################################################")
logger.postprocess("## ..::[%s]::.. CLIENT:%s ## STARTING" % (args[0], clientAgent)) logger.info("## ..::[%s]::.. CLIENT:%s ## STARTING" % (args[0], clientAgent))
logger.postprocess("#########################################################") logger.info("#########################################################")
# debug command line options # debug command line options
logger.debug("Options passed into TorrentToMedia: %s" % (args)) logger.debug("Options passed into TorrentToMedia: %s" % (args))
@ -325,7 +325,7 @@ def main(args):
if nzbtomedia.CFG[section][category].isenabled(): if nzbtomedia.CFG[section][category].isenabled():
dirNames = get_dirnames(section, category) dirNames = get_dirnames(section, category)
for dirName in dirNames: for dirName in dirNames:
logger.postprocess("Running %s:%s as a manual run for folder %s ..." % (section, category, dirName)) logger.info("Running %s:%s as a manual run for folder %s ..." % (section, category, dirName))
results = processTorrent(dirName, os.path.basename(dirName), category, inputHash, inputID, clientAgent) results = processTorrent(dirName, os.path.basename(dirName), category, inputHash, inputID, clientAgent)
if results != 0: if results != 0:
result = results result = results
@ -334,7 +334,7 @@ def main(args):
logger.warning("%s:%s is DISABLED, you can enable this in autoProcessMedia.cfg ..." % (section, category)) logger.warning("%s:%s is DISABLED, you can enable this in autoProcessMedia.cfg ..." % (section, category))
if result == 0: if result == 0:
logger.postprocess("The %s script completed successfully." % (args[0])) logger.info("The %s script completed successfully." % (args[0]))
else: else:
logger.error("A problem was reported in the %s script." % (args[0])) logger.error("A problem was reported in the %s script." % (args[0]))

View file

@ -224,11 +224,10 @@ class autoProcessMovie:
timeout = time.time() + 60 * wait_for timeout = time.time() + 60 * wait_for
while (time.time() < timeout): # only wait 2 (default) minutes, then return. while (time.time() < timeout): # only wait 2 (default) minutes, then return.
releases_current = self.get_releases(baseURL, imdbid, download_id) releases_current = self.get_releases(baseURL, imdbid, download_id)
releasesDiff = self.releases_diff(releases, releases_current)
logger.postprocess("Checking for status change, please stand by ...", section) logger.postprocess("Checking for status change, please stand by ...", section)
if releasesDiff: # Something has changed. CPS must have processed this movie. if len(releases) > len(releases_current): # Something has changed. CPS must have processed this movie.
try: try:
release_status = releasesDiff[releasesDiff.keys()[0]]['status'] release_status = releases_current['status']
logger.postprocess("SUCCESS: Release %s has now been marked with a status of [%s]" % ( logger.postprocess("SUCCESS: Release %s has now been marked with a status of [%s]" % (
nzbName, str(release_status).upper()), section) nzbName, str(release_status).upper()), section)
return 0 # success return 0 # success

View file

@ -15,9 +15,26 @@ from nzbtomedia.utorrent.client import UTorrentClient
from nzbtomedia.transmissionrpc.client import Client as TransmissionClient from nzbtomedia.transmissionrpc.client import Client as TransmissionClient
def safeName(name): def sanitizeFileName(name):
return re.sub(r"[\/\\\:\*\?\"\<\>\|]", "", name) #make this name safe for use in directories for windows etc. '''
>>> sanitizeFileName('a/b/c')
'a-b-c'
>>> sanitizeFileName('abc')
'abc'
>>> sanitizeFileName('a"b')
'ab'
>>> sanitizeFileName('.a.b..')
'a.b'
'''
# remove bad chars from the filename
name = re.sub(r'[\\/\*]', '-', name)
name = re.sub(r'[:"<>|?]', '', name)
# remove leading/trailing periods and spaces
name = name.strip(' .')
return name
def makeDir(path): def makeDir(path):
if not os.path.isdir(path): if not os.path.isdir(path):
@ -59,10 +76,10 @@ def category_search(inputDirectory, inputName, inputCategory, root, categories):
inputDirectory = os.path.join(inputDirectory, inputName) inputDirectory = os.path.join(inputDirectory, inputName)
logger.info("SEARCH: Setting inputDirectory to %s" % (inputDirectory)) logger.info("SEARCH: Setting inputDirectory to %s" % (inputDirectory))
tordir = True tordir = True
if inputName and os.path.isdir(os.path.join(inputDirectory, safeName(inputName))): if inputName and os.path.isdir(os.path.join(inputDirectory, sanitizeFileName(inputName))):
logger.info("SEARCH: Found torrent directory %s in input directory directory %s" % ( logger.info("SEARCH: Found torrent directory %s in input directory directory %s" % (
safeName(inputName), inputDirectory)) sanitizeFileName(inputName), inputDirectory))
inputDirectory = os.path.join(inputDirectory, safeName(inputName)) inputDirectory = os.path.join(inputDirectory, sanitizeFileName(inputName))
logger.info("SEARCH: Setting inputDirectory to %s" % (inputDirectory)) logger.info("SEARCH: Setting inputDirectory to %s" % (inputDirectory))
tordir = True tordir = True
@ -82,7 +99,7 @@ def category_search(inputDirectory, inputName, inputCategory, root, categories):
pass pass
if inputName and not tordir: if inputName and not tordir:
if inputName in pathlist or safeName(inputName) in pathlist: if inputName in pathlist or sanitizeFileName(inputName) in pathlist:
logger.info("SEARCH: Found torrent directory %s in the directory structure" % (inputName)) logger.info("SEARCH: Found torrent directory %s in the directory structure" % (inputName))
tordir = True tordir = True
else: else:
@ -617,7 +634,7 @@ def find_imdbid(dirName, nzbName):
m = re.search('(tt\d{7})', dirName) m = re.search('(tt\d{7})', dirName)
if m: if m:
imdbid = m.group(1) imdbid = m.group(1)
logger.info("Found movie id %s in directory" % imdbid) logger.info("Found imdbID %s in directory" % imdbid)
return imdbid return imdbid
# find imdbid in nzbName # find imdbid in nzbName
@ -625,7 +642,7 @@ def find_imdbid(dirName, nzbName):
m = re.search('(tt\d{7})', nzbName) m = re.search('(tt\d{7})', nzbName)
if m: if m:
imdbid = m.group(1) imdbid = m.group(1)
logger.info("Found imdbid %s in name" % imdbid) logger.info("Found imdbID %s in filename" % imdbid)
return imdbid return imdbid
logger.info('Searching IMDB for imdbID ...') logger.info('Searching IMDB for imdbID ...')
@ -652,6 +669,7 @@ def find_imdbid(dirName, nzbName):
pass pass
if imdbid: if imdbid:
logger.info("Found imdbID %s on IMDB" % imdbid)
return imdbid return imdbid
else: else:
logger.warning('Unable to find a imdbID for %s' % (nzbName)) logger.warning('Unable to find a imdbID for %s' % (nzbName))