Cleaned up code and fixed a few small bugs.

This commit is contained in:
echel0n 2014-04-24 21:23:10 -07:00
commit 289d451112
11 changed files with 274 additions and 286 deletions

View file

@ -9,12 +9,11 @@ import nzbtomedia
from subprocess import Popen from subprocess import Popen
from nzbtomedia import logger, nzbToMediaDB from nzbtomedia import logger, nzbToMediaDB
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent): def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
status = 1 # 1 = failed | 0 = success status = 1 # 1 = failed | 0 = success
root = 0 root = 0
video = 0
foundFile = 0 foundFile = 0
copy_list = []
if clientAgent != 'manual': if clientAgent != 'manual':
logger.debug('Adding TORRENT download info for directory %s to database' % (inputDirectory)) logger.debug('Adding TORRENT download info for directory %s to database' % (inputDirectory))
@ -33,39 +32,56 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
logger.debug("Received Directory: %s | Name: %s | Category: %s" % (inputDirectory, inputName, inputCategory)) logger.debug("Received Directory: %s | Name: %s | Category: %s" % (inputDirectory, inputName, inputCategory))
inputDirectory, inputName, inputCategory, root, single = nzbtomedia.category_search(inputDirectory, inputName, inputCategory, root, nzbtomedia.CATEGORIES) # Confirm the category by parsing directory structure inputDirectory, inputName, inputCategory, root, single = nzbtomedia.category_search(inputDirectory, inputName,
inputCategory, root,
nzbtomedia.CATEGORIES) # Confirm the category by parsing directory structure
logger.debug("Determined Directory: %s | Name: %s | Category: %s" % (inputDirectory, inputName, inputCategory)) logger.debug("Determined Directory: %s | Name: %s | Category: %s" % (inputDirectory, inputName, inputCategory))
# Add torrent info hash to folder name incase we need it later on # auto-detect section
section = nzbtomedia.CFG.findsection(inputCategory) section = nzbtomedia.CFG.findsection(inputCategory)
if not section: if len(section) > 1:
logger.error( logger.error(
"We could not find a section with containing a download category labeled %s in your autoProcessMedia.cfg, Exiting!" % inputCategory) 'Category:[%s] is not unique, %s are using it. Please rename it or disable all other sections using the same category name in your autoProcessMedia.cfg and try again.' % (
inputCategory, section.keys()))
return -1 return -1
try:Torrent_NoLink = int(nzbtomedia.CFG[section][inputCategory]["Torrent_NoLink"]) if section:
except:Torrent_NoLink = 0 sectionName = section.keys()[0]
logger.info('Auto-detected SECTION:%s' % (sectionName))
else:
logger.error("Unable to locate a section with subsection:%s enabled in your autoProcessMedia.cfg, exiting!" % (
inputCategory))
return -1
try:extract = int(nzbtomedia.CFG[section][inputCategory]['extract']) try:
except:extract = 0 Torrent_NoLink = int(section[inputCategory]["Torrent_NoLink"])
except:
Torrent_NoLink = 0
try:
extract = int(section[inputCategory]['extract'])
except:
extract = 0
if clientAgent != 'manual': if clientAgent != 'manual':
nzbtomedia.pause_torrent(clientAgent, inputHash, inputID, inputName) nzbtomedia.pause_torrent(clientAgent, inputHash, inputID, inputName)
processCategories = nzbtomedia.CFG[nzbtomedia.SECTIONS].sections processCategories = nzbtomedia.CATEGORIES
processOnly = processCategories
if inputCategory == "": if inputCategory == "":
inputCategory = "UNCAT" inputCategory = "UNCAT"
outputDestination = os.path.normpath(nzbtomedia.os.path.join(nzbtomedia.OUTPUTDIRECTORY, inputCategory, nzbtomedia.sanitizeName(inputName)))
outputDestination = os.path.normpath(
nzbtomedia.os.path.join(nzbtomedia.OUTPUTDIRECTORY, inputCategory, nzbtomedia.sanitizeName(inputName)))
logger.info("Output directory set to: %s" % (outputDestination)) logger.info("Output directory set to: %s" % (outputDestination))
processOnly = nzbtomedia.CFG[nzbtomedia.SECTIONS].sections if not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES: # if None, we only process the 5 listed.
if not "NONE" in nzbtomedia.USER_SCRIPT_CATEGORIES: # if None, we only process the 5 listed. if "ALL" in nzbtomedia.USER_SCRIPT_CATEGORIES: # All defined categories
if "ALL" in nzbtomedia.USER_SCRIPT_CATEGORIES: # All defined categories
processOnly = nzbtomedia.CATEGORIES processOnly = nzbtomedia.CATEGORIES
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.info("No processing to be done for category: %s. Exiting" % (inputCategory)) logger.info("No processing to be done for category: %s. Exiting" % (inputCategory))
@ -73,8 +89,9 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
logger.debug("Scanning files in directory: %s" % (inputDirectory)) logger.debug("Scanning files in directory: %s" % (inputDirectory))
if nzbtomedia.CFG["HeadPhones"][inputCategory]: if sectionName == 'HeadPhones':
nzbtomedia.NOFLATTEN.extend(nzbtomedia.CFG["HeadPhones"].sections) # Make sure we preserve folder structure for HeadPhones. nzbtomedia.NOFLATTEN.extend(
section.sections) # Make sure we preserve folder structure for HeadPhones.
now = datetime.datetime.now() now = datetime.datetime.now()
@ -88,13 +105,16 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
targetFile = nzbtomedia.os.path.join(outputDestination, fullFileName) targetFile = nzbtomedia.os.path.join(outputDestination, fullFileName)
if inputCategory in nzbtomedia.NOFLATTEN: if inputCategory in nzbtomedia.NOFLATTEN:
if not os.path.basename(filePath) in outputDestination: if not os.path.basename(filePath) in outputDestination:
targetFile = nzbtomedia.os.path.join(nzbtomedia.os.path.join(outputDestination, os.path.basename(filePath)), fullFileName) targetFile = nzbtomedia.os.path.join(
logger.debug("Setting outputDestination to %s to preserve folder structure" % (os.path.dirname(targetFile))) nzbtomedia.os.path.join(outputDestination, os.path.basename(filePath)), fullFileName)
logger.debug(
"Setting outputDestination to %s to preserve folder structure" % (os.path.dirname(targetFile)))
if root == 1: if root == 1:
if not foundFile: if not foundFile:
logger.debug("Looking for %s in: %s" % (inputName, fullFileName)) logger.debug("Looking for %s in: %s" % (inputName, fullFileName))
if (nzbtomedia.sanitizeName(inputName) in nzbtomedia.sanitizeName(fullFileName)) or (nzbtomedia.sanitizeName(fileName) in nzbtomedia.sanitizeName(inputName)): if (nzbtomedia.sanitizeName(inputName) in nzbtomedia.sanitizeName(fullFileName)) or (
nzbtomedia.sanitizeName(fileName) in nzbtomedia.sanitizeName(inputName)):
foundFile = True foundFile = True
logger.debug("Found file %s that matches Torrent Name %s" % (fullFileName, inputName)) logger.debug("Found file %s that matches Torrent Name %s" % (fullFileName, inputName))
else: else:
@ -107,7 +127,6 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
if not foundFile: if not foundFile:
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.")
if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)): 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 foundFile = True
logger.debug("Found file %s with date modifed/created less than 5 minutes ago." % (fullFileName)) logger.debug("Found file %s with date modifed/created less than 5 minutes ago." % (fullFileName))
else: else:
@ -120,7 +139,7 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
except: except:
logger.error("Failed to link: %s to %s" % (inputFile, targetFile)) logger.error("Failed to link: %s to %s" % (inputFile, targetFile))
if not inputCategory in nzbtomedia.NOFLATTEN: #don't flatten hp in case multi cd albums, and we need to copy this back later. if not inputCategory in nzbtomedia.NOFLATTEN: #don't flatten hp in case multi cd albums, and we need to copy this back later.
nzbtomedia.flatten(outputDestination) nzbtomedia.flatten(outputDestination)
if extract == 1: if extract == 1:
@ -128,8 +147,9 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
nzbtomedia.extractFiles(outputDestination) nzbtomedia.extractFiles(outputDestination)
# Now check if video files exist in destination: # Now check if video files exist in destination:
if nzbtomedia.CFG["SickBeard","NzbDrone", "CouchPotato"][inputCategory]: if sectionName in ["SickBeard", "NzbDrone", "CouchPotato"]:
numVideos = len(nzbtomedia.listMediaFiles(outputDestination, media=True, audio=False, meta=False, archives=False)) numVideos = len(
nzbtomedia.listMediaFiles(outputDestination, media=True, audio=False, meta=False, archives=False))
if numVideos > 0: if numVideos > 0:
logger.info("Found %s media files in %s" % (numVideos, outputDestination)) logger.info("Found %s media files in %s" % (numVideos, outputDestination))
status = 0 status = 0
@ -137,43 +157,41 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
logger.warning("Found no media files in %s" % outputDestination) logger.warning("Found no media files in %s" % outputDestination)
# Only these sections can handling failed downloads so make sure everything else gets through without the check for failed # Only these sections can handling failed downloads so make sure everything else gets through without the check for failed
if not nzbtomedia.CFG['CouchPotato','SickBeard','NzbDrone'][inputCategory]: if not sectionName in ['CouchPotato', 'SickBeard', 'NzbDrone']:
status = 0 status = 0
result = 0 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 != 0: elif status != 0:
logger.error("Something failed! Please check logs. Exiting") logger.error("Something failed! Please check logs. Exiting")
return status return status
logger.info("Calling %s:%s to post-process:%s" % (sectionName, inputCategory, inputName))
if nzbtomedia.CFG['CouchPotato'][inputCategory]: if sectionName == 'CouchPotato':
logger.info("Calling CouchPotato:" + inputCategory + " to post-process: %s" % (inputName)) result = nzbtomedia.autoProcessMovie().process(sectionName,outputDestination, inputName, status, clientAgent, inputHash,
result = nzbtomedia.autoProcessMovie().process(outputDestination, inputName, status, clientAgent, inputHash, inputCategory) inputCategory)
elif nzbtomedia.CFG['SickBeard'][inputCategory]: elif sectionName == 'SickBeard':
logger.info("Calling Sick-Beard:" + inputCategory + " to post-process: %s" % (inputName)) result = nzbtomedia.autoProcessTV().processEpisode(sectionName,outputDestination, inputName, status, clientAgent,
result = nzbtomedia.autoProcessTV().processEpisode(outputDestination, inputName, status, clientAgent, inputCategory) inputCategory)
elif nzbtomedia.CFG['NzbDrone'][inputCategory]: elif sectionName == 'NzbDrone':
logger.info("Calling NzbDrone:" + inputCategory + " to post-process: %s" % (inputName)) result = nzbtomedia.autoProcessTV().processEpisode(sectionName,outputDestination, inputName, status, clientAgent,
result = nzbtomedia.autoProcessTV().processEpisode(outputDestination, inputName, status, clientAgent, inputCategory) inputCategory)
elif nzbtomedia.CFG['HeadPhones'][inputCategory]: elif sectionName == 'HeadPhones':
status = 0 #Failed Handling Not Supported result = nzbtomedia.autoProcessMusic().process(sectionName,outputDestination, inputName, status, clientAgent, inputCategory)
logger.info("Calling HeadPhones:" + inputCategory + " to post-process: %s" % (inputName)) elif sectionName == 'Mylar':
result = nzbtomedia.autoProcessMusic().process(outputDestination, inputName, status, clientAgent, inputCategory) result = nzbtomedia.autoProcessComics().processEpisode(sectionName,outputDestination, inputName, status, clientAgent,
elif nzbtomedia.CFG['Mylar'][inputCategory]: inputCategory)
status = 0 #Failed Handling Not Supported elif sectionName == 'Gamez':
logger.info("Calling Mylar:" + inputCategory + " to post-process: %s" % (inputName)) result = nzbtomedia.autoProcessGames().process(sectionName,outputDestination, inputName, status, clientAgent, inputCategory)
result = nzbtomedia.autoProcessComics().processEpisode(outputDestination, inputName, status, clientAgent, inputCategory)
elif nzbtomedia.CFG['Gamez'][inputCategory]:
status = 0 #Failed Handling Not Supported
logger.info("Calling Gamez:" + inputCategory + " to post-process: %s" % (inputName))
result = nzbtomedia.autoProcessGames().process(outputDestination, inputName, status, clientAgent, inputCategory)
if result != 0: if result != 0:
if clientAgent != 'manual': if 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")
nzbtomedia.resume_torrent(clientAgent, inputHash, inputID, inputName) nzbtomedia.resume_torrent(clientAgent, inputHash, inputID, inputName)
else: else:
if clientAgent != 'manual': if clientAgent != 'manual':
@ -181,16 +199,16 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
nzbtomedia.update_downloadInfoStatus(inputName, 1) nzbtomedia.update_downloadInfoStatus(inputName, 1)
# remove torrent # remove torrent
nzbtomedia.remove_torrent(clientAgent,inputHash,inputID,inputName) nzbtomedia.remove_torrent(clientAgent, inputHash, inputID, inputName)
# cleanup our processing folders of any misc unwanted files and empty directories # cleanup our processing folders of any misc unwanted files and empty directories
nzbtomedia.cleanProcDirs() nzbtomedia.cleanProcDirs()
return result return result
def external_script(outputDestination, torrentName, torrentLabel):
final_result = 0 # start at 0. def external_script(outputDestination, torrentName, torrentLabel):
final_result = 0 # start at 0.
num_files = 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:
@ -200,7 +218,7 @@ def external_script(outputDestination, torrentName, torrentLabel):
if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or "ALL" in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS: if fileExtension in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS or "ALL" in nzbtomedia.USER_SCRIPT_MEDIAEXTENSIONS:
num_files = num_files + 1 num_files = num_files + 1
if nzbtomedia.USER_SCRIPT_RUNONCE == 1 and num_files > 1: # we have already run once, so just continue to get number of files. if nzbtomedia.USER_SCRIPT_RUNONCE == 1 and num_files > 1: # we have already run once, so just continue to get number of files.
continue continue
command = [nzbtomedia.USER_SCRIPT] command = [nzbtomedia.USER_SCRIPT]
for param in nzbtomedia.USER_SCRIPT_PARAM: for param in nzbtomedia.USER_SCRIPT_PARAM:
@ -232,12 +250,14 @@ def external_script(outputDestination, torrentName, torrentLabel):
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.info("UserScript %s was successfull" % (command[0])) logger.info("UserScript %s was successfull" % (command[0]))
result = 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))
result = int(1) result = int(1)
except: except:
logger.error("UserScript %s has failed" % (command[0])) logger.error("UserScript %s has failed" % (command[0]))
@ -258,9 +278,11 @@ def external_script(outputDestination, torrentName, torrentLabel):
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 != 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
def main(args): def main(args):
# Initialize the config # Initialize the config
nzbtomedia.initialize() nzbtomedia.initialize()
@ -269,7 +291,7 @@ def main(args):
clientAgent = nzbtomedia.TORRENT_CLIENTAGENT clientAgent = nzbtomedia.TORRENT_CLIENTAGENT
logger.info("#########################################################") logger.info("#########################################################")
logger.info("## ..::[%s]::.. CLIENT:%s ## STARTING" % (args[0], clientAgent)) logger.info("## ..::[%s]::.. ##" % os.path.basename(__file__))
logger.info("#########################################################") logger.info("#########################################################")
# debug command line options # debug command line options
@ -292,36 +314,42 @@ def main(args):
for section, subsection in nzbtomedia.SUBSECTIONS.items(): for section, subsection in nzbtomedia.SUBSECTIONS.items():
for category in subsection: for category in subsection:
if nzbtomedia.CFG[section][category].isenabled(): for dirName in nzbtomedia.getDirs(subsection[category]):
dirNames = nzbtomedia.getDirs(section, category) logger.info("Starting manual run for %s:%s - Folder:%s" % (section, category, dirName))
for dirName in dirNames:
logger.info("Checking database for download info for %s ..." % (os.path.basename(dirName)))
downloadInfo = nzbtomedia.get_downloadInfo(os.path.basename(dirName), 0)
if downloadInfo:
logger.info(
"Found download info for %s, setting variables now ..." % (os.path.basename(dirName)))
else:
logger.info(
'Unable to locate download info for %s, continuing to try and process this release ...' % (
os.path.basename(dirName))
)
try:
clientAgent = str(downloadInfo[0]['client_agent'])
except:
clientAgent = 'manual' clientAgent = 'manual'
try:
inputHash = str(downloadInfo[0]['input_hash'])
except:
inputHash = None inputHash = None
try:
inputID = str(downloadInfo[0]['input_id'])
except:
inputID = None inputID = None
logger.info("Checking database for download info for %s ..." % (os.path.basename(dirName))) if not clientAgent.lower() in (nzbtomedia.TORRENT_CLIENTS or 'manual'):
downloadInfo = nzbtomedia.get_downloadInfo(os.path.basename(dirName), 0) continue
if downloadInfo:
clientAgent = str(downloadInfo[0]['client_agent'])
if not clientAgent.lower() in nzbtomedia.TORRENT_CLIENTS:
continue
inputHash = str(downloadInfo[0]['input_hash']) results = processTorrent(dirName, os.path.basename(dirName), category, inputHash, inputID,
inputID = str(downloadInfo[0]['input_id']) clientAgent)
logger.info("Found download info for %s, setting variables now ..." % (os.path.basename(dirName))) if results != 0:
logger.error("A problem was reported when trying to perform a manual run for %s:%s." % (
section, category))
logger.info("Running %s:%s as a manual run for folder %s ..." % (section, category, dirName)) result = results
results = processTorrent(dirName, os.path.basename(dirName), category, inputHash, inputID, clientAgent)
if results != 0:
result = results
logger.error("A problem was reported when trying to manually run %s:%s." % (section, category))
if len(dirNames) == 0:
logger.info('[%s] - No directories found to post-process ...' % (str(category).upper()),
section)
else:
logger.warning("%s:%s is DISABLED, you can enable this in autoProcessMedia.cfg ..." % (section, category))
if result == 0: if result == 0:
logger.info("The %s script completed successfully." % (args[0])) logger.info("The %s script completed successfully." % (args[0]))
@ -330,5 +358,6 @@ def main(args):
return result return result
if __name__ == "__main__": if __name__ == "__main__":
exit(main(sys.argv)) exit(main(sys.argv))

