mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-19 12:59:36 -07:00
Added cleanup of processing folders to start and end of scripts, helps resolve issues where empty folders or misc files left behind cause processing scripts to execute without need.
Modified our listMediaFiles function to allow include/exclude of extension containers so that we can chose what extensions are used to create the results list that gets returned back to us.
This commit is contained in:
parent
9c817af60e
commit
0053fdcf3e
4 changed files with 45 additions and 52 deletions
|
@ -2,23 +2,20 @@
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import re
|
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import nzbtomedia
|
import nzbtomedia
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
from nzbtomedia.Transcoder import Transcoder
|
|
||||||
from nzbtomedia.autoProcess.autoProcessComics import autoProcessComics
|
from nzbtomedia.autoProcess.autoProcessComics import autoProcessComics
|
||||||
from nzbtomedia.autoProcess.autoProcessGames import autoProcessGames
|
from nzbtomedia.autoProcess.autoProcessGames import autoProcessGames
|
||||||
from nzbtomedia.autoProcess.autoProcessMovie import autoProcessMovie
|
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.nzbToMediaUtil import category_search, sanitizeFileName, copy_link, parse_args, flatten, get_dirnames, \
|
from nzbtomedia.nzbToMediaUtil import category_search, sanitizeFileName, copy_link, parse_args, flatten, get_dirnames, \
|
||||||
remove_read_only, cleanup_directories, create_torrent_class, pause_torrent, resume_torrent, listMediaFiles, joinPath, \
|
remove_read_only, create_torrent_class, pause_torrent, resume_torrent, listMediaFiles, joinPath, \
|
||||||
extractFiles
|
extractFiles, cleanProcDirs
|
||||||
from nzbtomedia import logger
|
from nzbtomedia import logger
|
||||||
|
|
||||||
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
|
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
|
||||||
|
@ -67,7 +64,6 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
|
||||||
if clientAgent != 'manual':
|
if clientAgent != 'manual':
|
||||||
resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputName)
|
resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputName)
|
||||||
|
|
||||||
cleanup_directories(inputCategory, processCategories, result, outputDestination)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
processOnly = nzbtomedia.CFG[nzbtomedia.SECTIONS].sections
|
processOnly = nzbtomedia.CFG[nzbtomedia.SECTIONS].sections
|
||||||
|
@ -193,7 +189,6 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
|
||||||
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")
|
||||||
resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputName)
|
resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputName)
|
||||||
|
|
||||||
cleanup_directories(inputCategory, processCategories, result, outputDestination)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def external_script(outputDestination, torrentName, torrentLabel):
|
def external_script(outputDestination, torrentName, torrentLabel):
|
||||||
|
@ -314,6 +309,9 @@ 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:
|
||||||
|
# cleanup our processing folders of any misc unwanted files and empty directories
|
||||||
|
cleanProcDirs()
|
||||||
|
|
||||||
logger.info("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]))
|
||||||
|
|
|
@ -276,13 +276,12 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import nzbtomedia
|
import nzbtomedia
|
||||||
from nzbtomedia.Transcoder import Transcoder
|
|
||||||
from nzbtomedia.autoProcess.autoProcessComics import autoProcessComics
|
from nzbtomedia.autoProcess.autoProcessComics import autoProcessComics
|
||||||
from nzbtomedia.autoProcess.autoProcessGames import autoProcessGames
|
from nzbtomedia.autoProcess.autoProcessGames import autoProcessGames
|
||||||
from nzbtomedia.autoProcess.autoProcessMovie import autoProcessMovie
|
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.nzbToMediaUtil import get_dirnames, cleanup_directories, listMediaFiles, extractFiles
|
from nzbtomedia.nzbToMediaUtil import get_dirnames, extractFiles, cleanProcDirs
|
||||||
from nzbtomedia import logger
|
from nzbtomedia import logger
|
||||||
|
|
||||||
# post-processing
|
# post-processing
|
||||||
|
@ -312,10 +311,6 @@ def process(nzbDir, inputName=None, status=0, clientAgent='manual', download_id=
|
||||||
logger.error("We could not find a section with containing a download category labeled %s in your autoProcessMedia.cfg, Exiting!" % inputCategory)
|
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:
|
|
||||||
# Clean up any leftover files
|
|
||||||
cleanup_directories(inputCategory, section, result, nzbDir)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def main(args, section=None):
|
def main(args, section=None):
|
||||||
|
@ -432,6 +427,9 @@ def main(args, section=None):
|
||||||
logger.debug("nzbToMedia %s:%s is DISABLED" % (section, category))
|
logger.debug("nzbToMedia %s:%s is DISABLED" % (section, category))
|
||||||
|
|
||||||
if result == 0:
|
if result == 0:
|
||||||
|
# cleanup our processing folders of any misc unwanted files and empty directories
|
||||||
|
cleanProcDirs()
|
||||||
|
|
||||||
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
|
||||||
sys.exit(nzbtomedia.NZBGET_POSTPROCESS_SUCCESS)
|
sys.exit(nzbtomedia.NZBGET_POSTPROCESS_SUCCESS)
|
||||||
|
|
|
@ -21,7 +21,7 @@ sys.path.insert(0, os.path.abspath(os.path.join(PROGRAM_DIR, 'lib')))
|
||||||
|
|
||||||
from nzbtomedia import logger, versionCheck
|
from nzbtomedia import logger, versionCheck
|
||||||
from nzbtomedia.nzbToMediaConfig import config
|
from nzbtomedia.nzbToMediaConfig import config
|
||||||
from nzbtomedia.nzbToMediaUtil import WakeUp, makeDir, joinPath
|
from nzbtomedia.nzbToMediaUtil import WakeUp, makeDir, joinPath, cleanProcDirs
|
||||||
|
|
||||||
# sabnzbd constants
|
# sabnzbd constants
|
||||||
SABNZB_NO_OF_ARGUMENTS = 8
|
SABNZB_NO_OF_ARGUMENTS = 8
|
||||||
|
@ -250,8 +250,6 @@ def initialize(section=None):
|
||||||
if isinstance(AUDIOCONTAINER, str): AUDIOCONTAINER = AUDIOCONTAINER.split(',')
|
if isinstance(AUDIOCONTAINER, str): AUDIOCONTAINER = AUDIOCONTAINER.split(',')
|
||||||
if isinstance(METACONTAINER, str): METACONTAINER = METACONTAINER.split(',')
|
if isinstance(METACONTAINER, str): METACONTAINER = METACONTAINER.split(',')
|
||||||
|
|
||||||
EXTCONTAINER = [y for x in COMPRESSEDCONTAINER,MEDIACONTAINER,AUDIOCONTAINER,METACONTAINER for y in x]
|
|
||||||
|
|
||||||
MINSAMPLESIZE = int(CFG["Extensions"]["minSampleSize"]) # 200 (in MB)
|
MINSAMPLESIZE = int(CFG["Extensions"]["minSampleSize"]) # 200 (in MB)
|
||||||
SAMPLEIDS = CFG["Extensions"]["SampleIDs"]
|
SAMPLEIDS = CFG["Extensions"]["SampleIDs"]
|
||||||
|
|
||||||
|
@ -312,7 +310,9 @@ def initialize(section=None):
|
||||||
SUBSECTIONS = CFG[SECTIONS]
|
SUBSECTIONS = CFG[SECTIONS]
|
||||||
CATEGORIES += SUBSECTIONS.sections
|
CATEGORIES += SUBSECTIONS.sections
|
||||||
|
|
||||||
__INITIALIZED__ = True
|
# cleanup our processing folders of any misc unwanted files and empty directories
|
||||||
|
cleanProcDirs()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def restart():
|
def restart():
|
||||||
|
|
|
@ -209,7 +209,6 @@ def removeEmptyFolders(path):
|
||||||
logger.debug("Removing empty folder: %s" % (path), 'REMOVER')
|
logger.debug("Removing empty folder: %s" % (path), 'REMOVER')
|
||||||
os.rmdir(path)
|
os.rmdir(path)
|
||||||
|
|
||||||
|
|
||||||
def remove_read_only(path):
|
def remove_read_only(path):
|
||||||
if not os.path.isdir(path):
|
if not os.path.isdir(path):
|
||||||
return
|
return
|
||||||
|
@ -436,27 +435,22 @@ def rmDir(dirName):
|
||||||
except:
|
except:
|
||||||
logger.error("Unable to delete folder %s" % (dirName))
|
logger.error("Unable to delete folder %s" % (dirName))
|
||||||
|
|
||||||
def cleanup_directories(inputCategory, processCategories, result, directory):
|
def cleanProcDirs():
|
||||||
if inputCategory in processCategories and result == 0 and os.path.isdir(directory):
|
logger.info('Cleaning processing directories ...', 'CLEANDIRS')
|
||||||
num_files_new = int(0)
|
for section, subsection in nzbtomedia.SUBSECTIONS.items():
|
||||||
file_list = []
|
for category in subsection:
|
||||||
for dirpath, dirnames, filenames in os.walk(directory):
|
if nzbtomedia.CFG[section][category].isenabled():
|
||||||
for file in filenames:
|
dirNames = get_dirnames(section, category)
|
||||||
filePath = joinPath(dirpath, file)
|
for dirName in dirNames:
|
||||||
fileName, fileExtension = os.path.splitext(file)
|
num_files = len(listMediaFiles(dirName, archives=False))
|
||||||
if fileExtension in nzbtomedia.MEDIACONTAINER or fileExtension in nzbtomedia.METACONTAINER:
|
if num_files > 0:
|
||||||
num_files_new += 1
|
logger.info(
|
||||||
file_list.append(file)
|
"Directory %s still contains %s unprocessed file(s), skipping ..." % (dirName, num_files),
|
||||||
if num_files_new is 0 or int(nzbtomedia.CFG["General"]["force_clean"]) == 1:
|
'CLEANDIRS')
|
||||||
logger.info("All files have been processed. Cleaning directory %s" % (directory))
|
continue
|
||||||
shutil.rmtree(directory)
|
|
||||||
else:
|
|
||||||
logger.info(
|
|
||||||
"Directory %s still contains %s media and/or meta files. This directory will not be removed." % (
|
|
||||||
directory, num_files_new))
|
|
||||||
for item in file_list:
|
|
||||||
logger.debug("media/meta file found: %s" % (item))
|
|
||||||
|
|
||||||
|
logger.info("Directory %s has already been processed, removing ..." % (dirName), 'CLEANDIRS')
|
||||||
|
shutil.rmtree(dirName)
|
||||||
|
|
||||||
def create_torrent_class(clientAgent):
|
def create_torrent_class(clientAgent):
|
||||||
# Hardlink solution for Torrents
|
# Hardlink solution for Torrents
|
||||||
|
@ -564,7 +558,7 @@ def find_download(clientAgent, download_id):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def clean_nzbname(nzbname):
|
def cleanFileName(filename):
|
||||||
"""Cleans up nzb name by removing any . and _
|
"""Cleans up nzb name by removing any . and _
|
||||||
characters, along with any trailing hyphens.
|
characters, along with any trailing hyphens.
|
||||||
|
|
||||||
|
@ -572,16 +566,16 @@ def clean_nzbname(nzbname):
|
||||||
space, but handles decimal numbers in string, for example:
|
space, but handles decimal numbers in string, for example:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
nzbname = re.sub("(\D)\.(?!\s)(\D)", "\\1 \\2", nzbname)
|
filename = re.sub("(\D)\.(?!\s)(\D)", "\\1 \\2", filename)
|
||||||
nzbname = re.sub("(\d)\.(\d{4})", "\\1 \\2", nzbname) # if it ends in a year then don't keep the dot
|
filename = re.sub("(\d)\.(\d{4})", "\\1 \\2", filename) # if it ends in a year then don't keep the dot
|
||||||
nzbname = re.sub("(\D)\.(?!\s)", "\\1 ", nzbname)
|
filename = re.sub("(\D)\.(?!\s)", "\\1 ", filename)
|
||||||
nzbname = re.sub("\.(?!\s)(\D)", " \\1", nzbname)
|
filename = re.sub("\.(?!\s)(\D)", " \\1", filename)
|
||||||
nzbname = nzbname.replace("_", " ")
|
filename = filename.replace("_", " ")
|
||||||
nzbname = re.sub("-$", "", nzbname)
|
filename = re.sub("-$", "", filename)
|
||||||
nzbname = re.sub("^\[.*\]", "", nzbname)
|
filename = re.sub("^\[.*\]", "", filename)
|
||||||
return nzbname.strip()
|
return filename.strip()
|
||||||
|
|
||||||
def isMediaFile(mediafile):
|
def isMediaFile(mediafile, media=True, audio=True, meta=True, archives=True):
|
||||||
fileName, fileExt = os.path.splitext(mediafile)
|
fileName, fileExt = os.path.splitext(mediafile)
|
||||||
|
|
||||||
# ignore MAC OS's retarded "resource fork" files
|
# ignore MAC OS's retarded "resource fork" files
|
||||||
|
@ -592,12 +586,15 @@ def isMediaFile(mediafile):
|
||||||
logger.info("Ignoring extras file: %s " % (mediafile))
|
logger.info("Ignoring extras file: %s " % (mediafile))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if fileExt.lower() in nzbtomedia.EXTCONTAINER:
|
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):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def listMediaFiles(path, ignoreSample=True):
|
def listMediaFiles(path, media=True, audio=True, meta=True, archives=True, ignoreSample=True):
|
||||||
if not dir or not os.path.isdir(path):
|
if not dir or not os.path.isdir(path):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
@ -607,9 +604,9 @@ def listMediaFiles(path, ignoreSample=True):
|
||||||
|
|
||||||
# if it's a folder do it recursively
|
# if it's a folder do it recursively
|
||||||
if os.path.isdir(fullCurFile) and not curFile.startswith('.') and not curFile == 'Extras':
|
if os.path.isdir(fullCurFile) and not curFile.startswith('.') and not curFile == 'Extras':
|
||||||
files += listMediaFiles(fullCurFile)
|
files += listMediaFiles(fullCurFile, media, audio, meta, archives)
|
||||||
|
|
||||||
elif isMediaFile(curFile):
|
elif isMediaFile(curFile, media, audio, meta, archives):
|
||||||
# Optionally ignore sample files
|
# Optionally ignore sample files
|
||||||
if ignoreSample and is_sample(fullCurFile, nzbtomedia.MINSAMPLESIZE, nzbtomedia.SAMPLEIDS):
|
if ignoreSample and is_sample(fullCurFile, nzbtomedia.MINSAMPLESIZE, nzbtomedia.SAMPLEIDS):
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue