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.autoProcessTV import autoProcessTV
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
from nzbtomedia import logger
@ -43,13 +43,13 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
if inputCategory == "":
inputCategory = "UNCAT"
outputDestination = os.path.normpath(os.path.join(nzbtomedia.OUTPUTDIRECTORY, inputCategory, safeName(inputName)))
logger.postprocess("Output directory set to: %s" % (outputDestination))
outputDestination = os.path.normpath(os.path.join(nzbtomedia.OUTPUTDIRECTORY, inputCategory, sanitizeFileName(inputName)))
logger.info("Output directory set to: %s" % (outputDestination))
if nzbtomedia.CFG["SickBeard"][inputCategory]:
Torrent_NoLink = int(nzbtomedia.CFG["SickBeard"][inputCategory]["Torrent_NoLink"]) # 0
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)
if result != 0:
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.
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
logger.debug("Scanning files in directory: %s" % (inputDirectory))
@ -99,7 +99,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
if root == 1:
if foundFile == int(0):
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
foundFile = 1
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,
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
if fileExtension in nzbtomedia.COMPRESSEDCONTAINER:
@ -133,7 +133,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
else:
logger.debug("Found part %s of a multi-part archive %s. Ignoring" % (part, file))
continue
logger.postprocess("Found compressed archive %s for file %s" % (fileExtension, filePath))
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)
@ -176,7 +176,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
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):
logger.postprocess("Processing user script %s." % (nzbtomedia.USER_SCRIPT))
logger.info("Processing user script %s." % (nzbtomedia.USER_SCRIPT))
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.
logger.debug("Calling autoProcess script for successful download.")
@ -187,23 +187,23 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
result = 0
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
result = autoProcessMovie().process(outputDestination, inputName, status, clientAgent, download_id, 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)
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)
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)
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)
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)
if result == 1 and clientAgent != 'manual':
@ -255,16 +255,16 @@ def external_script(outputDestination, torrentName, torrentLabel):
cmd = ""
for item in command:
cmd = cmd + " " + item
logger.postprocess("Running script %s on file %s." % (cmd, filePath))
logger.info("Running script %s on file %s." % (cmd, filePath))
try:
p = Popen(command)
res = p.wait()
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)
else:
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)
except:
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
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)
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
def main(args):
@ -295,9 +295,9 @@ def main(args):
# clientAgent for Torrents
clientAgent = nzbtomedia.TORRENT_CLIENTAGENT
logger.postprocess("#########################################################")
logger.postprocess("## ..::[%s]::.. CLIENT:%s ## STARTING" % (args[0], clientAgent))
logger.postprocess("#########################################################")
logger.info("#########################################################")
logger.info("## ..::[%s]::.. CLIENT:%s ## STARTING" % (args[0], clientAgent))
logger.info("#########################################################")
# debug command line options
logger.debug("Options passed into TorrentToMedia: %s" % (args))
@ -325,7 +325,7 @@ def main(args):
if nzbtomedia.CFG[section][category].isenabled():
dirNames = get_dirnames(section, category)
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)
if results != 0:
result = results
@ -334,7 +334,7 @@ def main(args):
logger.warning("%s:%s is DISABLED, you can enable this in autoProcessMedia.cfg ..." % (section, category))
if result == 0:
logger.postprocess("The %s script completed successfully." % (args[0]))
logger.info("The %s script completed successfully." % (args[0]))
else:
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
while (time.time() < timeout): # only wait 2 (default) minutes, then return.
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)
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:
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]" % (
nzbName, str(release_status).upper()), section)
return 0 # success

View file

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