diff --git a/core/autoProcess/autoProcessMovie.py b/core/autoProcess/autoProcessMovie.py index 27ff1915..3b5718ea 100644 --- a/core/autoProcess/autoProcessMovie.py +++ b/core/autoProcess/autoProcessMovie.py @@ -104,6 +104,39 @@ class autoProcessMovie(object): return results + def command_complete(self, url, params, headers, section): + try: + r = requests.get(url, params=params, headers=headers, stream=True, verify=False, timeout=(30, 60)) + except requests.ConnectionError: + logger.error("Unable to open URL: {0}".format(url), section) + return None + if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]: + logger.error("Server returned status {0}".format(r.status_code), section) + return None + else: + try: + return r.json()['state'] + except (ValueError, KeyError): + # ValueError catches simplejson's JSONDecodeError and json's ValueError + logger.error("{0} did not return expected json data.".format(section), section) + return None + + def CDH(self, url2, headers, section="MAIN"): + try: + r = requests.get(url2, params={}, headers=headers, stream=True, verify=False, timeout=(30, 60)) + except requests.ConnectionError: + logger.error("Unable to open URL: {0}".format(url2), section) + return False + if r.status_code not in [requests.codes.ok, requests.codes.created, requests.codes.accepted]: + logger.error("Server returned status {0}".format(r.status_code), section) + return False + else: + try: + return r.json().get("enableCompletedDownloadHandling", False) + except ValueError: + # ValueError catches simplejson's JSONDecodeError and json's ValueError + return False + def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", download_id="", inputCategory=None, failureLink=None): cfg = dict(core.CFG[section][inputCategory]) @@ -132,6 +165,7 @@ class autoProcessMovie(object): baseURL = "{0}{1}:{2}{3}/api/{4}/".format(protocol, host, port, web_root, apikey) if section == "Radarr": baseURL = "{0}{1}:{2}{3}/api/command".format(protocol, host, port, web_root) + url2 = "{0}{1}:{2}{3}/api/config/downloadClient".format(protocol, host, port, web_root) headers = {'X-Api-Key': apikey} if not apikey: logger.info('No CouchPotato or Radarr apikey entered. Performing transcoder functions only') @@ -273,6 +307,14 @@ class autoProcessMovie(object): return [0, "{0}: Successfully post-processed {1}".format(section, inputName)] elif section == "Radarr": logger.postprocess("Radarr response: {0}".format(result['state'])) + try: + res = json.loads(r.content) + scan_id = int(res['id']) + logger.debug("Scan started with id: {0}".format(scan_id), section) + Started = True + except Exception as e: + logger.warning("No scan id was returned due to: {0}".format(e), section) + scan_id = None else: logger.error("FAILED: {0} scan was unable to finish for folder {1}. exiting!".format(method, dirName), section) @@ -353,6 +395,7 @@ class autoProcessMovie(object): logger.postprocess("Checking for status change, please stand by ...", section) if section == "CouchPotato": release = self.get_release(baseURL, imdbid, download_id, release_id) + scan_id = None else: release = None if release: @@ -368,6 +411,18 @@ class autoProcessMovie(object): return [0, "{0}: Successfully post-processed {1}".format(section, inputName)] except: pass + elif scan_id: + url = "{0}/{1}".format(baseURL, scan_id) + command_status = self.command_complete(url, params, headers, section) + if command_status: + logger.debug("The Scan command return status: {0}".format(command_status), section) + if command_status in ['completed']: + logger.debug("The Scan command has completed successfully. Renaming was successful.", section) + return [0, "{0}: Successfully post-processed {1}".format(section, inputName)] + elif command_status in ['failed']: + logger.debug("The Scan command has failed. Renaming was not successful.", section) + # return [1, "%s: Failed to post-process %s" % (section, inputName) ] + if not os.path.isdir(dirName): logger.postprocess("SUCCESS: Input Directory [{0}] has been processed and removed".format( dirName), section) @@ -378,10 +433,13 @@ class autoProcessMovie(object): dirName), section) return [0, "{0}: Successfully post-processed {1}".format(section, inputName)] - # pause and let CouchPotatoServer catch its breath + # pause and let CouchPotatoServer/Radarr catch its breath time.sleep(10 * wait_for) - # The status hasn't changed. we have waited 2 minutes which is more than enough. uTorrent can resume seeding now. + # The status hasn't changed. we have waited wait_for minutes which is more than enough. uTorrent can resume seeding now. + if section == "Radarr" and self.CDH(url2, headers, section=section): + logger.debug("The Scan command did not return status completed, but complete Download Handling is enabled. Passing back to {0}.".format(section), section) + return [status, "{0}: Complete DownLoad Handling is enabled. Passing back to {1}".format(section, section)] logger.warning( "{0} does not appear to have changed status after {1} minutes, Please check your logs.".format(inputName, wait_for), section)