diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 699032ce..ea1eba67 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -312,7 +312,7 @@ def main(args): # Perform Manual Post-Processing 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.SECTIONS.items(): for category in subsection: for dirName in nzbtomedia.getDirs(subsection[category]): logger.info("Starting manual run for %s:%s - Folder:%s" % (section, category, dirName)) @@ -341,7 +341,7 @@ def main(args): except: inputID = None - if not clientAgent.lower() in (nzbtomedia.TORRENT_CLIENTS or 'manual'): + if not clientAgent.lower() in (nzbtomedia.TORRENT_CLIENTS,'manual'): continue results = processTorrent(dirName, os.path.basename(dirName), category, inputHash, inputID, diff --git a/nzbToMedia.py b/nzbToMedia.py index e0aa34d4..6d6239f2 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -460,7 +460,7 @@ def main(args, section=None): # Perform Manual Post-Processing 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.SECTIONS.items(): for category in subsection: for dirName in getDirs(subsection[category]): logger.info("Starting manual run for %s:%s - Folder:%s" % (section, category, dirName)) diff --git a/nzbtomedia/__init__.py b/nzbtomedia/__init__.py index 672b4940..884eb197 100644 --- a/nzbtomedia/__init__.py +++ b/nzbtomedia/__init__.py @@ -1,5 +1,6 @@ import locale import os +import re import subprocess import sys import platform @@ -111,7 +112,6 @@ MINSAMPLESIZE = None SAMPLEIDS = None SECTIONS = [] -SUBSECTIONS = {} TRANSCODE = None FFMPEG_PATH = None @@ -140,8 +140,9 @@ USER_SCRIPT_CLEAN = None USER_DELAY = None USER_SCRIPT_RUNONCE = None -__INITIALIZED__ = False +PASSWORDSFILE = None +__INITIALIZED__ = False def initialize(section=None): global NZBGET_POSTPROCESS_ERROR, NZBGET_POSTPROCESS_NONE, NZBGET_POSTPROCESS_PARCHECK, NZBGET_POSTPROCESS_SUCCESS, \ @@ -150,12 +151,13 @@ def initialize(section=None): SABNZB_NO_OF_ARGUMENTS, SABNZB_0717_NO_OF_ARGUMENTS, CATEGORIES, TORRENT_CLIENTAGENT, USELINK, OUTPUTDIRECTORY, NOFLATTEN, \ UTORRENTPWD, UTORRENTUSR, UTORRENTWEBUI, DELUGEHOST, DELUGEPORT, DELUGEUSR, DELUGEPWD, TRANSMISSIONHOST, TRANSMISSIONPORT, \ TRANSMISSIONPWD, TRANSMISSIONUSR, COMPRESSEDCONTAINER, MEDIACONTAINER, METACONTAINER, MINSAMPLESIZE, SAMPLEIDS, \ - SECTIONS, SUBSECTIONS, USER_SCRIPT_CATEGORIES, __INITIALIZED__, AUTO_UPDATE, APP_FILENAME, USER_DELAY, USER_SCRIPT_RUNONCE, \ + SECTIONS, USER_SCRIPT_CATEGORIES, __INITIALIZED__, AUTO_UPDATE, APP_FILENAME, USER_DELAY, USER_SCRIPT_RUNONCE, \ APP_NAME, USER_SCRIPT_MEDIAEXTENSIONS, USER_SCRIPT, USER_SCRIPT_PARAM, USER_SCRIPT_SUCCESSCODES, USER_SCRIPT_CLEAN, \ TRANSCODE, GIT_PATH, GIT_USER, GIT_BRANCH, GIT_REPO, SYS_ENCODING, NZB_CLIENTAGENT, SABNZBDHOST, SABNZBDPORT, SABNZBDAPIKEY, \ DUPLICATE, IGNOREEXTENSIONS, OUTPUTVIDEOEXTENSION, OUTPUTVIDEOCODEC, OUTPUTVIDEOPRESET, OUTPUTVIDEOFRAMERATE, LOG_DB, \ OUTPUTVIDEOBITRATE, OUTPUTAUDIOCODEC, OUTPUTAUDIOBITRATE, OUTPUTSUBTITLECODEC, OUTPUTFASTSTART, OUTPUTQUALITYPERCENT, \ - NICENESS, LOG_DEBUG, FORCE_CLEAN, FFMPEG_PATH, FFMPEG, FFPROBE, AUDIOCONTAINER, EXTCONTAINER, TORRENT_CLASS, DELETE_ORIGINAL + NICENESS, LOG_DEBUG, FORCE_CLEAN, FFMPEG_PATH, FFMPEG, FFPROBE, AUDIOCONTAINER, EXTCONTAINER, TORRENT_CLASS, DELETE_ORIGINAL,\ + PASSWORDSFILE if __INITIALIZED__: return False @@ -263,7 +265,10 @@ def initialize(section=None): DELUGEUSR = CFG["Torrent"]["DelugeUSR"] # mysecretusr DELUGEPWD = CFG["Torrent"]["DelugePWD"] # mysecretpwr - COMPRESSEDCONTAINER = CFG["Extensions"]["compressedExtensions"] + COMPRESSEDCONTAINER = [re.compile('.r\d{2}$', re.I), + re.compile('.part\d+.rar$', re.I), + re.compile('.rar$', re.I)] + COMPRESSEDCONTAINER += [re.compile('%s$' % ext, re.I) for ext in CFG["Extensions"]["compressedExtensions"]] MEDIACONTAINER = CFG["Extensions"]["mediaExtensions"] AUDIOCONTAINER = CFG["Extensions"]["audioExtensions"] METACONTAINER = CFG["Extensions"]["metaExtensions"] # .nfo,.sub,.srt @@ -287,6 +292,8 @@ def initialize(section=None): OUTPUTQUALITYPERCENT = int(CFG["Transcoder"]["outputQualityPercent"]) NICENESS = int(CFG["Transcoder"]["niceness"]) + PASSWORDSFILE = CFG["passwords"]["PassWordFile"] + # Setup FFMPEG and FFPROBE locations if platform.system() == 'Windows': FFMPEG = os.path.join(FFMPEG_PATH, 'ffmpeg.exe') @@ -327,9 +334,9 @@ def initialize(section=None): USER_SCRIPT_RUNONCE = int(CFG["UserScript"]["user_script_runOnce"]) # check for script-defied section and if None set to allow sections - SECTIONS = tuple(x for x in CFG if CFG[x].sections) if not section else (section,) - SUBSECTIONS = CFG[SECTIONS].isenabled() - CATEGORIES += SUBSECTIONS.sections + SECTIONS = CFG[tuple(x for x in CFG if CFG[x].sections) if not section else (section,)] + map(CATEGORIES.extend,([subsection.sections for section,subsection in SECTIONS.items()])) + CATEGORIES = list(set(CATEGORIES)) # create torrent class TORRENT_CLASS = create_torrent_class(TORRENT_CLIENTAGENT) diff --git a/nzbtomedia/extractor/extractor.py b/nzbtomedia/extractor/extractor.py index e8b6795c..394a952e 100644 --- a/nzbtomedia/extractor/extractor.py +++ b/nzbtomedia/extractor/extractor.py @@ -72,9 +72,8 @@ def extract(filePath, outputDestination): # Create outputDestination folder nzbtomedia.makeDir(outputDestination) - passwordsfile = nzbtomedia.CFG["passwords"]["PassWordFile"] - if passwordsfile != "" and os.path.isfile(os.path.normpath(passwordsfile)): - passwords = [line.strip() for line in open(os.path.normpath(passwordsfile))] + if nzbtomedia.PASSWORDSFILE != "" and os.path.isfile(os.path.normpath(nzbtomedia.PASSWORDSFILE)): + passwords = [line.strip() for line in open(os.path.normpath(nzbtomedia.PASSWORDSFILE))] else: passwords = [] nzbtomedia.logger.info("Extracting %s to %s" % (filePath, outputDestination)) diff --git a/nzbtomedia/nzbToMediaConfig.py b/nzbtomedia/nzbToMediaConfig.py index b8fd23c0..070f49a2 100644 --- a/nzbtomedia/nzbToMediaConfig.py +++ b/nzbtomedia/nzbToMediaConfig.py @@ -39,9 +39,12 @@ class Section(configobj.Section): if section in key: continue if isinstance(subsections, Section) and subsections.sections: - for subsection in subsections: + for subsection, options in subsections.items(): if subsection in key: continue + if key in options: + return options[key] + subsections.pop(subsection) else: if section not in key: diff --git a/nzbtomedia/nzbToMediaUtil.py b/nzbtomedia/nzbToMediaUtil.py index feceefaf..19d55e93 100644 --- a/nzbtomedia/nzbToMediaUtil.py +++ b/nzbtomedia/nzbToMediaUtil.py @@ -459,26 +459,24 @@ def rmDir(dirName): def cleanProcDirs(): logger.info('Cleaning processing directories ...', 'CLEANDIRS') - for section, subsection in nzbtomedia.SUBSECTIONS.items(): + for section, subsection in nzbtomedia.SECTIONS.items(): for category in subsection: - if nzbtomedia.CFG[section][category].isenabled(): - dirNames = getDirs(section, category) - for dirName in dirNames: - try: - minSize = int(nzbtomedia.CFG[section][category]['minSize']) - except:minSize = 0 - try: - delete_ignored = int(nzbtomedia.CFG[section][category]['delete_ignored']) - except:delete_ignored = 0 - num_files = len(listMediaFiles(dirName, minSize=minSize, delete_ignored=delete_ignored)) - if num_files > 0: - logger.info( - "Directory %s still contains %s unprocessed file(s), skipping ..." % (dirName, num_files), - 'CLEANDIRS') - continue + for dirName in nzbtomedia.getDirs(subsection[category]): + try: + minSize = int(nzbtomedia.CFG[section][category]['minSize']) + except:minSize = 0 + try: + delete_ignored = int(nzbtomedia.CFG[section][category]['delete_ignored']) + except:delete_ignored = 0 + num_files = len(listMediaFiles(dirName, minSize=minSize, delete_ignored=delete_ignored)) + if num_files > 0: + logger.info( + "Directory %s still contains %s unprocessed file(s), skipping ..." % (dirName, num_files), + 'CLEANDIRS') + continue - logger.info("Directory %s has already been processed, removing ..." % (dirName), 'CLEANDIRS') - shutil.rmtree(dirName) + logger.info("Directory %s has already been processed, removing ..." % (dirName), 'CLEANDIRS') + shutil.rmtree(dirName) def create_torrent_class(clientAgent): # Hardlink solution for Torrents @@ -603,6 +601,14 @@ def cleanFileName(filename): filename = re.sub("^\[.*\]", "", filename) return filename.strip() + +def is_archive_file(filename): + """Check if the filename is allowed for the Archive""" + for regext in nzbtomedia.COMPRESSEDCONTAINER: + if regext.search(filename): + return regext.split(filename)[0] + return False + def isMediaFile(mediafile, media=True, audio=True, meta=True, archives=True): fileName, fileExt = os.path.splitext(mediafile) @@ -613,7 +619,7 @@ def isMediaFile(mediafile, media=True, audio=True, meta=True, archives=True): if (media and fileExt.lower() in nzbtomedia.MEDIACONTAINER)\ or (audio and fileExt.lower() in nzbtomedia.AUDIOCONTAINER)\ or (meta and fileExt.lower() in nzbtomedia.METACONTAINER)\ - or (archives and fileExt.lower() in nzbtomedia.COMPRESSEDCONTAINER): + or (archives and is_archive_file(mediafile)): return True else: return False @@ -696,33 +702,25 @@ def find_imdbid(dirName, inputName): def extractFiles(src, dst=None): extracted_folder = [] - for inputFile in listMediaFiles(src): + for inputFile in listMediaFiles(src, media=False, audio=False, meta=False, archives=True): dirPath = os.path.dirname(inputFile) - fileName, fileExt = os.path.splitext(os.path.basename(inputFile)) fullFileName = os.path.basename(inputFile) - if fileExt in nzbtomedia.COMPRESSEDCONTAINER: - if re.search('part\d+', fullFileName): - if not re.search('^((?!\.part(?!0*1\.rar$)\d+\.rar$).)*\.(?:rar|r?0*1)$', fullFileName): - continue + if dirPath in extracted_folder: + break - logger.info("Found compressed archive %s for file %s" % (fileExt, fullFileName)) - - while(True): - try: - extractor.extract(inputFile, dst or dirPath) - extracted_folder.append(dst or dirPath) - break - except: - logger.error("Extraction failed for: %s" % (fullFileName)) + try: + if extractor.extract(inputFile, dirPath or dst): + extracted_folder.append(dirPath or dst) + except Exception, e: + logger.error("Extraction failed for: %s" % (fullFileName)) if extracted_folder: for folder in extracted_folder: for inputFile in listMediaFiles(folder): fullFileName = os.path.basename(inputFile) - fileName, fileExt = os.path.splitext(fullFileName) - if fileExt in nzbtomedia.COMPRESSEDCONTAINER: + if is_archive_file(inputFile): logger.info("Removing extracted archive %s from folder %s ..." % (fullFileName, folder)) try: os.remove(inputFile) diff --git a/tests/general.py b/tests/general.py index e2075ff9..424d0a56 100644 --- a/tests/general.py +++ b/tests/general.py @@ -1,5 +1,6 @@ import os import datetime +import re import nzbtomedia from nzbtomedia import nzbToMediaDB from nzbtomedia.nzbToMediaUtil import get_downloadInfo @@ -7,6 +8,11 @@ from nzbtomedia.nzbToMediaUtil import get_downloadInfo # Initialize the config nzbtomedia.initialize() +EXTENSIONS = [re.compile('.r\d{2}$', re.I), + re.compile('.part\d+.rar$', re.I), + re.compile('.rar$', re.I)] +EXTENSIONS += [re.compile('%s$' % ext, re.I) for ext in nzbtomedia.COMPRESSEDCONTAINER] + test = nzbtomedia.CFG['HeadPhones']['music'] section = nzbtomedia.CFG.findsection('tv').isenabled() print section \ No newline at end of file