View file

@ -304,30 +304,45 @@ def process(inputDirectory, inputName=None, status=0, clientAgent='manual', down
# auto-detect section # auto-detect section
section = nzbtomedia.CFG.findsection(inputCategory) section = nzbtomedia.CFG.findsection(inputCategory)
if len(section) > 1:
logger.error(
'Category:[%s] is not unique, %s are using it. Please rename it or disable all other sections using the same category name in your autoProcessMedia.cfg and try again.' % (
inputCategory, section.keys()))
return -1
if section: if section:
try:extract = int(nzbtomedia.CFG[section][inputCategory]['extract']) sectionName = section.keys()[0]
except:extract = 0 logger.info('Auto-detected SECTION:%s' % (sectionName))
else:
if extract == 1: logger.error("Unable to locate a section with subsection:%s enabled in your autoProcessMedia.cfg, exiting!" % (
logger.debug('Checking for archives to extract in directory: %s' % (inputDirectory)) inputCategory))
extractFiles(inputDirectory) return -1
logger.info("Sending %s to %s for post-processing ..." % (inputName, str(section).upper())) try:
extract = int(section[inputCategory]['extract'])
if nzbtomedia.CFG["CouchPotato"][inputCategory]: except:
result = autoProcessMovie().process(inputDirectory, inputName, status, clientAgent, download_id, inputCategory) extract = 0
elif nzbtomedia.CFG["SickBeard", "NzbDrone"][inputCategory]:
result = autoProcessTV().processEpisode(inputDirectory, inputName, status, clientAgent, inputCategory) if extract == 1:
elif nzbtomedia.CFG["HeadPhones"][inputCategory]: logger.debug('Checking for archives to extract in directory: %s' % (inputDirectory))
result = autoProcessMusic().process(inputDirectory, inputName, status, clientAgent, inputCategory) extractFiles(inputDirectory)
elif nzbtomedia.CFG["Mylar"][inputCategory]:
result = autoProcessComics().processEpisode(inputDirectory, inputName, status, clientAgent, inputCategory) logger.info("Calling %s:%s to post-process:%s" % (sectionName, inputCategory, inputName))
elif nzbtomedia.CFG["Gamez"][inputCategory]:
result = autoProcessGames().process(inputDirectory, inputName, status, clientAgent, inputCategory) if nzbtomedia.CFG["CouchPotato"][inputCategory]:
else: result = autoProcessMovie().process(sectionName, inputDirectory, inputName, status, clientAgent, download_id,
result = -1 inputCategory)
elif nzbtomedia.CFG["SickBeard", "NzbDrone"][inputCategory]:
result = autoProcessTV().processEpisode(sectionName, inputDirectory, inputName, status, clientAgent,
inputCategory)
elif nzbtomedia.CFG["HeadPhones"][inputCategory]:
result = autoProcessMusic().process(sectionName, inputDirectory, inputName, status, clientAgent, inputCategory)
elif nzbtomedia.CFG["Mylar"][inputCategory]:
result = autoProcessComics().processEpisode(sectionName, inputDirectory, inputName, status, clientAgent,
inputCategory)
elif nzbtomedia.CFG["Gamez"][inputCategory]:
result = autoProcessGames().process(sectionName, inputDirectory, inputName, status, clientAgent, inputCategory)
else: else:
logger.error("We could not find a section with containing a download category labeled %s in your autoProcessMedia.cfg, Exiting!" % inputCategory)
result = -1 result = -1
if result == 0: if result == 0:
@ -340,6 +355,7 @@ def process(inputDirectory, inputName=None, status=0, clientAgent='manual', down
return result return result
def main(args, section=None): def main(args, section=None):
# Initialize the config # Initialize the config
nzbtomedia.initialize(section) nzbtomedia.initialize(section)
@ -386,17 +402,20 @@ def main(args, section=None):
# Unpack was skipped due to nzb-file properties or due to errors during par-check # Unpack was skipped due to nzb-file properties or due to errors during par-check
if os.environ['NZBPP_HEALTH'] < 1000: if os.environ['NZBPP_HEALTH'] < 1000:
logger.warning("Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \"failed\"") logger.warning(
"Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \"failed\"")
logger.info("Please check your Par-check/repair settings for future downloads.") logger.info("Please check your Par-check/repair settings for future downloads.")
status = 1 status = 1
else: else:
logger.info("Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful") logger.info(
"Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful")
logger.info("Please check your Par-check/repair settings for future downloads.") logger.info("Please check your Par-check/repair settings for future downloads.")
# Check if destination directory exists (important for reprocessing of history items) # Check if destination directory exists (important for reprocessing of history items)
if not os.path.isdir(os.environ['NZBPP_DIRECTORY']): if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
logger.error("Nothing to post-process: destination directory %s doesn't exist. Setting status failed" % (os.environ['NZBPP_DIRECTORY'])) logger.error("Nothing to post-process: destination directory %s doesn't exist. Setting status failed" % (
os.environ['NZBPP_DIRECTORY']))
status = 1 status = 1
# Check for download_id to pass to CouchPotato # Check for download_id to pass to CouchPotato
@ -406,7 +425,8 @@ def main(args, section=None):
# All checks done, now launching the script. # All checks done, now launching the script.
clientAgent = 'nzbget' clientAgent = 'nzbget'
result = process(os.environ['NZBPP_DIRECTORY'], inputName=os.environ['NZBPP_NZBFILENAME'], status=status, clientAgent=clientAgent, download_id=download_id, inputCategory=os.environ['NZBPP_CATEGORY']) result = process(os.environ['NZBPP_DIRECTORY'], inputName=os.environ['NZBPP_NZBFILENAME'], status=status,
clientAgent=clientAgent, download_id=download_id, inputCategory=os.environ['NZBPP_CATEGORY'])
# SABnzbd Pre 0.7.17 # SABnzbd Pre 0.7.17
elif len(args) == nzbtomedia.SABNZB_NO_OF_ARGUMENTS: elif len(args) == nzbtomedia.SABNZB_NO_OF_ARGUMENTS:
# SABnzbd argv: # SABnzbd argv:
@ -419,7 +439,8 @@ def main(args, section=None):
# 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2 # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2
clientAgent = 'sabnzbd' clientAgent = 'sabnzbd'
logger.info("Script triggered from SABnzbd") logger.info("Script triggered from SABnzbd")
result = process(args[1], inputName=args[2], status=args[7], inputCategory=args[5], clientAgent=clientAgent, download_id='') result = process(args[1], inputName=args[2], status=args[7], inputCategory=args[5], clientAgent=clientAgent,
download_id='')
# SABnzbd 0.7.17+ # SABnzbd 0.7.17+
elif len(args) >= nzbtomedia.SABNZB_0717_NO_OF_ARGUMENTS: elif len(args) >= nzbtomedia.SABNZB_0717_NO_OF_ARGUMENTS:
# SABnzbd argv: # SABnzbd argv:
@ -433,51 +454,58 @@ def main(args, section=None):
# 8 Failure URL # 8 Failure URL
clientAgent = 'sabnzbd' clientAgent = 'sabnzbd'
logger.info("Script triggered from SABnzbd 0.7.17+") logger.info("Script triggered from SABnzbd 0.7.17+")
result = process(args[1], inputName=args[2], status=args[7], inputCategory=args[5], clientAgent=clientAgent, download_id='') result = process(args[1], inputName=args[2], status=args[7], inputCategory=args[5], clientAgent=clientAgent,
download_id='')
else: else:
# Perform Manual Post-Processing # Perform Manual Post-Processing
logger.warning("Invalid number of arguments received from client, Switching to manual run mode ...") logger.warning("Invalid number of arguments received from client, Switching to manual run mode ...")
for section, subsection in nzbtomedia.SUBSECTIONS.items(): for section, subsection in nzbtomedia.SUBSECTIONS.items():
for category in subsection: for category in subsection:
if nzbtomedia.CFG[section][category].isenabled(): for dirName in getDirs(subsection[category]):
dirNames = getDirs(section, category) logger.info("Starting manual run for %s:%s - Folder:%s" % (section, category, dirName))
for dirName in dirNames:
logger.info("Checking database for download info for %s ..." % (os.path.basename(dirName)))
downloadInfo = get_downloadInfo(os.path.basename(dirName), 0)
if downloadInfo:
logger.info(
"Found download info for %s, setting variables now ..." % (os.path.basename(dirName)))
else:
logger.info(
'Unable to locate download info for %s, continuing to try and process this release ...' % (
os.path.basename(dirName))
)
try:
clientAgent = str(downloadInfo[0]['client_agent'])
except:
clientAgent = 'manual' clientAgent = 'manual'
try:
download_id = str(downloadInfo[0]['input_id'])
except:
download_id = None download_id = None
logger.info("Checking database for download info for %s ..." % (os.path.basename(dirName))) if not clientAgent.lower() in (nzbtomedia.NZB_CLIENTS or 'manual'):
downloadInfo = get_downloadInfo(os.path.basename(dirName), 0) continue
if downloadInfo:
clientAgent = str(downloadInfo[0]['client_agent'])
if not clientAgent.lower() in nzbtomedia.NZB_CLIENTS:
continue
download_id = str(downloadInfo[0]['input_id']) results = process(dirName, os.path.basename(dirName), 0, clientAgent=clientAgent,
logger.info("Found download info for %s, setting variables now ..." % (os.path.basename(dirName))) download_id=download_id, inputCategory=category)
if results != 0:
logger.info("Starting manual run for %s:%s - Folder:%s" % (section, category, dirName)) logger.error("A problem was reported when trying to perform a manual run for %s:%s." % (
results = process(dirName, os.path.basename(dirName), 0, clientAgent=clientAgent, download_id=download_id, inputCategory=category) section, category))
if results != 0: result = results
logger.error("A problem was reported when trying to perform a manual run for %s:%s." % (section, category))
result = results
if len(dirNames) == 0:
logger.info('[%s] - No directories found to post-process ...' % (str(category).upper()),
section)
else:
logger.debug("nzbToMedia %s:%s is DISABLED" % (section, category))
if result == 0: if result == 0:
logger.info("The %s script completed successfully." % args[0]) logger.info("The %s script completed successfully." % args[0])
if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11 if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
return(nzbtomedia.NZBGET_POSTPROCESS_SUCCESS) return (nzbtomedia.NZBGET_POSTPROCESS_SUCCESS)
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])
if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11 if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
return(nzbtomedia.NZBGET_POSTPROCESS_ERROR) return (nzbtomedia.NZBGET_POSTPROCESS_ERROR)
return (result)
return(result)
if __name__ == '__main__': if __name__ == '__main__':
exit(main(sys.argv)) exit(main(sys.argv))

