From 83f95f1f4f062df0eaa05bd4ab64e20ec25a09f5 Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Wed, 3 Apr 2013 16:01:31 +1030 Subject: [PATCH] Allow postprocess of individual folder --- TorrentToMedia.py | 8 ++-- autoProcess/autoProcessMovie.py | 83 ++++++++++++++++++++++++++++----- autoProcess/nzbToMediaUtil.py | 16 +++++-- nzbToCouchPotato.py | 9 ++-- nzbToMedia.py | 5 +- 5 files changed, 97 insertions(+), 24 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 0cd730a3..793fb5cd 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -25,7 +25,7 @@ from nzbToMediaEnv import * from nzbToMediaUtil import * from utorrent.client import UTorrentClient -def main(inputDirectory, inputName, inputCategory, inputHash): +def main(inputDirectory, inputName, inputCategory, inputHash, inputID): status = int(1) # 1 = failed | 0 = success root = int(0) @@ -170,7 +170,7 @@ def main(inputDirectory, inputName, inputCategory, inputHash): if inputCategory == cpsCategory: Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", inputName) - result = autoProcessMovie.process(outputDestination, inputName, status) + result = autoProcessMovie.process(outputDestination, inputName, status, clientAgent, inputID) elif inputCategory == sbCategory: Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName) result = autoProcessTV.processEpisode(outputDestination, inputName, status) @@ -247,9 +247,9 @@ if __name__ == "__main__": transcode = int(config.get("Transcoder", "transcode")) try: - inputDirectory, inputName, inputCategory, inputHash = parse_args(clientAgent) + inputDirectory, inputName, inputCategory, inputHash, inputID = parse_args(clientAgent) except Exception as e: Logger.error("MAIN: There was a problem loading variables: %s", e) sys.exit(-1) - main(inputDirectory, inputName, inputCategory, inputHash) + main(inputDirectory, inputName, inputCategory, inputHash, inputID) diff --git a/autoProcess/autoProcessMovie.py b/autoProcess/autoProcessMovie.py index accd55ff..821f64ae 100644 --- a/autoProcess/autoProcessMovie.py +++ b/autoProcess/autoProcessMovie.py @@ -58,7 +58,7 @@ def get_movie_info(myOpener, baseURL, imdbid): if not imdbid: return "" - url = baseURL + "movie.list" + url = baseURL + "movie.list/?status=active" Logger.debug("Opening URL: %s", url) @@ -77,31 +77,87 @@ def get_movie_info(myOpener, baseURL, imdbid): movie_id = str(movieid[index]) Logger.info("Found movie id %s in CPS database for movie %s", movie_id, imdbid) break + return movie_id -def get_status(myOpener, baseURL, movie_id): +def get_status(myOpener, baseURL, movie_id, clientAgent, download_id): if not movie_id: - return "" + return "", clientAgent, "none", "none" url = baseURL + "movie.get/?id=" + str(movie_id) - + Logger.debug("Looking for status of movie: %s - with release sent to clientAgent: %s and download_id: %s", movie_id, clientAgent, download_id) Logger.debug("Opening URL: %s", url) try: urlObj = myOpener.openit(url) except IOError, e: Logger.error("Unable to open URL: %s", str(e)) - return "" + return "", clientAgent, "none", "none" result = json.load(urlObj) try: movie_status = result["movie"]["status"]["identifier"] Logger.debug("This movie is marked as status %s in CouchPotatoServer", movie_status) - return movie_status except e: # index out of range/doesn't exist? Logger.error("Could not find a status for this movie due to: %s", str(e)) - return "" + movie_status = "" + try: + release_status = "none" + if download_id != "" and download_id != "none": # we have the download id from the downloader. Let's see if it's valid. + release_statuslist = [item["status"]["identifier"] for item in result["movie"]["releases"] if item["info"]["download_id"] == download_id] + clientAgentlist = [item["info"]["download_downloader"] for item in result["movie"]["releases"] if item["info"]["download_id"] == download_id] + if len(release_statuslist) == 1: # we have found a release by this id. :) + release_status = release_statuslist[0] + clientAgent = clientAgentlist[0] + Logger.debug("Found a single release with download_id: %s for clientAgent: %s. Release status is: %s", download_id, clientAgent, release_status) + return movie_status, clientAgent, download_id, release_status + elif len(release_statuslist) > 1: # we have found many releases by this id. Check for snatched status + clients = [item in clientAgentlist if item.lower() == clientAgent.lower()] + clientAgent = clients[0] + if len(clients) == 1: # ok.. a unique entry for download_id and clientAgent ;) + release_status = [item["status"]["identifier"] for item in result["movie"]["releases"] if item["info"]["download_id"] == download_id and item["info"]["download_downloader"] == clientAgent][0] + Logger.debug("Found a single release for download_id: %s and clientAgent: %s. Release status is: %s", download_id, clientAgent, release_status) + else: # doesn't matter. only really used as secondary confirmation of movie status change. Let's continue. + Logger.debug("Found several releases for download_id: %s and clientAgent: %s. Cannot determine the release status", download_id, clientAgent) + return movie_status, clientAgent, download_id, release_status + else: # clearly the id we were passed doesn't match the database. Reset it and search all snatched releases.... hence the next if (not elif ;) ) + download_id = "" + if download_id == "none": # if we couldn't find this initially, there is no need to check next time around. + return movie_status, clientAgent, download_id, release_status + elif download_id == "": # in case we didn't get this from the downloader. + download_idlist = [item["info"]["download_id"] for item in result["movie"]["releases"] if item["status"]["identifier"] == "snatched"] + clientAgentlist = [item["info"]["download_downloader"] for item in result["movie"]["releases"] if item["status"]["identifier"] == "snatched"] + if len(clientAgentlist) == 1: + if clientAgent == "manual": + clientAgent = clientAgentlist[0] + download_id = download_idlist[0] + release_status = "snatched" + elif clientAgent.lower() == clientAgentlist[0].lower(): + download_id = download_idlist[0] + clientAgent = clientAgentlist[0] + release_status = "snatched" + Logger.debug("Found a single download_id: %s and clientAgent: %s. Release status is: %s", download_id, clientAgent, release_status) + elif clientAgent == "manual": + download_id = "none" + release_status = "none" + else: + index = [index for index in range(len(clientAgentlist)) if clientAgentlist[index] == clientAgent] + if len(index) == 1: + download_id = download_idlist[index[0]] + release_status = "snatched" + Logger.debug("Found download_id: %s for clientAgent: %s. Release status is: %s", download_id, clientAgent, release_status) + else: + Logger.info("Found a total of %s releases snatched for clientAgent: %s. Cannot determine download_id. Will perform a renamenr scan to try and process.", len(index), clientAgent) + download_id = "none" + release_status = "none" + else: #something went wrong here.... we should never get to this. + Logger.info("Could not find a download_id in the database for this movie") + release_status = "none" + except: # index out of range/doesn't exist? + Logger.error("Could not find a download_id for this movie") + download_id = "none" + return movie_status, clientAgent, download_id, release_status -def process(dirName, nzbName=None, status=0): +def process(dirName, nzbName=None, status=0, clientAgent = "manual", download_id = ""): status = int(status) config = ConfigParser.ConfigParser() @@ -155,8 +211,8 @@ def process(dirName, nzbName=None, status=0): baseURL = protocol + host + ":" + port + web_root + "/api/" + apikey + "/" movie_id = get_movie_info(myOpener, baseURL, imdbid) # get the CPS database movie id this movie. - - initial_status = get_status(myOpener, baseURL, movie_id) + + initial_status, clientAgent, download_id, initial_release_status = get_status(myOpener, baseURL, movie_id, clientAgent, download_id) process_all_exceptions(nzbName.lower(), dirName) @@ -172,6 +228,8 @@ def process(dirName, nzbName=None, status=0): command = "manage.update" else: command = "renamer.scan" + if clientAgent != "manual" and download_id != "none": + command = command + "/?movie_folder=" + dirName + "&downloader=" + clientAgent + "&download_id=" + download_id url = baseURL + command @@ -234,10 +292,13 @@ def process(dirName, nzbName=None, status=0): # we will now check to see if CPS has finished renaming before returning to TorrentToMedia and unpausing. start = datetime.datetime.now() # set time for timeout while (datetime.datetime.now() - start) < datetime.timedelta(minutes=2): # only wait 2 minutes, then return to TorrentToMedia - movie_status = get_status(myOpener, baseURL, movie_id) # get the current status fo this movie. + movie_status, clientAgent, download_id, release_status = get_status(myOpener, baseURL, movie_id, clientAgent, download_id) # get the current status fo this movie. if movie_status != initial_status: # Something has changed. CPS must have processed this movie. Logger.info("SUCCESS: This movie is now marked as status %s in CouchPotatoServer", movie_status) return 0 # success + if release_status != initial_release_status: # Something has changed. CPS must have processed this movie. + Logger.info("SUCCESS: This release is now marked as status %s in CouchPotatoServer", release_status) + return 0 # success time.sleep(20) # Just stop this looping infinitely and hogging resources for 2 minutes ;) else: # The status hasn't changed. we have waited 2 minutes which is more than enough. uTorrent can resule seeding now. Logger.warning("The movie does not appear to have changed status after 2 minutes. Please check CouchPotato Logs") diff --git a/autoProcess/nzbToMediaUtil.py b/autoProcess/nzbToMediaUtil.py index ef22bf53..7ec02656 100644 --- a/autoProcess/nzbToMediaUtil.py +++ b/autoProcess/nzbToMediaUtil.py @@ -210,7 +210,7 @@ def iterate_media_files(dirname): def parse_other(args): - return os.path.normpath(sys.argv[1]), '', '', '' + return os.path.normpath(sys.argv[1]), '', '', '', '' def parse_utorrent(args): @@ -225,8 +225,12 @@ def parse_utorrent(args): inputHash = args[4] except: inputHash = '' + try: + inputID = args[4] + except: + inputID = '' - return inputDirectory, inputName, inputCategory, inputHash + return inputDirectory, inputName, inputCategory, inputHash, inputID] def parse_deluge(args): @@ -235,7 +239,8 @@ def parse_deluge(args): inputName = sys.argv[2] inputCategory = '' # We dont have a category yet inputHash = '' - return inputDirectory, inputName, inputCategory, inputHash + inputID = sys.argv[1] + return inputDirectory, inputName, inputCategory, inputHash, inputID def parse_transmission(args): @@ -243,8 +248,9 @@ def parse_transmission(args): inputDirectory = os.path.normpath(os.getenv('TR_TORRENT_DIR')) inputName = os.getenv('TR_TORRENT_NAME') inputCategory = '' # We dont have a category yet - inputHash = '' - return inputDirectory, inputName, inputCategory, inputHash + inputHash = os.getenv('TR_TORRENT_HASH') + inputID = os.getenv('TR_TORRENT_ID') + return inputDirectory, inputName, inputCategory, inputHash, inputID __ARG_PARSERS__ = { diff --git a/nzbToCouchPotato.py b/nzbToCouchPotato.py index 4e82ed65..23306e6d 100755 --- a/nzbToCouchPotato.py +++ b/nzbToCouchPotato.py @@ -31,7 +31,8 @@ if len(sys.argv) == SABNZB_NO_OF_ARGUMENTS: # 6 Group that the NZB was posted in e.g. alt.binaries.x # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2 Logger.info("Script triggered from SABnzbd, starting autoProcessMovie...") - result = autoProcessMovie.process(sys.argv[1], sys.argv[2], sys.argv[7]) + clientAgent = "sabnzbd" + result = autoProcessMovie.process(sys.argv[1], sys.argv[2], sys.argv[7], clientAgent) # NZBGet elif len(sys.argv) == NZBGET_NO_OF_ARGUMENTS: # NZBGet argv: @@ -39,8 +40,10 @@ elif len(sys.argv) == NZBGET_NO_OF_ARGUMENTS: # 2 The original name of the NZB file # 3 The status of the download: 0 == successful Logger.info("Script triggered from NZBGet, starting autoProcessMovie...") - result = autoProcessMovie.process(sys.argv[1], sys.argv[2], sys.argv[3]) + clientAgent = "nzbget" + result = autoProcessMovie.process(sys.argv[1], sys.argv[2], sys.argv[3], clientAgent) else: Logger.warn("Invalid number of arguments received from client.") Logger.info("Running autoProcessMovie as a manual run...") - result = autoProcessMovie.process('Manual Run', 'Manual Run', 0) + clientAgent = "manual" + result = autoProcessMovie.process('Manual Run', 'Manual Run', 0, clientAgent) diff --git a/nzbToMedia.py b/nzbToMedia.py index bd5a0f2f..40076000 100755 --- a/nzbToMedia.py +++ b/nzbToMedia.py @@ -50,6 +50,7 @@ if len(sys.argv) == SABNZB_NO_OF_ARGUMENTS: # 6 Group that the NZB was posted in e.g. alt.binaries.x # 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2 Logger.info("MAIN: Script triggered from SABnzbd") + clientAgent = "sabnzbd" nzbDir, inputName, status, inputCategory = (sys.argv[1], sys.argv[2], sys.argv[7], sys.argv[5]) # NZBGet elif len(sys.argv) == NZBGET_NO_OF_ARGUMENTS: @@ -59,15 +60,17 @@ elif len(sys.argv) == NZBGET_NO_OF_ARGUMENTS: # 3 The status of the download: 0 == successful # 4 User-defined category Logger.info("MAIN: Script triggered from NZBGet") + clientAgent = "nzbget" nzbDir, inputName, status, inputCategory = (sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) else: # only CPS supports this manual run for now. Logger.warn("MAIN: Invalid number of arguments received from client.") Logger.info("MAIN: Running autoProcessMovie as a manual run...") + clientAgent = "manual" nzbDir, inputName, status, inputCategory = ('Manual Run', 'Manual Run', 0, cpsCategory) if inputCategory == cpsCategory: Logger.info("MAIN: Calling CouchPotatoServer to post-process: %s", inputName) - result = autoProcessMovie.process(nzbDir, inputName, status) + result = autoProcessMovie.process(nzbDir, inputName, status, clientAgent) elif inputCategory == sbCategory: Logger.info("MAIN: Calling Sick-Beard to post-process: %s", inputName) result = autoProcessTV.processEpisode(nzbDir, inputName, status)