diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 8b65b14d..323b063e 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -17,10 +17,11 @@ import extractor.extractor as extractor import autoProcess.autoProcessComics as autoProcessComics import autoProcess.autoProcessGames as autoProcessGames import autoProcess.autoProcessMusic as autoProcessMusic -import autoProcess.autoProcessTV as autoProcessTV import autoProcess.autoProcessMovie as autoProcessMovie +import autoProcess.autoProcessTV as autoProcessTV from autoProcess.nzbToMediaEnv import * from autoProcess.nzbToMediaUtil import * +from autoSickBeardFork import autoFork from utorrent.client import UTorrentClient from transmissionrpc.client import Client as TransmissionClient from synchronousdeluge.client import DelugeClient @@ -491,7 +492,7 @@ if __name__ == "__main__": cpsCategory = (config.get("CouchPotato", "cpsCategory")).split(',') # movie sbCategory = (config.get("SickBeard", "sbCategory")).split(',') # tv - sbFork = config.get("SickBeard", "fork") # default + sbFork, sbParams = autoFork(config.get("SickBeard", "fork")) # default Torrent_ForceLink = int(config.get("SickBeard", "Torrent_ForceLink")) # 1 hpCategory = (config.get("HeadPhones", "hpCategory")).split(',') # music mlCategory = (config.get("Mylar", "mlCategory")).split(',') # comics diff --git a/autoProcess/autoProcessTV.py b/autoProcess/autoProcessTV.py index eb79fd21..2132fa8a 100644 --- a/autoProcess/autoProcessTV.py +++ b/autoProcess/autoProcessTV.py @@ -11,6 +11,7 @@ import Transcoder from nzbToMediaEnv import * from nzbToMediaUtil import * from nzbToMediaSceneExceptions import process_all_exceptions +from autoSickBeardFork import autoFork Logger = logging.getLogger() @@ -32,7 +33,6 @@ class AuthURLOpener(urllib.FancyURLopener): self.numTries = 0 return urllib.FancyURLopener.open(self, url) - def delete(dirName): Logger.info("Deleting failed files and folder %s", dirName) try: @@ -63,6 +63,7 @@ def processEpisode(dirName, nzbName=None, failed=False, clientAgent=None, inputC port = config.get(section, "port") username = config.get(section, "username") password = config.get(section, "password") + try: ssl = int(config.get(section, "ssl")) except (ConfigParser.NoOptionError, ValueError): @@ -79,11 +80,6 @@ def processEpisode(dirName, nzbName=None, failed=False, clientAgent=None, inputC watch_dir = "" try: - fork = config.get(section, "fork") - except ConfigParser.NoOptionError: - fork = "default" - - try: transcode = int(config.get("Transcoder", "transcode")) except (ConfigParser.NoOptionError, ValueError): transcode = 0 @@ -125,16 +121,14 @@ def processEpisode(dirName, nzbName=None, failed=False, clientAgent=None, inputC if os.path.isdir(SpecificPath): dirName = SpecificPath - SICKBEARD_TORRENT_USE = SICKBEARD_TORRENT + # auto-detect fork type + fork, params = autoFork() - if clientAgent in ['nzbget','sabnzbd'] and not nzbExtractionBy == "Destination": #Assume Torrent actions (unrar and link) don't happen. We need to check for valid media here. - SICKBEARD_TORRENT_USE = [] - - if not fork in SICKBEARD_TORRENT_USE: + if (not fork in SICKBEARD_TORRENT) or (clientAgent in ['nzbget','sabnzbd'] and not nzbExtractionBy == "Destination"): process_all_exceptions(nzbName.lower(), dirName) nzbName, dirName = converto_to_ascii(nzbName, dirName) - if nzbName != "Manual Run" and not fork in SICKBEARD_TORRENT_USE: + if nzbName != "Manual Run" and not fork in SICKBEARD_TORRENT: # Now check if movie files exist in destination: video = int(0) for dirpath, dirnames, filenames in os.walk(dirName): @@ -157,10 +151,12 @@ def processEpisode(dirName, nzbName=None, failed=False, clientAgent=None, inputC if watch_dir != "" and (not host in ['localhost', '127.0.0.1'] or nzbName == "Manual Run"): dirName = watch_dir - params = {} - params['quiet'] = 1 - if fork in SICKBEARD_DIRNAME: + + if hasattr(params, "failed"): + params['failed'] = failed + + if hasattr(params, "dirName"): params['dirName'] = dirName else: params['dir'] = dirName @@ -168,9 +164,6 @@ def processEpisode(dirName, nzbName=None, failed=False, clientAgent=None, inputC if nzbName != None: params['nzbName'] = nzbName - if fork in SICKBEARD_FAILED: - params['failed'] = failed - if status == 0: Logger.info("The download succeeded. Sending process request to SickBeard's %s branch", fork) elif fork in SICKBEARD_FAILED: @@ -181,7 +174,7 @@ def processEpisode(dirName, nzbName=None, failed=False, clientAgent=None, inputC Logger.info("Deleting directory: %s", dirName) delete(dirName) return 0 # Success (as far as this script is concerned) - + if status == 0 and transcode == 1: # only transcode successful downlaods result = Transcoder.Transcode_directory(dirName) if result == 0: diff --git a/autoProcess/migratecfg.py b/autoProcess/migratecfg.py index e4389702..1ddb462f 100644 --- a/autoProcess/migratecfg.py +++ b/autoProcess/migratecfg.py @@ -60,10 +60,7 @@ def migrate(): option = "sbCategory" if option == "failed_fork": # change this old format option = "fork" - if int(value) == 1: - value = "failed" - else: - value = "default" + value = "auto" if option == "outputDirectory": # move this to new location format value = os.path.split(os.path.normpath(value))[0] confignew.set("Torrent", option, value) diff --git a/autoProcess/nzbToMediaEnv.py b/autoProcess/nzbToMediaEnv.py index 89bf99dd..928b7e34 100644 --- a/autoProcess/nzbToMediaEnv.py +++ b/autoProcess/nzbToMediaEnv.py @@ -8,12 +8,16 @@ TimeOut = 60 SABNZB_NO_OF_ARGUMENTS = 8 SABNZB_0717_NO_OF_ARGUMENTS = 9 -# Constants pertaining to SickBeard Branches: -# extend this list to include all branches/forks that use "failed" to handle failed downloads. -SICKBEARD_FAILED = ["failed", "TPB-failed", "Pistachitos", "TPB"] -# extend this list to include all branches/forks that use "dirName" not "dir" -SICKBEARD_DIRNAME = ["failed"] -# extend this list to include all branches/forks that process rar and link files for torrents and therefore skip extraction and linking in TorrentToMedia. -SICKBEARD_TORRENT = ["TPB", "TPB-failed", "Pistachitos"] +# Constants pertaining to SickBeard Branches: +fork_default = "default" +fork_failed = "failed" +fork_failed_torrent = "failed-torrent" +forks = {} +forks[fork_default] = {"dir": None, "process": None} +forks[fork_failed] = {"dir": None, "failed": None} +forks[fork_failed_torrent] = {"dir": None, "failed": None, "process_method": None} + +SICKBEARD_FAILED = [fork_failed, fork_failed_torrent] +SICKBEARD_TORRENT = [fork_failed_torrent] diff --git a/autoProcessMedia.cfg.sample b/autoProcessMedia.cfg.sample index 3270cbee..ede393b5 100644 --- a/autoProcessMedia.cfg.sample +++ b/autoProcessMedia.cfg.sample @@ -33,7 +33,7 @@ ssl = 0 delay = 0 wait_for = 5 watch_dir = -fork = default +fork = auto delete_failed = 0 nzbExtractionBy = Downloader Torrent_ForceLink = 1 diff --git a/autoSickBeardFork.py b/autoSickBeardFork.py new file mode 100644 index 00000000..bfaef381 --- /dev/null +++ b/autoSickBeardFork.py @@ -0,0 +1,82 @@ +import sys +import urllib +import os +import ConfigParser +import logging + +from autoProcess.nzbToMediaEnv import * + +Logger = logging.getLogger() + +class AuthURLOpener(urllib.FancyURLopener): + def __init__(self, user, pw): + self.username = user + self.password = pw + self.numTries = 0 + urllib.FancyURLopener.__init__(self) + + def prompt_user_passwd(self, host, realm): + if self.numTries == 0: + self.numTries = 1 + return (self.username, self.password) + else: + return ('', '') + + def openit(self, url): + self.numTries = 0 + return urllib.FancyURLopener.open(self, url) + +def autoFork(fork=None): + config = ConfigParser.ConfigParser() + configFilename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessMedia.cfg") + config.read(configFilename) + + # config settings + section = "SickBeard" + host = config.get(section, "host") + port = config.get(section, "port") + username = config.get(section, "username") + password = config.get(section, "password") + + try: + ssl = int(config.get(section, "ssl")) + except (ConfigParser.NoOptionError, ValueError): + ssl = 0 + + try: + web_root = config.get(section, "web_root") + except ConfigParser.NoOptionError: + web_root = "" + + try: + fork = config.get(section, "fork") + if not fork in "auto": + fork = forks[fork] \ + if fork in SICKBEARD_FAILED or SICKBEARD_TORRENT else forks[fork_default] + except ConfigParser.NoOptionError: + fork = forks[fork_default] + + myOpener = AuthURLOpener(username, password) + + if ssl: + protocol = "https://" + else: + protocol = "http://" + + if fork in "auto": + Logger.info("Attempting to auto-detect SickBeard fork") + for f in forks.iteritems(): + url = protocol + host + ":" + port + web_root + "/home/postprocess/processEpisode?" + urllib.urlencode(f[1]) + + # attempting to auto-detect fork + urlObj = myOpener.openit(url) + + if urlObj.getcode() == 200: + Logger.info("SickBeard fork auto-detection successful. Fork set to %s", f[0]) + return f[0], f[1] + + # failed to auto-detect fork + Logger.info("SickBeard fork auto-detection failed") + + Logger.info("SickBeard fork set to %s", fork[0]) + return fork[0], fork[1] \ No newline at end of file diff --git a/nzbToMedia.py b/nzbToMedia.py index 95609cf9..e7db98af 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -109,8 +109,8 @@ # SickBeard fork. # -# set to default or TPB or failed if using the custom "TPB" or "failed fork". -#sbfork=default +# set to default or auto to auto-detect the custom failed fork type". +#sbfork=auto # SickBeard Delete Failed Downloads (0, 1) # @@ -265,8 +265,8 @@ import autoProcess.migratecfg as migratecfg import autoProcess.autoProcessComics as autoProcessComics import autoProcess.autoProcessGames as autoProcessGames import autoProcess.autoProcessMusic as autoProcessMusic -import autoProcess.autoProcessTV as autoProcessTV import autoProcess.autoProcessMovie as autoProcessMovie +import autoProcess.autoProcessTV as autoProcessTV from autoProcess.nzbToMediaEnv import * from autoProcess.nzbToMediaUtil import *