View file

@ -327,18 +327,16 @@ def initialize(section=None):
USER_SCRIPT_RUNONCE = int(CFG["UserScript"]["user_script_runOnce"]) USER_SCRIPT_RUNONCE = int(CFG["UserScript"]["user_script_runOnce"])
# check for script-defied section and if None set to allow sections # check for script-defied section and if None set to allow sections
SECTIONS = ("CouchPotato", "SickBeard", "NzbDrone", "HeadPhones", "Mylar", "Gamez") SECTIONS = tuple(x for x in CFG if CFG[x].sections) if not section else (section,)
if section: SECTIONS = (section,) SUBSECTIONS = CFG[SECTIONS].isenabled()
SUBSECTIONS = CFG[SECTIONS]
CATEGORIES += SUBSECTIONS.sections CATEGORIES += SUBSECTIONS.sections
# create torrent class # create torrent class
TORRENT_CLASS = create_torrent_class(TORRENT_CLIENTAGENT) TORRENT_CLASS = create_torrent_class(TORRENT_CLIENTAGENT)
# finished initalizing
return True return True
def restart(): def restart():
install_type = versionCheck.CheckVersion().install_type install_type = versionCheck.CheckVersion().install_type

View file

@ -6,14 +6,7 @@ from nzbtomedia.nzbToMediaUtil import convert_to_ascii
from nzbtomedia import logger from nzbtomedia import logger
class autoProcessComics: class autoProcessComics:
def processEpisode(self, dirName, inputName=None, status=0, clientAgent='manual', inputCategory=None): def processEpisode(self, section, dirName, inputName=None, status=0, clientAgent='manual', inputCategory=None):
# auto-detect correct section
section = nzbtomedia.CFG.findsection(inputCategory)
if not section:
logger.error(
"We were unable to find a section for category %s, please check your autoProcessMedia.cfg file." % inputCategory)
return 1
host = nzbtomedia.CFG[section][inputCategory]["host"] host = nzbtomedia.CFG[section][inputCategory]["host"]
port = nzbtomedia.CFG[section][inputCategory]["port"] port = nzbtomedia.CFG[section][inputCategory]["port"]
username = nzbtomedia.CFG[section][inputCategory]["username"] username = nzbtomedia.CFG[section][inputCategory]["username"]

View file

@ -4,18 +4,7 @@ from nzbtomedia.nzbToMediaUtil import convert_to_ascii
from nzbtomedia import logger from nzbtomedia import logger
class autoProcessGames: class autoProcessGames:
def process(self, dirName, inputName=None, status=0, clientAgent='manual', inputCategory=None): def process(self, section, dirName, inputName=None, status=0, clientAgent='manual', inputCategory=None):
if dirName is None:
logger.error("No directory was given!")
return 1 # failure
# auto-detect correct section
section = nzbtomedia.CFG.findsection(inputCategory)
if not section:
logger.error(
"We were unable to find a section for category %s, please check your autoProcessMedia.cfg file." % inputCategory)
return 1
status = int(status) status = int(status)
host = nzbtomedia.CFG[section][inputCategory]["host"] host = nzbtomedia.CFG[section][inputCategory]["host"]

View file

@ -87,14 +87,7 @@ class autoProcessMovie:
return results return results
def process(self, dirName, inputName=None, status=0, clientAgent="manual", download_id="", inputCategory=None): def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", download_id="", inputCategory=None):
# auto-detect correct section
section = nzbtomedia.CFG.findsection(inputCategory)
if not section:
logger.error(
"We were unable to find a section for category %s, please check your autoProcessMedia.cfg file." % inputCategory)
return 1
# Check video files for corruption # Check video files for corruption
status = int(status) status = int(status)
for video in listMediaFiles(dirName): for video in listMediaFiles(dirName):

View file

@ -29,14 +29,7 @@ class autoProcessMusic:
return album["Status"].lower() return album["Status"].lower()
except:pass except:pass
def process(self, dirName, inputName=None, status=0, clientAgent="manual", inputCategory=None): def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", inputCategory=None):
# auto-detect correct section
section = nzbtomedia.CFG.findsection(inputCategory)
if len(section) == 0:
logger.error(
"We were unable to find a section for category %s, please check your autoProcessMedia.cfg file." % (inputCategory))
return 1
status = int(status) status = int(status)
host = nzbtomedia.CFG[section][inputCategory]["host"] host = nzbtomedia.CFG[section][inputCategory]["host"]

View file

@ -10,14 +10,7 @@ from nzbtomedia import logger
from nzbtomedia.transcoder import transcoder from nzbtomedia.transcoder import transcoder
class autoProcessTV: class autoProcessTV:
def processEpisode(self, dirName, inputName=None, failed=False, clientAgent = "manual", inputCategory=None): def processEpisode(self, section, dirName, inputName=None, failed=False, clientAgent = "manual", inputCategory=None):
# auto-detect correct section
section = nzbtomedia.CFG.findsection(inputCategory)
if not section:
logger.error(
"We were unable to find a section for category %s, please check your autoProcessMedia.cfg file." % inputCategory)
return 1
# auto-detect correct fork # auto-detect correct fork
fork, fork_params = autoFork(inputCategory) fork, fork_params = autoFork(inputCategory)

View file

@ -5,61 +5,52 @@ from configobj import *
from itertools import chain from itertools import chain
class Sections(dict):
def isenabled(sections):
# checks if subsection enabled, returns true/false if subsection specified otherwise returns true/false in {}
to_return = False
for section, subsection in sections.items():
for item in subsection:
if int(subsection[item]['enabled']) == 1:
to_return = True
return to_return
@property
def sections(sections):
# returns [subsections]
to_return = []
for section, subsection in sections.items():
to_return.append(subsection)
return list(set(chain.from_iterable(to_return)))
def __getitem__(self, key):
# check for key in sections
if key in self:
return dict.__getitem__(self, key)
# check for key in subsections
to_return = Sections()
for section, subsection in self.items():
if key in subsection:
to_return.update({section:{key:dict.__getitem__(subsection, key)}})
return to_return
class Section(configobj.Section): class Section(configobj.Section):
def isenabled(section): def isenabled(section):
# checks if subsection enabled, returns true/false if subsection specified otherwise returns true/false in {} # checks if subsection enabled, returns true/false if subsection specified otherwise returns true/false in {}
if section: if not section.sections:
if int(section['enabled']) == 1: if not int(section['enabled']) == 1:
return True return
return False else:
for section_name, subsections in section.items():
if subsections.sections:
for subsection in subsections:
if not int(subsections[subsection]['enabled']) == 1:
subsections.pop(subsection)
else:
if not int(subsections['enabled']) == 1:
section.pop(section_name)
if len(section[section_name]) == 0:
section.pop(section_name)
return section
def findsection(section, key): def findsection(section, key):
for subsection in section: for subsection in section:
if key in section[subsection]: if key not in section[subsection]:
return subsection section.pop(subsection)
return section
def __getitem__(self, key): def __getitem__(self, key):
# check for key in section
if key in self.keys(): if key in self.keys():
return dict.__getitem__(self, key) return dict.__getitem__(self, key)
# check for key in subsection for section, subsections in self.items():
result = Sections() if section in key:
for section in key: continue
if section in self: if isinstance(subsections, Section) and subsections.sections:
subsection = dict.__getitem__(self, section) for subsection in subsections:
result.update({section: subsection}) if subsection in key:
return result continue
subsections.pop(subsection)
else:
if section not in key:
self.pop(section)
if len(subsections) == 0:
self.pop(section)
return self
class ConfigObj(configobj.ConfigObj, Section): class ConfigObj(configobj.ConfigObj, Section):
def __init__(self, *args, **kw): def __init__(self, *args, **kw):

View file

@ -197,24 +197,25 @@ def flatten(outputDestination):
removeEmptyFolders(outputDestination) # Cleanup empty directories removeEmptyFolders(outputDestination) # Cleanup empty directories
def removeEmptyFolders(path): def removeEmptyFolders(path, removeRoot=True):
logger.info("Removing empty folders in: %s" % (path), 'REMOVER') 'Function to remove empty folders'
if not os.path.isdir(path): if not os.path.isdir(path):
return return
# Remove empty subfolders # remove empty subfolders
files = os.listdir(path) logger.debug("Checking for empty folders in:%s" % (path))
if len(files): files = os.listdir(path)
for f in files: if len(files):
fullpath = os.path.join(path, f) for f in files:
if os.path.isdir(fullpath): fullpath = os.path.join(path, f)
removeEmptyFolders(fullpath) if os.path.isdir(fullpath):
removeEmptyFolders(fullpath)
# If folder empty, delete it # if folder empty, delete it
files = os.listdir(path) files = os.listdir(path)
if len(files) == 0: if len(files) == 0 and removeRoot:
logger.debug("Removing empty folder: %s" % (path), 'REMOVER') logger.debug("Removing empty folder:%s" % (path))
os.rmdir(path) os.rmdir(path)
def rmReadOnly(filename): def rmReadOnly(filename):
if os.path.isfile(filename): if os.path.isfile(filename):
@ -382,15 +383,10 @@ def parse_args(clientAgent, args):
return None, None, None, None, None return None, None, None, None, None
def getDirs(section, subsections=None): def getDirs(subsection):
subsectionName = subsection.keys()[0]
to_return = [] to_return = []
if subsections is None:
subsections = nzbtomedia.SUBSECTIONS[section].sections
if not isinstance(subsections, list):
subsections = [subsections]
def processDir(path): def processDir(path):
folders = [] folders = []
# search for single files and move them into there own folder for post-processing # search for single files and move them into there own folder for post-processing
@ -434,21 +430,23 @@ def getDirs(section, subsections=None):
# move file to its new path # move file to its new path
shutil.move(mediafile, newPath) shutil.move(mediafile, newPath)
folders.extend([os.path.join(path, o) for o in os.listdir(path) if removeEmptyFolders(path, removeRoot=False)
os.path.isdir(os.path.join(path, o))])
if os.listdir(path):
folders.extend([os.path.join(path, o) for o in os.listdir(path) if
os.path.isdir(os.path.join(path, o))])
return folders return folders
for subsection in subsections: watch_dir = subsection["watch_dir"]
watch_dir = os.path.abspath(nzbtomedia.CFG[section][subsection]["watch_dir"]) if os.path.exists(watch_dir):
if os.path.exists(watch_dir): to_return.extend(processDir(watch_dir))
to_return.extend(processDir(watch_dir))
outputDirectory = os.path.join(nzbtomedia.OUTPUTDIRECTORY, subsection) outputDirectory = os.path.join(nzbtomedia.OUTPUTDIRECTORY, subsectionName)
if os.path.exists(outputDirectory): if os.path.exists(outputDirectory):
to_return.extend(processDir(outputDirectory)) to_return.extend(processDir(outputDirectory))
if not to_return: if not to_return:
logger.debug("No directories identified in %s for post-processing" % (subsection), section) logger.debug("No directories identified in %s for post-processing" % (subsectionName))
return list(set(to_return)) return list(set(to_return))

View file

@ -7,23 +7,6 @@ from nzbtomedia.nzbToMediaUtil import get_downloadInfo
# Initialize the config # Initialize the config
nzbtomedia.initialize() nzbtomedia.initialize()
inputDirectory = 'Z:/complete/music/B.O.A.T.S. II_ Me Time [2013]' test = nzbtomedia.CFG['HeadPhones']['music']
outputDestination = 'Z:\\test\\music\\B.O.A.T.S. II_ Me Time [2013]' section = nzbtomedia.CFG.findsection('tv').isenabled()
outputDestinationMaster = outputDestination # Save the original, so we can change this within the loop below, and reset afterwards. print section
now = datetime.datetime.now()
for dirpath, dirnames, filenames in os.walk(inputDirectory):
for file in filenames:
filePath = os.path.join(dirpath, file)
fileName, fileExtension = os.path.splitext(file)
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.
targetDirectory = os.path.join(outputDestination, file)
outputDestination = outputDestinationMaster
nzbtomedia.flatten(outputDestination)