Esempio n. 1
0
    def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", download_id="", inputCategory=None, failureLink=None):

        host = core.CFG[section][inputCategory]["host"]
        port = core.CFG[section][inputCategory]["port"]
        apikey = core.CFG[section][inputCategory]["apikey"]
        method = core.CFG[section][inputCategory]["method"]
        delete_failed = int(core.CFG[section][inputCategory]["delete_failed"])
        wait_for = int(core.CFG[section][inputCategory]["wait_for"])

        try:
            ssl = int(core.CFG[section][inputCategory]["ssl"])
        except:
            ssl = 0
        try:
            web_root = core.CFG[section][inputCategory]["web_root"]
        except:
            web_root = ""
        try:
            remote_path = int(core.CFG[section][inputCategory]["remote_path"])
        except:
            remote_path = 0
        try:
            extract = int(section[inputCategory]["extract"])
        except:
            extract = 0

        if ssl:
            protocol = "https://"
        else:
            protocol = "http://"

        baseURL = "%s%s:%s%s/api/%s" % (protocol, host, port, web_root, apikey)
        if not server_responding(baseURL):
            logger.error("Server did not respond. Exiting", section)
            return [1, "%s: Failed to post-process - %s did not respond." % (section, section) ]

        imdbid = find_imdbid(dirName, inputName)
        release = self.get_release(baseURL, imdbid, download_id)

        # pull info from release found if available
        release_id = None
        media_id = None
        downloader = None
        release_status_old = None
        if release and imdbid:
            try:
                release_id = release.keys()[0]
                media_id = release[release_id]['media_id']
                download_id = release[release_id]['download_info']['id']
                downloader = release[release_id]['download_info']['downloader']
                release_status_old = release[release_id]['status']
            except:
                pass

        if not os.path.isdir(dirName) and os.path.isfile(dirName): # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False) and listMediaFiles(dirName, media=False, audio=False, meta=False, archives=True) and extract:
            logger.debug('Checking for archives to extract in directory: %s' % (dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        good_files = 0
        num_files = 0
        # Check video files for corruption
        status = int(status)
        for video in listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False):
            num_files += 1
            if transcoder.isVideoGood(video, status):
                import_subs(video)
                good_files += 1
        if num_files > 0 and good_files == num_files:
            if status:
                logger.info("Status shown as failed from Downloader, but %s valid video files found. Setting as success." % (str(good_files)), section)
                status = 0
        elif num_files > 0 and good_files < num_files:
            logger.info("Status shown as success from Downloader, but corrupt video files found. Setting as failed.", section)
            if os.environ.has_key('NZBOP_VERSION') and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
                print('[NZB] MARK=BAD')
            if failureLink:
                failureLink = failureLink + '&corrupt=true'
            status = 1
        elif clientAgent == "manual":
            logger.warning("No media files found in directory %s to manually process." % (dirName), section)
            return [0, ""]  # Success (as far as this script is concerned)
        else:
            logger.warning("No media files found in directory %s. Processing this as a failed download" % (dirName), section)
            status = 1
            if os.environ.has_key('NZBOP_VERSION') and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
                print('[NZB] MARK=BAD')

        if status == 0:
            if core.TRANSCODE == 1:
                result, newDirName = transcoder.Transcode_directory(dirName)
                if result == 0:
                    logger.debug("Transcoding succeeded for files in %s" % (dirName), section)
                    dirName = newDirName
                else:
                    logger.error("Transcoding failed for files in %s" % (dirName), section)
                    return [1, "%s: Failed to post-process - Transcoding failed" % (section) ]
            for video in listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False): 
                if not release and not ".cp(tt" in video and imdbid:
                    videoName, videoExt = os.path.splitext(video)
                    video2 = "%s.cp(%s)%s" % (videoName, imdbid, videoExt)
                    logger.debug('Renaming: %s to: %s' % (video, video2))
                    os.rename(video, video2)        

            params = {}
            if download_id:
                params['downloader'] = downloader or clientAgent
                params['download_id'] = download_id

            params['media_folder'] = dirName
            if remote_path:
                params['media_folder'] = remoteDir(dirName)

            if method == "manage":
                command = "/manage.update"
                params = {}
            else:
                command = "/renamer.scan"

            url = "%s%s" % (baseURL, command)

            logger.debug("Opening URL: %s with PARAMS: %s" % (url, params), section)

            logger.postprocess("Starting %s scan for %s" % (method, inputName), section)

            try:
                r = requests.get(url, params=params, verify=False, timeout=(30, 1800))
            except requests.ConnectionError:
                logger.error("Unable to open URL", section)
                return [1, "%s: Failed to post-process - Unable to connect to %s" % (section, section) ]

            result = r.json()
            if not r.status_code in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
                logger.error("Server returned status %s" % (str(r.status_code)), section)
                return [1, "%s: Failed to post-process - Server returned status %s" % (section, str(r.status_code)) ]
            elif result['success']:
                logger.postprocess("SUCCESS: Finished %s scan for folder %s" % (method, dirName), section)
                if method == "manage":
                    return [0, "%s: Successfully post-processed %s" % (section, inputName) ]
            else:
                logger.error("FAILED: %s scan was unable to finish for folder %s. exiting!" % (method, dirName),
                             section)
                return [1, "%s: Failed to post-process - Server did not return success" % (section) ]

        else:
            logger.postprocess("FAILED DOWNLOAD DETECTED FOR %s" % (inputName), section)
            if failureLink:
                reportNzb(failureLink, clientAgent)

            if delete_failed and os.path.isdir(dirName) and not os.path.dirname(dirName) == dirName:
                logger.postprocess("Deleting failed files and folder %s" % dirName, section)
                rmDir(dirName)

            if not release_id and not media_id:
                logger.error("Could not find a downloaded movie in the database matching %s, exiting!" % inputName,
                             section)
                return [1, "%s: Failed to post-process - Failed download not found in %s" % (section, section) ]

            if release_id:
                logger.postprocess("Setting failed release %s to ignored ..." % (inputName), section)

                url = baseURL + "/release.ignore"
                params = {'id': release_id}

                logger.debug("Opening URL: %s with PARAMS: %s" % (url, params), section)

                try:
                    r = requests.get(url, params=params, verify=False, timeout=(30, 120))
                except requests.ConnectionError:
                    logger.error("Unable to open URL %s" % (url), section)
                    return [1, "%s: Failed to post-process - Unable to connect to %s" % (section, section) ]

                result = r.json()
                if not r.status_code in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
                    logger.error("Server returned status %s" % (str(r.status_code)), section)
                    return [1, "%s: Failed to post-process - Server returned status %s" % (section, str(r.status_code)) ]
                elif result['success']:
                    logger.postprocess("SUCCESS: %s has been set to ignored ..." % (inputName), section)
                else:
                    logger.warning("FAILED: Unable to set %s to ignored!" % (inputName), section)
                    return [1, "%s: Failed to post-process - Unable to set %s to ignored" % (section, inputName) ]

            logger.postprocess("Trying to snatch the next highest ranked release.", section)

            url = "%s/movie.searcher.try_next" % (baseURL)
            logger.debug("Opening URL: %s" % (url), section)

            try:
                r = requests.get(url, params={'media_id': media_id}, verify=False, timeout=(30, 600))
            except requests.ConnectionError:
                logger.error("Unable to open URL %s" % (url), section)
                return [1, "%s: Failed to post-process - Unable to connect to %s" % (section, section) ]

            result = r.json()
            if not r.status_code in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
                logger.error("Server returned status %s" % (str(r.status_code)), section)
                return [1, "%s: Failed to post-process - Server returned status %s" % (section, str(r.status_code)) ]
            elif result['success']:
                logger.postprocess("SUCCESS: Snatched the next highest release ...", section)
                return [0, "%s: Successfully snatched next highest release" % (section) ]
            else:
                logger.postprocess("SUCCESS: Unable to find a new release to snatch now. CP will keep searching!", section)
                return [0, "%s: No new release found now. %s will keep searching" % (section, section) ]

        # Added a releease that was not in the wanted list so confirm rename successful by finding this movie media.list.
        if not release:
            download_id = None  # we don't want to filter new releases based on this.

        # we will now check to see if CPS has finished renaming before returning to TorrentToMedia and unpausing.
        timeout = time.time() + 60 * wait_for
        while (time.time() < timeout):  # only wait 2 (default) minutes, then return.
            logger.postprocess("Checking for status change, please stand by ...", section)
            release = self.get_release(baseURL, imdbid, download_id, release_id)
            if release:
                try:
                    if release_id is None and release_status_old is None:  # we didn't have a release before, but now we do.
                        logger.postprocess("SUCCESS: Movie %s has now been added to CouchPotato" % (imdbid), section)
                        return [0, "%s: Successfully post-processed %s" % (section, inputName) ]

                    release_status_new = release[release_id]['status']
                    if release_status_new != release_status_old:
                        logger.postprocess("SUCCESS: Release %s has now been marked with a status of [%s]" % (
                            inputName, str(release_status_new).upper()), section)
                        return [0, "%s: Successfully post-processed %s" % (section, inputName) ]
                except:
                    pass
            if not os.path.isdir(dirName):
                logger.postprocess("SUCCESS: Input Directory [%s] has been processed and removed" % (
                    dirName), section)
                return [0, "%s: Successfully post-processed %s" % (section, inputName) ]

            elif not listMediaFiles(dirName, media=True, audio=False, meta=False, archives=True):
                logger.postprocess("SUCCESS: Input Directory [%s] has no remaining media files. This has been fully processed." % (
                    dirName), section)
                return [0, "%s: Successfully post-processed %s" % (section, inputName) ]

            # pause and let CouchPotatoServer 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 resule seeding now.
        logger.warning(
            "%s does not appear to have changed status after %s minutes, Please check your logs." % (inputName, wait_for),
            section)
        return [1, "%s: Failed to post-process - No change in status" % (section) ]
Esempio n. 2
0
    def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", inputCategory=None):
        status = int(status)

        host = core.CFG[section][inputCategory]["host"]
        port = core.CFG[section][inputCategory]["port"]
        apikey = core.CFG[section][inputCategory]["apikey"]
        wait_for = int(core.CFG[section][inputCategory]["wait_for"])

        try:
            ssl = int(core.CFG[section][inputCategory]["ssl"])
        except:
            ssl = 0
        try:
            web_root = core.CFG[section][inputCategory]["web_root"]
        except:
            web_root = ""
        try:
            remote_path = int(core.CFG[section][inputCategory]["remote_path"])
        except:
            remote_path = 0
        try:
            extract = int(section[inputCategory]["extract"])
        except:
            extract = 0

        if ssl:
            protocol = "https://"
        else:
            protocol = "http://"


        url = "%s%s:%s%s/api" % (protocol,host,port,web_root)
        if not server_responding(url):
            logger.error("Server did not respond. Exiting", section)
            return [1, "%s: Failed to post-process - %s did not respond." % (section, section) ]

        if not os.path.isdir(dirName) and os.path.isfile(dirName): # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and listMediaFiles(dirName, media=False, audio=False, meta=False, archives=True) and extract:
            logger.debug('Checking for archives to extract in directory: %s' % (dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        if listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and status:
            logger.info("Status shown as failed from Downloader, but %s valid video files found. Setting as successful." % (str(good_files)), section)
            status = 0

        if status == 0:

            params = {}
            params['apikey'] = apikey
            params['cmd'] = "forceProcess"

            params['dir'] = os.path.dirname(dirName)
            if remote_path:
                params['dir'] = remoteDir(os.path.dirname(dirName))

            release_status = self.get_status(url, apikey, dirName)
            if not release_status:
                logger.error("Could not find a status for %s, is it in the wanted list ?" % (inputName),section)

            logger.debug("Opening URL: %s with PARAMS: %s" % (url, params), section)

            try:
                r = requests.get(url, params=params, verify=False, timeout=(30, 600))
            except requests.ConnectionError:
                logger.error("Unable to open URL %s" % (url) ,section)
                return [1, "%s: Failed to post-process - Unable to connect to %s" % (section, section) ]

            logger.debug("Result: %s" % (r.text),section)

            if not r.status_code in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
                logger.error("Server returned status %s" % (str(r.status_code)), section)
                return [1, "%s: Failed to post-process - Server returned status %s" % (section, str(r.status_code)) ]
            elif r.text == "OK":
                logger.postprocess("SUCCESS: Post-Processing started for %s in folder %s ..." % (inputName, dirName),section) 
            else:
                logger.error("FAILED: Post-Processing has NOT started for %s in folder %s. exiting!" % (inputName, dirName),section)
                return [1, "%s: Failed to post-process - Returned log from %s was not as expected." % (section, section) ]

        else:
            logger.warning("FAILED DOWNLOAD DETECTED", section)
            return [1, "%s: Failed to post-process. %s does not support failed downloads" % (section, section) ]

        # we will now wait for this album to be processed before returning to TorrentToMedia and unpausing.
        timeout = time.time() + 60 * wait_for
        while (time.time() < timeout):
            current_status = self.get_status(url, apikey, dirName)
            if current_status is not None and current_status != release_status:  # Something has changed. CPS must have processed this movie.
                logger.postprocess("SUCCESS: This release is now marked as status [%s]" % (current_status),section)
                return [0, "%s: Successfully post-processed %s" % (section, inputName) ]
            if not os.path.isdir(dirName):
                logger.postprocess("SUCCESS: The input directory %s has been removed Processing must have finished." % (dirName),section)
                return [0, "%s: Successfully post-processed %s" % (section, inputName) ]
            time.sleep(10 * wait_for)

        # The status hasn't changed. uTorrent can resume seeding now.
        logger.warning("The music album does not appear to have changed status after %s minutes. Please check your Logs" % (wait_for),section)
        return [1, "%s: Failed to post-process - No change in wanted status" % (section) ]
Esempio n. 3
0
    def process(self,
                section,
                dirName,
                inputName=None,
                status=0,
                clientAgent="manual",
                download_id="",
                inputCategory=None,
                failureLink=None):

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        apikey = cfg["apikey"]
        method = cfg["method"]
        delete_failed = int(cfg["delete_failed"])
        wait_for = int(cfg["wait_for"])
        ssl = int(cfg.get("ssl", 0))
        web_root = cfg.get("web_root", "")
        remote_path = int(cfg.get("remote_path", 0))
        extract = int(cfg.get("extract", 0))
        protocol = "https://" if ssl else "http://"

        baseURL = "{0}{1}:{2}{3}/api/{4}".format(protocol, host, port,
                                                 web_root, apikey)
        if not server_responding(baseURL):
            logger.error("Server did not respond. Exiting", section)
            return [
                1, "{0}: Failed to post-process - {1} did not respond.".format(
                    section, section)
            ]

        imdbid = find_imdbid(dirName, inputName)
        release = self.get_release(baseURL, imdbid, download_id)

        # pull info from release found if available
        release_id = None
        media_id = None
        downloader = None
        release_status_old = None
        if release and imdbid:
            try:
                release_id = release.keys()[0]
                media_id = release[release_id]['media_id']
                download_id = release[release_id]['download_info']['id']
                downloader = release[release_id]['download_info']['downloader']
                release_status_old = release[release_id]['status']
            except:
                pass

        if not os.path.isdir(dirName) and os.path.isfile(
                dirName
        ):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(
                dirName, media=True, audio=False, meta=False,
                archives=False) and listMediaFiles(dirName,
                                                   media=False,
                                                   audio=False,
                                                   meta=False,
                                                   archives=True) and extract:
            logger.debug(
                'Checking for archives to extract in directory: {0}'.format(
                    dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        good_files = 0
        num_files = 0
        # Check video files for corruption
        status = int(status)
        for video in listMediaFiles(dirName,
                                    media=True,
                                    audio=False,
                                    meta=False,
                                    archives=False):
            num_files += 1
            if transcoder.isVideoGood(video, status):
                import_subs(video)
                good_files += 1
        if num_files and good_files == num_files:
            if status:
                logger.info(
                    "Status shown as failed from Downloader, but {0} valid video files found. Setting as success."
                    .format(good_files), section)
                status = 0
        elif num_files and good_files < num_files:
            logger.info(
                "Status shown as success from Downloader, but corrupt video files found. Setting as failed.",
                section)
            if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][
                    0:5] >= '14.0':
                print('[NZB] MARK=BAD')
            if failureLink:
                failureLink += '&corrupt=true'
            status = 1
        elif clientAgent == "manual":
            logger.warning(
                "No media files found in directory {0} to manually process.".
                format(dirName), section)
            return [0, ""]  # Success (as far as this script is concerned)
        else:
            logger.warning(
                "No media files found in directory {0}. Processing this as a failed download"
                .format(dirName), section)
            status = 1
            if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][
                    0:5] >= '14.0':
                print('[NZB] MARK=BAD')

        if status == 0:
            if core.TRANSCODE == 1:
                result, newDirName = transcoder.Transcode_directory(dirName)
                if result == 0:
                    logger.debug(
                        "Transcoding succeeded for files in {0}".format(
                            dirName), section)
                    dirName = newDirName

                    chmod_directory = int(str(cfg.get("chmodDirectory", "0")),
                                          8)
                    logger.debug(
                        "Config setting 'chmodDirectory' currently set to {0}".
                        format(oct(chmod_directory)), section)
                    if chmod_directory:
                        logger.info(
                            "Attempting to set the octal permission of '{0}' on directory '{1}'"
                            .format(oct(chmod_directory), dirName), section)
                        core.rchmod(dirName, chmod_directory)
                else:
                    logger.error(
                        "Transcoding failed for files in {0}".format(dirName),
                        section)
                    return [
                        1, "{0}: Failed to post-process - Transcoding failed".
                        format(section)
                    ]
            for video in listMediaFiles(dirName,
                                        media=True,
                                        audio=False,
                                        meta=False,
                                        archives=False):
                if not release and ".cp(tt" not in video and imdbid:
                    videoName, videoExt = os.path.splitext(video)
                    video2 = "{0}.cp({1}){2}".format(videoName, imdbid,
                                                     videoExt)
                    if not (clientAgent
                            in [core.TORRENT_CLIENTAGENT, 'manual']
                            and core.USELINK == 'move-sym'):
                        logger.debug('Renaming: {0} to: {1}'.format(
                            video, video2))
                        os.rename(video, video2)

            params = {}
            if download_id:
                params['downloader'] = downloader or clientAgent
                params['download_id'] = download_id

            params['media_folder'] = remoteDir(
                dirName) if remote_path else dirName

            if method == "manage":
                command = "/manage.update"
                params = {}
            else:
                command = "/renamer.scan"

            url = "{0}{1}".format(baseURL, command)

            logger.debug(
                "Opening URL: {0} with PARAMS: {1}".format(url, params),
                section)

            logger.postprocess(
                "Starting {0} scan for {1}".format(method, inputName), section)

            try:
                r = requests.get(url,
                                 params=params,
                                 verify=False,
                                 timeout=(30, 1800))
            except requests.ConnectionError:
                logger.error("Unable to open URL", section)
                return [
                    1,
                    "{0}: Failed to post-process - Unable to connect to {1}".
                    format(section, section)
                ]

            result = r.json()
            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 [
                    1,
                    "{0}: Failed to post-process - Server returned status {1}".
                    format(section, r.status_code)
                ]
            elif result['success']:
                logger.postprocess(
                    "SUCCESS: Finished {0} scan for folder {1}".format(
                        method, dirName), section)
                if method == "manage":
                    return [
                        0, "{0}: Successfully post-processed {1}".format(
                            section, inputName)
                    ]
            else:
                logger.error(
                    "FAILED: {0} scan was unable to finish for folder {1}. exiting!"
                    .format(method, dirName), section)
                return [
                    1,
                    "{0}: Failed to post-process - Server did not return success"
                    .format(section)
                ]

        else:
            core.FAILED = True
            logger.postprocess(
                "FAILED DOWNLOAD DETECTED FOR {0}".format(inputName), section)
            if failureLink:
                reportNzb(failureLink, clientAgent)

            if delete_failed and os.path.isdir(
                    dirName) and not os.path.dirname(dirName) == dirName:
                logger.postprocess(
                    "Deleting failed files and folder {0}".format(dirName),
                    section)
                rmDir(dirName)

            if not release_id and not media_id:
                logger.error(
                    "Could not find a downloaded movie in the database matching {0}, exiting!"
                    .format(inputName), section)
                return [
                    1,
                    "{0}: Failed to post-process - Failed download not found in {1}"
                    .format(section, section)
                ]

            if release_id:
                logger.postprocess(
                    "Setting failed release {0} to ignored ...".format(
                        inputName), section)

                url = "{url}/release.ignore".format(url=baseURL)
                params = {'id': release_id}

                logger.debug(
                    "Opening URL: {0} with PARAMS: {1}".format(url, params),
                    section)

                try:
                    r = requests.get(url,
                                     params=params,
                                     verify=False,
                                     timeout=(30, 120))
                except requests.ConnectionError:
                    logger.error("Unable to open URL {0}".format(url), section)
                    return [
                        1,
                        "{0}: Failed to post-process - Unable to connect to {1}"
                        .format(section, section)
                    ]

                result = r.json()
                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 [
                        1,
                        "{0}: Failed to post-process - Server returned status {1}"
                        .format(section, r.status_code)
                    ]
                elif result['success']:
                    logger.postprocess(
                        "SUCCESS: {0} has been set to ignored ...".format(
                            inputName), section)
                else:
                    logger.warning(
                        "FAILED: Unable to set {0} to ignored!".format(
                            inputName), section)
                    return [
                        1,
                        "{0}: Failed to post-process - Unable to set {1} to ignored"
                        .format(section, inputName)
                    ]

            logger.postprocess(
                "Trying to snatch the next highest ranked release.", section)

            url = "{0}/movie.searcher.try_next".format(baseURL)
            logger.debug("Opening URL: {0}".format(url), section)

            try:
                r = requests.get(url,
                                 params={'media_id': media_id},
                                 verify=False,
                                 timeout=(30, 600))
            except requests.ConnectionError:
                logger.error("Unable to open URL {0}".format(url), section)
                return [
                    1,
                    "{0}: Failed to post-process - Unable to connect to {1}".
                    format(section, section)
                ]

            result = r.json()
            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 [
                    1,
                    "{0}: Failed to post-process - Server returned status {1}".
                    format(section, r.status_code)
                ]
            elif result['success']:
                logger.postprocess(
                    "SUCCESS: Snatched the next highest release ...", section)
                return [
                    0,
                    "{0}: Successfully snatched next highest release".format(
                        section)
                ]
            else:
                logger.postprocess(
                    "SUCCESS: Unable to find a new release to snatch now. CP will keep searching!",
                    section)
                return [
                    0,
                    "{0}: No new release found now. {1} will keep searching".
                    format(section, section)
                ]

        # Added a release that was not in the wanted list so confirm rename successful by finding this movie media.list.
        if not release:
            download_id = None  # we don't want to filter new releases based on this.

        # we will now check to see if CPS has finished renaming before returning to TorrentToMedia and unpausing.
        timeout = time.time() + 60 * wait_for
        while time.time(
        ) < timeout:  # only wait 2 (default) minutes, then return.
            logger.postprocess(
                "Checking for status change, please stand by ...", section)
            release = self.get_release(baseURL, imdbid, download_id,
                                       release_id)
            if release:
                try:
                    if release_id is None and release_status_old is None:  # we didn't have a release before, but now we do.
                        logger.postprocess(
                            "SUCCESS: Movie {0} has now been added to CouchPotato"
                            .format(imdbid), section)
                        return [
                            0, "{0}: Successfully post-processed {1}".format(
                                section, inputName)
                        ]

                    release_status_new = release[release_id]['status']
                    if release_status_new != release_status_old:
                        logger.postprocess(
                            "SUCCESS: Release {0} has now been marked with a status of [{1}]"
                            .format(inputName,
                                    str(release_status_new).upper()), section)
                        return [
                            0, "{0}: Successfully post-processed {1}".format(
                                section, inputName)
                        ]
                except:
                    pass
            if not os.path.isdir(dirName):
                logger.postprocess(
                    "SUCCESS: Input Directory [{0}] has been processed and removed"
                    .format(dirName), section)
                return [
                    0, "{0}: Successfully post-processed {1}".format(
                        section, inputName)
                ]

            elif not listMediaFiles(
                    dirName, media=True, audio=False, meta=False,
                    archives=True):
                logger.postprocess(
                    "SUCCESS: Input Directory [{0}] has no remaining media files. This has been fully processed."
                    .format(dirName), section)
                return [
                    0, "{0}: Successfully post-processed {1}".format(
                        section, inputName)
                ]

            # pause and let CouchPotatoServer 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.
        logger.warning(
            "{0} does not appear to have changed status after {1} minutes, Please check your logs."
            .format(inputName, wait_for), section)
        return [
            1,
            "{0}: Failed to post-process - No change in status".format(section)
        ]
Esempio n. 4
0
class autoProcessTV:
    def command_complete(self, url, params, headers, section):
        r = None
        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: %s" % (url1), section)
            return None
        if not r.status_code in [
                requests.codes.ok, requests.codes.created,
                requests.codes.accepted
        ]:
            logger.error("Server returned status %s" % (str(r.status_code)),
                         section)
            return None
        else:
            try:
                res = json.loads(r.content)
                return res['state']
            except:
                logger.error("%s did not return expected json data." % section,
                             section)
                return None

    def CDH(self, url2, headers):
        r = None
        try:
            r = requests.get(url2,
                             params={},
                             headers=headers,
                             stream=True,
                             verify=False,
                             timeout=(30, 60))
        except requests.ConnectionError:
            logger.error("Unable to open URL: %s" % (url2), section)
            return False
        if not r.status_code in [
                requests.codes.ok, requests.codes.created,
                requests.codes.accepted
        ]:
            logger.error("Server returned status %s" % (str(r.status_code)),
                         section)
            return False
        else:
            try:
                res = json.loads(r.content)
                return res["enableCompletedDownloadHandling"]
            except:
                return False

    def processEpisode(self,
                       section,
                       dirName,
                       inputName=None,
                       failed=False,
                       clientAgent="manual",
                       download_id=None,
                       inputCategory=None,
                       failureLink=None):
        host = core.CFG[section][inputCategory]["host"]
        port = core.CFG[section][inputCategory]["port"]
        try:
            ssl = int(core.CFG[section][inputCategory]["ssl"])
        except:
            ssl = 0
        if ssl:
            protocol = "https://"
        else:
            protocol = "http://"
        try:
            web_root = core.CFG[section][inputCategory]["web_root"]
        except:
            web_root = ""
        if not server_responding("%s%s:%s%s" %
                                 (protocol, host, port, web_root)):
            logger.error("Server did not respond. Exiting", section)
            return [
                1,
                "%s: Failed to post-process - %s did not respond." %
                (section, section)
            ]

        # auto-detect correct fork
        fork, fork_params = autoFork(section, inputCategory)

        try:
            username = core.CFG[section][inputCategory]["username"]
            password = core.CFG[section][inputCategory]["password"]
        except:
            username = ""
            password = ""
        try:
            apikey = core.CFG[section][inputCategory]["apikey"]
        except:
            apikey = ""
        try:
            delete_failed = int(
                core.CFG[section][inputCategory]["delete_failed"])
        except:
            delete_failed = 0
        try:
            nzbExtractionBy = core.CFG[section][inputCategory][
                "nzbExtractionBy"]
        except:
            nzbExtractionBy = "Downloader"
        try:
            process_method = core.CFG[section][inputCategory]["process_method"]
        except:
            process_method = None
        try:
            remote_path = int(core.CFG[section][inputCategory]["remote_path"])
        except:
            remote_path = 0
        try:
            wait_for = int(core.CFG[section][inputCategory]["wait_for"])
        except:
            wait_for = 2
        try:
            force = int(core.CFG[section][inputCategory]["force"])
        except:
            force = 0
        try:
            delete_on = int(core.CFG[section][inputCategory]["delete_on"])
        except:
            delete_on = 0
        try:
            extract = int(section[inputCategory]["extract"])
        except:
            extract = 0

        if not os.path.isdir(dirName) and os.path.isfile(
                dirName
        ):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        # Attempt to create the directory if it doesn't exist and ignore any
        # error stating that it already exists. This fixes a bug where SickRage
        # won't process the directory because it doesn't exist.
        try:
            os.makedirs(dirName)  # Attempt to create the directory
        except OSError, e:
            # Re-raise the error if it wasn't about the directory not existing
            if e.errno != errno.EEXIST:
                raise

        if fork not in core.SICKBEARD_TORRENT or (
                clientAgent in ['nzbget', 'sabnzbd']
                and nzbExtractionBy != "Destination"):
            if inputName:
                process_all_exceptions(inputName, dirName)
                inputName, dirName = convert_to_ascii(inputName, dirName)

            # Now check if tv files exist in destination.
            if not listMediaFiles(
                    dirName, media=True, audio=False, meta=False,
                    archives=False):
                if listMediaFiles(dirName,
                                  media=False,
                                  audio=False,
                                  meta=False,
                                  archives=True) and extract:
                    logger.debug(
                        'Checking for archives to extract in directory: %s' %
                        (dirName))
                    core.extractFiles(dirName)
                    inputName, dirName = convert_to_ascii(inputName, dirName)

            if listMediaFiles(
                    dirName, media=True, audio=False, meta=False,
                    archives=False
            ):  # Check that a video exists. if not, assume failed.
                flatten(dirName)

        # Check video files for corruption
        status = int(failed)
        good_files = 0
        num_files = 0
        for video in listMediaFiles(dirName,
                                    media=True,
                                    audio=False,
                                    meta=False,
                                    archives=False):
            num_files += 1
            if transcoder.isVideoGood(video, status):
                good_files += 1
                import_subs(video)
        if num_files > 0:
            if good_files == num_files and not status == 0:
                logger.info('Found Valid Videos. Setting status Success')
                status = 0
                failed = 0
            if good_files < num_files and status == 0:
                logger.info('Found corrupt videos. Setting status Failed')
                status = 1
                failed = 1
                if os.environ.has_key('NZBOP_VERSION') and os.environ[
                        'NZBOP_VERSION'][0:5] >= '14.0':
                    print('[NZB] MARK=BAD')
                if failureLink:
                    failureLink = failureLink + '&corrupt=true'
        elif clientAgent == "manual":
            logger.warning(
                "No media files found in directory %s to manually process." %
                (dirName), section)
            return [0, ""]  # Success (as far as this script is concerned)
        else:
            logger.warning(
                "No media files found in directory %s. Processing this as a failed download"
                % (dirName), section)
            status = 1
            failed = 1
            if os.environ.has_key('NZBOP_VERSION') and os.environ[
                    'NZBOP_VERSION'][0:5] >= '14.0':
                print('[NZB] MARK=BAD')

        if status == 0 and core.TRANSCODE == 1:  # only transcode successful downloads
            result, newDirName = transcoder.Transcode_directory(dirName)
            if result == 0:
                logger.debug(
                    "SUCCESS: Transcoding succeeded for files in %s" %
                    (dirName), section)
                dirName = newDirName
            else:
                logger.error(
                    "FAILED: Transcoding failed for files in %s" % (dirName),
                    section)
                return [
                    1,
                    "%s: Failed to post-process - Transcoding failed" %
                    (section)
                ]

        # configure SB params to pass
        fork_params['quiet'] = 1
        if inputName is not None:
            fork_params['nzbName'] = inputName

        for param in copy.copy(fork_params):
            if param == "failed":
                fork_params[param] = failed

            if param in ["dirName", "dir", "proc_dir"]:
                fork_params[param] = dirName
                if remote_path:
                    fork_params[param] = remoteDir(dirName)

            if param == "process_method":
                if process_method:
                    fork_params[param] = process_method
                else:
                    del fork_params[param]

            if param == "force":
                if force:
                    fork_params[param] = force
                else:
                    del fork_params[param]

            if param == "delete_on":
                if delete_on:
                    fork_params[param] = delete_on
                else:
                    del fork_params[param]

        # delete any unused params so we don't pass them to SB by mistake
        [fork_params.pop(k) for k, v in fork_params.items() if v is None]

        if status == 0:
            logger.postprocess(
                "SUCCESS: The download succeeded, sending a post-process request",
                section)
        else:
            core.FAILED = True
            if failureLink:
                reportNzb(failureLink, clientAgent)
            if fork in core.SICKBEARD_FAILED:
                logger.postprocess(
                    "FAILED: The download failed. Sending 'failed' process request to %s branch"
                    % (fork), section)
            elif section == "NzbDrone":
                logger.postprocess(
                    "FAILED: The download failed. Sending failed download to %s for CDH processing"
                    % (fork), section)
                return [
                    1,
                    "%s: Downlaod Failed. Sending back to %s" %
                    (section, section)
                ]  # Return as failed to flag this in the downloader.
            else:
                logger.postprocess(
                    "FAILED: The download failed. %s branch does not handle failed downloads. Nothing to process"
                    % (fork), section)
                if delete_failed and os.path.isdir(
                        dirName) and not os.path.dirname(dirName) == dirName:
                    logger.postprocess(
                        "Deleting failed files and folder %s" % (dirName),
                        section)
                    rmDir(dirName)
                return [
                    1,
                    "%s: Failed to post-process. %s does not support failed downloads"
                    % (section, section)
                ]  # Return as failed to flag this in the downloader.

        url = None
        if section == "SickBeard":
            url = "%s%s:%s%s/home/postprocess/processEpisode" % (
                protocol, host, port, web_root)
        elif section == "NzbDrone":
            url = "%s%s:%s%s/api/command" % (protocol, host, port, web_root)
            url2 = "%s%s:%s%s/api/config/downloadClient" % (protocol, host,
                                                            port, web_root)
            headers = {"X-Api-Key": apikey}
            params = {
                'sortKey': 'series.title',
                'page': 1,
                'pageSize': 1,
                'sortDir': 'asc'
            }
            if remote_path:
                logger.debug("remote_path: %s" % (remoteDir(dirName)), section)
                data = {
                    "name": "DownloadedEpisodesScan",
                    "path": remoteDir(dirName),
                    "downloadClientId": download_id
                }
            else:
                logger.debug("path: %s" % (dirName), section)
                data = {
                    "name": "DownloadedEpisodesScan",
                    "path": dirName,
                    "downloadClientId": download_id
                }
            if not download_id:
                data.pop("downloadClientId")
            data = json.dumps(data)

        try:
            if section == "SickBeard":
                logger.debug(
                    "Opening URL: %s with params: %s" %
                    (url, str(fork_params)), section)
                r = None
                s = requests.Session()
                login = "******" % (protocol, host, port, web_root)
                login_params = {'username': username, 'password': password}
                s.post(login,
                       data=login_params,
                       stream=True,
                       verify=False,
                       timeout=(30, 60))
                r = s.get(url,
                          auth=(username, password),
                          params=fork_params,
                          stream=True,
                          verify=False,
                          timeout=(30, 1800))
            elif section == "NzbDrone":
                logger.debug(
                    "Opening URL: %s with data: %s" % (url, str(data)),
                    section)
                r = None
                r = requests.post(url,
                                  data=data,
                                  headers=headers,
                                  stream=True,
                                  verify=False,
                                  timeout=(30, 1800))
        except requests.ConnectionError:
            logger.error("Unable to open URL: %s" % (url), section)
            return [
                1,
                "%s: Failed to post-process - Unable to connect to %s" %
                (section, section)
            ]

        if not r.status_code in [
                requests.codes.ok, requests.codes.created,
                requests.codes.accepted
        ]:
            logger.error("Server returned status %s" % (str(r.status_code)),
                         section)
            return [
                1,
                "%s: Failed to post-process - Server returned status %s" %
                (section, str(r.status_code))
            ]

        Success = False
        Started = False
        if section == "SickBeard":
            for line in r.iter_lines():
                if line:
                    logger.postprocess("%s" % (line), section)
                    if "Moving file from" in line:
                        inputName = os.path.split(line)[1]
                    if "Processing succeeded" in line or "Successfully processed" in line:
                        Success = True
        elif section == "NzbDrone":
            try:
                res = json.loads(r.content)
                scan_id = int(res['id'])
                logger.debug("Scan started with id: %s" % (str(scan_id)),
                             section)
                Started = True
            except Exception as e:
                logger.warning("No scan id was returned due to: %s" % (e),
                               section)
                scan_id = None
                Started = False

        if status != 0 and delete_failed and not os.path.dirname(
                dirName) == dirName:
            logger.postprocess(
                "Deleting failed files and folder %s" % (dirName), section)
            rmDir(dirName)

        if Success:
            return [
                0,
                "%s: Successfully post-processed %s" % (section, inputName)
            ]
        elif section == "NzbDrone" and Started:
            n = 0
            params = {}
            url = url + "/" + str(scan_id)
            while n < 6:  # set up wait_for minutes to see if command completes..
                time.sleep(10 * wait_for)
                command_status = self.command_complete(url, params, headers,
                                                       section)
                if command_status and command_status in [
                        'completed', 'failed'
                ]:
                    break
                n += 1
            if command_status:
                logger.debug(
                    "The Scan command return status: %s" % (command_status),
                    section)
            if not os.path.exists(dirName):
                logger.debug(
                    "The directory %s has been removed. Renaming was successful."
                    % (dirName), section)
                return [
                    0,
                    "%s: Successfully post-processed %s" % (section, inputName)
                ]
            elif command_status and command_status in ['completed']:
                logger.debug(
                    "The Scan command has completed successfully. Renaming was successful.",
                    section)
                return [
                    0,
                    "%s: Successfully post-processed %s" % (section, inputName)
                ]
            elif command_status and 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 self.CDH(url2, headers):
                logger.debug(
                    "The Scan command did not return status completed, but complete Download Handling is enabled. Passing back to %s."
                    % (section), section)
                return [
                    status,
                    "%s: Complete DownLoad Handling is enabled. Passing back to %s"
                    % (section, section)
                ]
            else:
                logger.warning(
                    "The Scan command did not return a valid status. Renaming was not successful.",
                    section)
                return [
                    1,
                    "%s: Failed to post-process %s" % (section, inputName)
                ]
        else:
            return [
                1,
                "%s: Failed to post-process - Returned log from %s was not as expected."
                % (section, section)
            ]  # We did not receive Success confirmation.
Esempio n. 5
0
    def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", inputCategory=None):
        status = int(status)

        host = core.CFG[section][inputCategory]["host"]
        port = core.CFG[section][inputCategory]["port"]
        apikey = core.CFG[section][inputCategory]["apikey"]
        wait_for = int(core.CFG[section][inputCategory]["wait_for"])

        try:
            ssl = int(core.CFG[section][inputCategory]["ssl"])
        except:
            ssl = 0
        try:
            web_root = core.CFG[section][inputCategory]["web_root"]
        except:
            web_root = ""
        try:
            remote_path = int(core.CFG[section][inputCategory]["remote_path"])
        except:
            remote_path = 0
        try:
            extract = int(section[inputCategory]["extract"])
        except:
            extract = 0

        if ssl:
            protocol = "https://"
        else:
            protocol = "http://"


        url = "%s%s:%s%s/api" % (protocol,host,port,web_root)
        if not server_responding(url):
            logger.error("Server did not respond. Exiting", section)
            return [1, "%s: Failed to post-process - %s did not respond." % (section, section) ]

        if not os.path.isdir(dirName) and os.path.isfile(dirName): # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and listMediaFiles(dirName, media=False, audio=False, meta=False, archives=True) and extract:
            logger.debug('Checking for archives to extract in directory: %s' % (dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        if listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and status:
            logger.info("Status shown as failed from Downloader, but %s valid video files found. Setting as successful." % (str(good_files)), section)
            status = 0

        if status == 0:

            params = {}
            params['apikey'] = apikey
            params['cmd'] = "forceProcess"

            params['dir'] = os.path.dirname(dirName)
            if remote_path:
                params['dir'] = remoteDir(os.path.dirname(dirName))

            release_status = self.get_status(url, apikey, dirName)
            if not release_status:
                logger.error("Could not find a status for %s, is it in the wanted list ?" % (inputName),section)

            logger.debug("Opening URL: %s with PARAMS: %s" % (url, params), section)

            try:
                r = requests.get(url, params=params, verify=False, timeout=(30, 300))
            except requests.ConnectionError:
                logger.error("Unable to open URL %s" % (url) ,section)
                return [1, "%s: Failed to post-process - Unable to connect to %s" % (section, section) ]

            logger.debug("Result: %s" % (r.text),section)

            if not r.status_code in [requests.codes.ok, requests.codes.created, requests.codes.accepted]:
                logger.error("Server returned status %s" % (str(r.status_code)), section)
                return [1, "%s: Failed to post-process - Server returned status %s" % (section, str(r.status_code)) ]
            elif r.text == "OK":
                logger.postprocess("SUCCESS: Post-Processing started for %s in folder %s ..." % (inputName, dirName),section) 
            else:
                logger.error("FAILED: Post-Processing has NOT started for %s in folder %s. exiting!" % (inputName, dirName),section)
                return [1, "%s: Failed to post-process - Returned log from %s was not as expected." % (section, section) ]

        else:
            logger.warning("FAILED DOWNLOAD DETECTED", section)
            return [1, "%s: Failed to post-process. %s does not support failed downloads" % (section, section) ]

        # we will now wait for this album to be processed before returning to TorrentToMedia and unpausing.
        timeout = time.time() + 60 * wait_for
        while (time.time() < timeout):
            current_status = self.get_status(url, apikey, dirName)
            if current_status is not None and current_status != release_status:  # Something has changed. CPS must have processed this movie.
                logger.postprocess("SUCCESS: This release is now marked as status [%s]" % (current_status),section)
                return [0, "%s: Successfully post-processed %s" % (section, inputName) ]
            if not os.path.isdir(dirName):
                logger.postprocess("SUCCESS: The input directory %s has been removed Processing must have finished." % (dirName),section)
                return [0, "%s: Successfully post-processed %s" % (section, inputName) ]
            time.sleep(10 * wait_for)

        # The status hasn't changed. uTorrent can resume seeding now.
        logger.warning("The music album does not appear to have changed status after %s minutes. Please check your Logs" % (wait_for),section)
        return [1, "%s: Failed to post-process - No change in wanted status" % (section) ]
Esempio n. 6
0
    def process(self,
                section,
                dirName,
                inputName=None,
                status=0,
                clientAgent="manual",
                inputCategory=None):
        status = int(status)

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        apikey = cfg["apikey"]
        wait_for = int(cfg["wait_for"])
        ssl = int(cfg.get("ssl", 0))
        delete_failed = int(cfg["delete_failed"])
        web_root = cfg.get("web_root", "")
        remote_path = int(cfg.get("remote_path", 0))
        protocol = "https://" if ssl else "http://"
        status = int(status)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))

        if section == "Lidarr":
            url = "{0}{1}:{2}{3}/api/v1".format(protocol, host, port, web_root)
        else:
            url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root)
        if not server_responding(url):
            logger.error("Server did not respond. Exiting", section)
            return [
                1, "{0}: Failed to post-process - {1} did not respond.".format(
                    section, section)
            ]

        if not os.path.isdir(dirName) and os.path.isfile(
                dirName
        ):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(
                dirName, media=False, audio=True, meta=False,
                archives=False) and listMediaFiles(dirName,
                                                   media=False,
                                                   audio=False,
                                                   meta=False,
                                                   archives=True) and extract:
            logger.debug(
                'Checking for archives to extract in directory: {0}'.format(
                    dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        #if listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and status:
        #    logger.info("Status shown as failed from Downloader, but valid video files found. Setting as successful.", section)
        #    status = 0

        if status == 0 and section == "HeadPhones":

            params = {
                'apikey': apikey,
                'cmd': "forceProcess",
                'dir': remoteDir(dirName) if remote_path else dirName
            }

            res = self.forceProcess(params, url, apikey, inputName, dirName,
                                    section, wait_for)
            if res[0] in [0, 1]:
                return res

            params = {
                'apikey':
                apikey,
                'cmd':
                "forceProcess",
                'dir':
                os.path.split(remoteDir(dirName))[0]
                if remote_path else os.path.split(dirName)[0]
            }

            res = self.forceProcess(params, url, apikey, inputName, dirName,
                                    section, wait_for)
            if res[0] in [0, 1]:
                return res

            # The status hasn't changed. uTorrent can resume seeding now.
            logger.warning(
                "The music album does not appear to have changed status after {0} minutes. Please check your Logs"
                .format(wait_for), section)
            return [
                1, "{0}: Failed to post-process - No change in wanted status".
                format(section)
            ]

        elif status == 0 and section == "Lidarr":
            url = "{0}{1}:{2}{3}/api/v1/command".format(
                protocol, host, port, web_root)
            headers = {"X-Api-Key": apikey}
            if remote_path:
                logger.debug("remote_path: {0}".format(remoteDir(dirName)),
                             section)
                data = {"name": "Rename", "path": remoteDir(dirName)}
            else:
                logger.debug("path: {0}".format(dirName), section)
                data = {"name": "Rename", "path": dirName}
            data = json.dumps(data)
            try:
                logger.debug(
                    "Opening URL: {0} with data: {1}".format(url, data),
                    section)
                r = requests.post(url,
                                  data=data,
                                  headers=headers,
                                  stream=True,
                                  verify=False,
                                  timeout=(30, 1800))
            except requests.ConnectionError:
                logger.error("Unable to open URL: {0}".format(url), section)
                return [
                    1,
                    "{0}: Failed to post-process - Unable to connect to {1}".
                    format(section, section)
                ]

            Success = False
            Queued = False
            Started = False
            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
                Started = False
                return [
                    1, "{0}: Failed to post-process - Unable to start scan".
                    format(section)
                ]

            n = 0
            params = {}
            url = "{0}/{1}".format(url, scan_id)
            while n < 6:  # set up wait_for minutes to see if command completes..
                time.sleep(10 * wait_for)
                command_status = self.command_complete(url, params, headers,
                                                       section)
                if command_status and command_status in [
                        'completed', 'failed'
                ]:
                    break
                n += 1
            if command_status:
                logger.debug(
                    "The Scan command return status: {0}".format(
                        command_status), section)
            if not os.path.exists(dirName):
                logger.debug(
                    "The directory {0} has been removed. Renaming was successful."
                    .format(dirName), section)
                return [
                    0, "{0}: Successfully post-processed {1}".format(
                        section, inputName)
                ]
            elif command_status and 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 and 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) ]
            else:
                logger.debug(
                    "The Scan command did not return status completed. Passing back to {0} to attempt complete download handling."
                    .format(section), section)
                return [
                    status,
                    "{0}: Passing back to {1} to attempt Complete Download Handling"
                    .format(section, section)
                ]

        else:
            if section == "Lidarr":
                logger.postprocess(
                    "FAILED: The download failed. Sending failed download to {0} for CDH processing"
                    .format(section), section)
                return [
                    1, "{0}: Download Failed. Sending back to {1}".format(
                        section, section)
                ]  # Return as failed to flag this in the downloader.
            else:
                logger.warning("FAILED DOWNLOAD DETECTED", section)
                if delete_failed and os.path.isdir(
                        dirName) and not os.path.dirname(dirName) == dirName:
                    logger.postprocess(
                        "Deleting failed files and folder {0}".format(dirName),
                        section)
                    rmDir(dirName)
                return [
                    1,
                    "{0}: Failed to post-process. {1} does not support failed downloads"
                    .format(section, section)
                ]  # Return as failed to flag this in the downloader.
Esempio n. 7
0
    def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", download_id="", inputCategory=None, failureLink=None):

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        apikey = cfg["apikey"]
        method = cfg["method"]
        delete_failed = int(cfg["delete_failed"])
        wait_for = int(cfg["wait_for"])
        ssl = int(cfg.get("ssl", 0))
        web_root = cfg.get("web_root", "")
        remote_path = int(cfg.get("remote_path", 0))
        protocol = "https://" if ssl else "http://"
        status = int(status)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))

        baseURL = "{0}{1}:{2}{3}/api/{4}".format(protocol, host, port, web_root, apikey)
        if not server_responding(baseURL):
            logger.error("Server did not respond. Exiting", section)
            return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]

        imdbid = find_imdbid(dirName, inputName)
        release = self.get_release(baseURL, imdbid, download_id)

        # pull info from release found if available
        release_id = None
        media_id = None
        downloader = None
        release_status_old = None
        if release and imdbid:
            try:
                release_id = release.keys()[0]
                media_id = release[release_id]['media_id']
                download_id = release[release_id]['download_info']['id']
                downloader = release[release_id]['download_info']['downloader']
                release_status_old = release[release_id]['status']
            except:
                pass

        if not os.path.isdir(dirName) and os.path.isfile(dirName):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False) and listMediaFiles(dirName, media=False, audio=False, meta=False, archives=True) and extract:
            logger.debug('Checking for archives to extract in directory: {0}'.format(dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        good_files = 0
        num_files = 0
        # Check video files for corruption
        for video in listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False):
            num_files += 1
            if transcoder.isVideoGood(video, status):
                import_subs(video)
                good_files += 1
        if num_files and good_files == num_files:
            if status:
                logger.info("Status shown as failed from Downloader, but {0} valid video files found. Setting as success.".format(good_files), section)
                status = 0
        elif num_files and good_files < num_files:
            logger.info("Status shown as success from Downloader, but corrupt video files found. Setting as failed.", section)
            if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
                print('[NZB] MARK=BAD')
            if failureLink:
                failureLink += '&corrupt=true'
            status = 1
        elif clientAgent == "manual":
            logger.warning("No media files found in directory {0} to manually process.".format(dirName), section)
            return [0, ""]  # Success (as far as this script is concerned)
        else:
            logger.warning("No media files found in directory {0}. Processing this as a failed download".format(dirName), section)
            status = 1
            if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
                print('[NZB] MARK=BAD')

        if status == 0:
            if core.TRANSCODE == 1:
                result, newDirName = transcoder.Transcode_directory(dirName)
                if result == 0:
                    logger.debug("Transcoding succeeded for files in {0}".format(dirName), section)
                    dirName = newDirName

                    chmod_directory = int(str(cfg.get("chmodDirectory", "0")), 8)
                    logger.debug("Config setting 'chmodDirectory' currently set to {0}".format(oct(chmod_directory)), section)
                    if chmod_directory:
                        logger.info("Attempting to set the octal permission of '{0}' on directory '{1}'".format(oct(chmod_directory), dirName), section)
                        core.rchmod(dirName, chmod_directory)
                else:
                    logger.error("Transcoding failed for files in {0}".format(dirName), section)
                    return [1, "{0}: Failed to post-process - Transcoding failed".format(section)]
            for video in listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False):
                if not release and ".cp(tt" not in video and imdbid:
                    videoName, videoExt = os.path.splitext(video)
                    video2 = "{0}.cp({1}){2}".format(videoName, imdbid, videoExt)
                    if not (clientAgent in [core.TORRENT_CLIENTAGENT, 'manual'] and core.USELINK == 'move-sym'):
                        logger.debug('Renaming: {0} to: {1}'.format(video, video2))
                        os.rename(video, video2)

            params = {}
            if download_id:
                params['downloader'] = downloader or clientAgent
                params['download_id'] = download_id

            params['media_folder'] = remoteDir(dirName) if remote_path else dirName

            if method == "manage":
                command = "/manage.update"
                params = {}
            else:
                command = "/renamer.scan"

            url = "{0}{1}".format(baseURL, command)

            logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)

            logger.postprocess("Starting {0} scan for {1}".format(method, inputName), section)

            try:
                r = requests.get(url, params=params, verify=False, timeout=(30, 1800))
            except requests.ConnectionError:
                logger.error("Unable to open URL", section)
                return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]

            result = r.json()
            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 [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]
            elif result['success']:
                logger.postprocess("SUCCESS: Finished {0} scan for folder {1}".format(method, dirName), section)
                if method == "manage":
                    return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]
            else:
                logger.error("FAILED: {0} scan was unable to finish for folder {1}. exiting!".format(method, dirName),
                             section)
                return [1, "{0}: Failed to post-process - Server did not return success".format(section)]

        else:
            core.FAILED = True
            logger.postprocess("FAILED DOWNLOAD DETECTED FOR {0}".format(inputName), section)
            if failureLink:
                reportNzb(failureLink, clientAgent)

            if delete_failed and os.path.isdir(dirName) and not os.path.dirname(dirName) == dirName:
                logger.postprocess("Deleting failed files and folder {0}".format(dirName), section)
                rmDir(dirName)

            if not release_id and not media_id:
                logger.error("Could not find a downloaded movie in the database matching {0}, exiting!".format(inputName),
                             section)
                return [1, "{0}: Failed to post-process - Failed download not found in {1}".format(section, section)]

            if release_id:
                logger.postprocess("Setting failed release {0} to ignored ...".format(inputName), section)

                url = "{url}/release.ignore".format(url=baseURL)
                params = {'id': release_id}

                logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)

                try:
                    r = requests.get(url, params=params, verify=False, timeout=(30, 120))
                except requests.ConnectionError:
                    logger.error("Unable to open URL {0}".format(url), section)
                    return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]

                result = r.json()
                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 [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]
                elif result['success']:
                    logger.postprocess("SUCCESS: {0} has been set to ignored ...".format(inputName), section)
                else:
                    logger.warning("FAILED: Unable to set {0} to ignored!".format(inputName), section)
                    return [1, "{0}: Failed to post-process - Unable to set {1} to ignored".format(section, inputName)]

            logger.postprocess("Trying to snatch the next highest ranked release.", section)

            url = "{0}/movie.searcher.try_next".format(baseURL)
            logger.debug("Opening URL: {0}".format(url), section)

            try:
                r = requests.get(url, params={'media_id': media_id}, verify=False, timeout=(30, 600))
            except requests.ConnectionError:
                logger.error("Unable to open URL {0}".format(url), section)
                return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]

            result = r.json()
            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 [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]
            elif result['success']:
                logger.postprocess("SUCCESS: Snatched the next highest release ...", section)
                return [0, "{0}: Successfully snatched next highest release".format(section)]
            else:
                logger.postprocess("SUCCESS: Unable to find a new release to snatch now. CP will keep searching!", section)
                return [0, "{0}: No new release found now. {1} will keep searching".format(section, section)]

        # Added a release that was not in the wanted list so confirm rename successful by finding this movie media.list.
        if not release:
            download_id = None  # we don't want to filter new releases based on this.

        # we will now check to see if CPS has finished renaming before returning to TorrentToMedia and unpausing.
        timeout = time.time() + 60 * wait_for
        while time.time() < timeout:  # only wait 2 (default) minutes, then return.
            logger.postprocess("Checking for status change, please stand by ...", section)
            release = self.get_release(baseURL, imdbid, download_id, release_id)
            if release:
                try:
                    if release_id is None and release_status_old is None:  # we didn't have a release before, but now we do.
                        logger.postprocess("SUCCESS: Movie {0} has now been added to CouchPotato".format(imdbid), section)
                        return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]

                    release_status_new = release[release_id]['status']
                    if release_status_new != release_status_old:
                        logger.postprocess("SUCCESS: Release {0} has now been marked with a status of [{1}]".format(
                            inputName, str(release_status_new).upper()), section)
                        return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]
                except:
                    pass
            if not os.path.isdir(dirName):
                logger.postprocess("SUCCESS: Input Directory [{0}] has been processed and removed".format(
                    dirName), section)
                return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]

            elif not listMediaFiles(dirName, media=True, audio=False, meta=False, archives=True):
                logger.postprocess("SUCCESS: Input Directory [{0}] has no remaining media files. This has been fully processed.".format(
                    dirName), section)
                return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]

            # pause and let CouchPotatoServer 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.
        logger.warning(
            "{0} does not appear to have changed status after {1} minutes, Please check your logs.".format(inputName, wait_for),
            section)
        return [1, "{0}: Failed to post-process - No change in status".format(section)]
Esempio n. 8
0
    def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", inputCategory=None):
        status = int(status)

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        apikey = cfg["apikey"]
        wait_for = int(cfg["wait_for"])
        ssl = int(cfg.get("ssl", 0))
        delete_failed = int(cfg["delete_failed"])
        web_root = cfg.get("web_root", "")
        remote_path = int(cfg.get("remote_path", 0))
        protocol = "https://" if ssl else "http://"
        status = int(status)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))

        if section == "Lidarr":
            url = "{0}{1}:{2}{3}/api/v1".format(protocol, host, port, web_root)
        else:
            url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root)
        if not server_responding(url):
            logger.error("Server did not respond. Exiting", section)
            return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]

        if not os.path.isdir(dirName) and os.path.isfile(dirName):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and listMediaFiles(dirName, media=False, audio=False, meta=False, archives=True) and extract:
            logger.debug('Checking for archives to extract in directory: {0}'.format(dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        #if listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and status:
        #    logger.info("Status shown as failed from Downloader, but valid video files found. Setting as successful.", section)
        #    status = 0

        if status == 0 and section == "HeadPhones":

            params = {
                'apikey': apikey,
                'cmd': "forceProcess",
                'dir': remoteDir(dirName) if remote_path else dirName
            }

            res = self.forceProcess(params, url, apikey, inputName, dirName, section, wait_for)
            if res[0] in [0, 1]:
                return res

            params = {
                'apikey': apikey,
                'cmd': "forceProcess",
                'dir': os.path.split(remoteDir(dirName))[0] if remote_path else os.path.split(dirName)[0]
            }

            res = self.forceProcess(params, url, apikey, inputName, dirName, section, wait_for)
            if res[0] in [0, 1]:
                return res

            # The status hasn't changed. uTorrent can resume seeding now.
            logger.warning("The music album does not appear to have changed status after {0} minutes. Please check your Logs".format(wait_for), section)
            return [1, "{0}: Failed to post-process - No change in wanted status".format(section)]

        elif status == 0 and section == "Lidarr":
            url = "{0}{1}:{2}{3}/api/v1/command".format(protocol, host, port, web_root)
            headers = {"X-Api-Key": apikey}
            if remote_path:
                logger.debug("remote_path: {0}".format(remoteDir(dirName)), section)
                data = {"name": "Rename", "path": remoteDir(dirName)}
            else:
                logger.debug("path: {0}".format(dirName), section)
                data = {"name": "Rename", "path": dirName}
            data = json.dumps(data)
            try:
                logger.debug("Opening URL: {0} with data: {1}".format(url, data), section)
                r = requests.post(url, data=data, headers=headers, stream=True, verify=False, timeout=(30, 1800))
            except requests.ConnectionError:
                logger.error("Unable to open URL: {0}".format(url), section)
                return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]

            Success = False
            Queued = False
            Started = False
            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
                Started = False
                return [1, "{0}: Failed to post-process - Unable to start scan".format(section)]

            n = 0
            params = {}
            url = "{0}/{1}".format(url, scan_id)
            while n < 6:  # set up wait_for minutes to see if command completes..
                time.sleep(10 * wait_for)
                command_status = self.command_complete(url, params, headers, section)
                if command_status and command_status in ['completed', 'failed']:
                    break
                n += 1
            if command_status:
                logger.debug("The Scan command return status: {0}".format(command_status), section)
            if not os.path.exists(dirName):
                logger.debug("The directory {0} has been removed. Renaming was successful.".format(dirName), section)
                return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]
            elif command_status and 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 and 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) ]
            else:
                logger.debug("The Scan command did not return status completed. Passing back to {0} to attempt complete download handling.".format(section), section)
                return [status, "{0}: Passing back to {1} to attempt Complete Download Handling".format(section, section)]

        else:
            if section == "Lidarr":
                logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(section), section)
                return [1, "{0}: Download Failed. Sending back to {1}".format(section, section)]  # Return as failed to flag this in the downloader.
            else:
                logger.warning("FAILED DOWNLOAD DETECTED", section)
                if delete_failed and os.path.isdir(dirName) and not os.path.dirname(dirName) == dirName:
                    logger.postprocess("Deleting failed files and folder {0}".format(dirName), section)
                    rmDir(dirName)
                return [1, "{0}: Failed to post-process. {1} does not support failed downloads".format(section, section)]  # Return as failed to flag this in the downloader.
Esempio n. 9
0
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
    status = 1  # 1 = failed | 0 = success
    root = 0
    foundFile = 0
    uniquePath = 1

    if clientAgent != 'manual' and not core.DOWNLOADINFO:
        logger.debug('Adding TORRENT download info for directory %s to database' % (inputDirectory))

        myDB = nzbToMediaDB.DBConnection()

        encoded = False
        inputDirectory1 = inputDirectory
        inputName1 = inputName

        try:
            encoded, inputDirectory1 = CharReplace(inputDirectory)
            encoded, inputName1 = CharReplace(inputName)
        except:
            pass

        controlValueDict = {"input_directory": unicode(inputDirectory1)}
        newValueDict = {"input_name": unicode(inputName1),
                        "input_hash": unicode(inputHash),
                        "input_id": unicode(inputID),
                        "client_agent": unicode(clientAgent),
                        "status": 0,
                        "last_update": datetime.date.today().toordinal()
        }
        myDB.upsert("downloads", newValueDict, controlValueDict)

    logger.debug("Received Directory: %s | Name: %s | Category: %s" % (inputDirectory, inputName, inputCategory))

    inputDirectory, inputName, inputCategory, root = core.category_search(inputDirectory, inputName,
                                                                                        inputCategory, root,
                                                                                        core.CATEGORIES)  # Confirm the category by parsing directory structure 
    if inputCategory == "":
        inputCategory = "UNCAT"

    usercat = inputCategory
    try:
        inputName = inputName.encode(core.SYS_ENCODING)
    except: pass
    try:
        inputDirectory = inputDirectory.encode(core.SYS_ENCODING)
    except: pass

    logger.debug("Determined Directory: %s | Name: %s | Category: %s" % (inputDirectory, inputName, inputCategory))

    # auto-detect section
    section = core.CFG.findsection(inputCategory).isenabled()
    if section is None:
        section = core.CFG.findsection("ALL").isenabled()
        if section is None:
            logger.error(
                'Category:[%s] is not defined or is not enabled. Please rename it or ensure it is enabled for the appropriate section in your autoProcessMedia.cfg and try again.' % (
                    inputCategory))
            return [-1, ""]
        else:
            usercat = "ALL"

    if len(section) > 1:
        logger.error(
            'Category:[%s] is not unique, %s are using it. Please rename it or disable all other sections using the same category name in your autoProcessMedia.cfg and try again.' % (
                usercat, section.keys()))
        return [-1, ""]

    if section:
        sectionName = section.keys()[0]
        logger.info('Auto-detected SECTION:%s' % (sectionName))
    else:
        logger.error("Unable to locate a section with subsection:%s enabled in your autoProcessMedia.cfg, exiting!" % (
            inputCategory))
        return [-1, ""]

    try:
        Torrent_NoLink = int(section[usercat]["Torrent_NoLink"])
    except:
        Torrent_NoLink = 0

    try:
        keep_archive = int(section[usercat]["keep_archive"])
    except:
        keep_archive = 0

    try:
        extract = int(section[usercat]['extract'])
    except:
        extract = 0

    try:
        uniquePath = int(section[usercat]["unique_path"])
    except:
        uniquePath = 1

    if clientAgent != 'manual':
        core.pause_torrent(clientAgent, inputHash, inputID, inputName)

    # Incase input is not directory, make sure to create one.
    # This way Processing is isolated.
    if not os.path.isdir(os.path.join(inputDirectory, inputName)):
        basename = os.path.basename(inputDirectory)
        basename = core.sanitizeName(inputName) \
            if inputName == basename else os.path.splitext(core.sanitizeName(inputName)[0])
        outputDestination = os.path.join(core.OUTPUTDIRECTORY, inputCategory, basename)
    elif uniquePath:
        outputDestination = os.path.normpath(
            core.os.path.join(core.OUTPUTDIRECTORY, inputCategory, core.sanitizeName(inputName)))
    else:
        outputDestination = os.path.normpath(
            core.os.path.join(core.OUTPUTDIRECTORY, inputCategory))
    try:
        outputDestination = outputDestination.encode(core.SYS_ENCODING)
    except: pass

    if outputDestination in inputDirectory:
        outputDestination = inputDirectory

    logger.info("Output directory set to: %s" % (outputDestination))

    if core.SAFE_MODE and outputDestination == core.TORRENT_DEFAULTDIR:
        logger.error(
            'The output directory:[%s] is the Download Directory. Edit outputDirectory in autoProcessMedia.cfg. Exiting' % (
            inputDirectory))
        return [-1, ""]

    logger.debug("Scanning files in directory: %s" % (inputDirectory))

    if sectionName == 'HeadPhones':
        core.NOFLATTEN.extend(
            inputCategory)  # Make sure we preserve folder structure for HeadPhones.

    now = datetime.datetime.now()

    if extract == 1:
        inputFiles = core.listMediaFiles(inputDirectory, archives=False)
    else:
        inputFiles = core.listMediaFiles(inputDirectory)
    logger.debug("Found %s files in %s" % (str(len(inputFiles)), inputDirectory))
    for inputFile in inputFiles:
        filePath = os.path.dirname(inputFile)
        fileName, fileExt = os.path.splitext(os.path.basename(inputFile))
        fullFileName = os.path.basename(inputFile)

        targetFile = core.os.path.join(outputDestination, fullFileName)
        if inputCategory in core.NOFLATTEN:
            if not os.path.basename(filePath) in outputDestination:
                targetFile = core.os.path.join(
                    core.os.path.join(outputDestination, os.path.basename(filePath)), fullFileName)
                logger.debug(
                    "Setting outputDestination to %s to preserve folder structure" % (os.path.dirname(targetFile)))
        try:
            targetFile = targetFile.encode(core.SYS_ENCODING)
        except: pass
        if root == 1:
            if not foundFile:
                logger.debug("Looking for %s in: %s" % (inputName, inputFile))
            if (core.sanitizeName(inputName) in core.sanitizeName(inputFile)) or (
                        core.sanitizeName(fileName) in core.sanitizeName(inputName)):
                foundFile = True
                logger.debug("Found file %s that matches Torrent Name %s" % (fullFileName, inputName))
            else:
                continue

        if root == 2:
            mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(inputFile))
            ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(inputFile))

            if not foundFile:
                logger.debug("Looking for files with modified/created dates less than 5 minutes old.")
            if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)):
                foundFile = True
                logger.debug("Found file %s with date modifed/created less than 5 minutes ago." % (fullFileName))
            else:
                continue  # This file has not been recently moved or created, skip it

        if Torrent_NoLink == 0:
            try:
                core.copy_link(inputFile, targetFile, core.USELINK)
                core.rmReadOnly(targetFile)
            except:
                logger.error("Failed to link: %s to %s" % (inputFile, targetFile))

    inputName, outputDestination = convert_to_ascii(inputName, outputDestination)

    if extract == 1:
        logger.debug('Checking for archives to extract in directory: %s' % (inputDirectory))
        core.extractFiles(inputDirectory, outputDestination, keep_archive)

    if not inputCategory in core.NOFLATTEN:  #don't flatten hp in case multi cd albums, and we need to copy this back later.
        core.flatten(outputDestination)

    # Now check if video files exist in destination:
    if sectionName in ["SickBeard", "NzbDrone", "CouchPotato"]:
        numVideos = len(
            core.listMediaFiles(outputDestination, media=True, audio=False, meta=False, archives=False))
        if numVideos > 0:
            logger.info("Found %s media files in %s" % (numVideos, outputDestination))
            status = 0
        elif extract != 1:
            logger.info("Found no media files in %s. Sending to %s to process" % (outputDestination, sectionName))
            status = 0
        else:
            logger.warning("Found no media files in %s" % outputDestination)

    # Only these sections can handling failed downloads so make sure everything else gets through without the check for failed
    if not sectionName in ['CouchPotato', 'SickBeard', 'NzbDrone']:
        status = 0

    logger.info("Calling %s:%s to post-process:%s" % (sectionName, usercat, inputName))

    if core.TORRENT_CHMOD_DIRECTORY:
        core.rchmod(outputDestination, core.TORRENT_CHMOD_DIRECTORY)

    result = [ 0, "" ]
    if sectionName == 'UserScript':
        result = external_script(outputDestination, inputName, inputCategory, section[usercat])

    elif sectionName == 'CouchPotato':
        result = core.autoProcessMovie().process(sectionName,outputDestination, inputName, status, clientAgent, inputHash,
                                                       inputCategory)
    elif sectionName in ['SickBeard','NzbDrone']:
        if inputHash:
            inputHash = inputHash.upper()
        result = core.autoProcessTV().processEpisode(sectionName,outputDestination, inputName, status, clientAgent,
                                                           inputHash, inputCategory)
    elif sectionName == 'HeadPhones':
        result = core.autoProcessMusic().process(sectionName,outputDestination, inputName, status, clientAgent, inputCategory)
    elif sectionName == 'Mylar':
        result = core.autoProcessComics().processEpisode(sectionName,outputDestination, inputName, status, clientAgent,
                                                               inputCategory)
    elif sectionName == 'Gamez':
        result = core.autoProcessGames().process(sectionName,outputDestination, inputName, status, clientAgent, inputCategory)

    plex_update(inputCategory)

    if result[0] != 0:
        if not core.TORRENT_RESUME_ON_FAILURE:
            logger.error("A problem was reported in the autoProcess* script. torrent won't resume seeding (settings)")
        elif clientAgent != 'manual':
            logger.error(
                "A problem was reported in the autoProcess* script. If torrent was paused we will resume seeding")
            core.resume_torrent(clientAgent, inputHash, inputID, inputName)

    else:
        if clientAgent != 'manual':
            # update download status in our DB
            core.update_downloadInfoStatus(inputName, 1)

            # remove torrent
            if core.USELINK == 'move-sym' and not core.DELETE_ORIGINAL == 1:
                logger.debug('Checking for sym-links to re-direct in: %s' % (inputDirectory))
                for dirpath, dirs, files in os.walk(inputDirectory):
                    for file in files:
                        logger.debug('Checking symlink: %s' % (os.path.join(dirpath,file)))
                        core.replace_links(os.path.join(dirpath,file))
            core.remove_torrent(clientAgent, inputHash, inputID, inputName)

        if not sectionName == 'UserScript':  # for user script, we assume this is cleaned by the script or option USER_SCRIPT_CLEAN
            # cleanup our processing folders of any misc unwanted files and empty directories
            core.cleanDir(outputDestination, sectionName, inputCategory)

    return result
Esempio n. 10
0
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
    status = 1  # 1 = failed | 0 = success
    root = 0
    foundFile = 0
    uniquePath = 1

    if clientAgent != 'manual' and not core.DOWNLOADINFO:
        logger.debug('Adding TORRENT download info for directory %s to database' % (inputDirectory))

        myDB = nzbToMediaDB.DBConnection()

        encoded, inputDirectory1 = CharReplace(inputDirectory)
        encoded, inputName1 = CharReplace(inputName)

        controlValueDict = {"input_directory": unicode(inputDirectory1)}
        newValueDict = {"input_name": unicode(inputName1),
                        "input_hash": unicode(inputHash),
                        "input_id": unicode(inputID),
                        "client_agent": unicode(clientAgent),
                        "status": 0,
                        "last_update": datetime.date.today().toordinal()
        }
        myDB.upsert("downloads", newValueDict, controlValueDict)

    logger.debug("Received Directory: %s | Name: %s | Category: %s" % (inputDirectory, inputName, inputCategory))

    inputDirectory, inputName, inputCategory, root = core.category_search(inputDirectory, inputName,
                                                                                        inputCategory, root,
                                                                                        core.CATEGORIES)  # Confirm the category by parsing directory structure 
    if inputCategory == "":
        inputCategory = "UNCAT"

    usercat = inputCategory
    try:
        inputName = inputName.encode(core.SYS_ENCODING)
    except: pass
    try:
        inputDirectory = inputDirectory.encode(core.SYS_ENCODING)
    except: pass

    logger.debug("Determined Directory: %s | Name: %s | Category: %s" % (inputDirectory, inputName, inputCategory))

    # auto-detect section
    section = core.CFG.findsection(inputCategory).isenabled()
    if section is None:
        section = core.CFG.findsection("ALL").isenabled()
        if section is None:
            logger.error(
                'Category:[%s] is not defined or is not enabled. Please rename it or ensure it is enabled for the appropriate section in your autoProcessMedia.cfg and try again.' % (
                    inputCategory))
            return [-1, ""]
        else:
            usercat = "ALL"

    if len(section) > 1:
        logger.error(
            'Category:[%s] is not unique, %s are using it. Please rename it or disable all other sections using the same category name in your autoProcessMedia.cfg and try again.' % (
                usercat, section.keys()))
        return [-1, ""]

    if section:
        sectionName = section.keys()[0]
        logger.info('Auto-detected SECTION:%s' % (sectionName))
    else:
        logger.error("Unable to locate a section with subsection:%s enabled in your autoProcessMedia.cfg, exiting!" % (
            inputCategory))
        return [-1, ""]

    try:
        Torrent_NoLink = int(section[usercat]["Torrent_NoLink"])
    except:
        Torrent_NoLink = 0

    try:
        extract = int(section[usercat]['extract'])
    except:
        extract = 0

    try:
        uniquePath = int(section[usercat]["unique_path"])
    except:
        uniquePath = 1

    if clientAgent != 'manual':
        core.pause_torrent(clientAgent, inputHash, inputID, inputName)

    # Incase input is not directory, make sure to create one.
    # This way Processing is isolated.
    if not os.path.isdir(os.path.join(inputDirectory, inputName)):
        basename = os.path.splitext(core.sanitizeName(inputName))[0]
        outputDestination = os.path.join(core.OUTPUTDIRECTORY, inputCategory, basename)
    elif uniquePath:
        outputDestination = os.path.normpath(
            core.os.path.join(core.OUTPUTDIRECTORY, inputCategory, core.sanitizeName(inputName)))
    else:
        outputDestination = os.path.normpath(
            core.os.path.join(core.OUTPUTDIRECTORY, inputCategory))
    try:
        outputDestination = outputDestination.encode(core.SYS_ENCODING)
    except: pass

    logger.info("Output directory set to: %s" % (outputDestination))

    if core.SAFE_MODE and outputDestination == core.TORRENT_DEFAULTDIR:
        logger.error(
            'The output directory:[%s] is the Download Directory. Edit outputDirectory in autoProcessMedia.cfg. Exiting' % (
            inputDirectory))
        return [-1, ""]

    logger.debug("Scanning files in directory: %s" % (inputDirectory))

    if sectionName == 'HeadPhones':
        core.NOFLATTEN.extend(
            inputCategory)  # Make sure we preserve folder structure for HeadPhones.

    now = datetime.datetime.now()

    inputFiles = core.listMediaFiles(inputDirectory)
    logger.debug("Found %s files in %s" % (str(len(inputFiles)), inputDirectory))
    for inputFile in inputFiles:
        filePath = os.path.dirname(inputFile)
        fileName, fileExt = os.path.splitext(os.path.basename(inputFile))
        fullFileName = os.path.basename(inputFile)

        targetFile = core.os.path.join(outputDestination, fullFileName)
        if inputCategory in core.NOFLATTEN:
            if not os.path.basename(filePath) in outputDestination:
                targetFile = core.os.path.join(
                    core.os.path.join(outputDestination, os.path.basename(filePath)), fullFileName)
                logger.debug(
                    "Setting outputDestination to %s to preserve folder structure" % (os.path.dirname(targetFile)))
        try:
            targetFile = targetFile.encode(core.SYS_ENCODING)
        except: pass
        if root == 1:
            if not foundFile:
                logger.debug("Looking for %s in: %s" % (inputName, inputFile))
            if (core.sanitizeName(inputName) in core.sanitizeName(inputFile)) or (
                        core.sanitizeName(fileName) in core.sanitizeName(inputName)):
                foundFile = True
                logger.debug("Found file %s that matches Torrent Name %s" % (fullFileName, inputName))
            else:
                continue

        if root == 2:
            mtime_lapse = now - datetime.datetime.fromtimestamp(os.path.getmtime(inputFile))
            ctime_lapse = now - datetime.datetime.fromtimestamp(os.path.getctime(inputFile))

            if not foundFile:
                logger.debug("Looking for files with modified/created dates less than 5 minutes old.")
            if (mtime_lapse < datetime.timedelta(minutes=5)) or (ctime_lapse < datetime.timedelta(minutes=5)):
                foundFile = True
                logger.debug("Found file %s with date modifed/created less than 5 minutes ago." % (fullFileName))
            else:
                continue  # This file has not been recently moved or created, skip it

        if Torrent_NoLink == 0:
            try:
                core.copy_link(inputFile, targetFile, core.USELINK)
                core.rmReadOnly(targetFile)
            except:
                logger.error("Failed to link: %s to %s" % (inputFile, targetFile))

    inputName, outputDestination = convert_to_ascii(inputName, outputDestination)

    if extract == 1:
        logger.debug('Checking for archives to extract in directory: %s' % (outputDestination))
        core.extractFiles(outputDestination)

    if not inputCategory in core.NOFLATTEN:  #don't flatten hp in case multi cd albums, and we need to copy this back later.
        core.flatten(outputDestination)

    # Now check if video files exist in destination:
    if sectionName in ["SickBeard", "NzbDrone", "CouchPotato"]:
        numVideos = len(
            core.listMediaFiles(outputDestination, media=True, audio=False, meta=False, archives=False))
        if numVideos > 0:
            logger.info("Found %s media files in %s" % (numVideos, outputDestination))
            status = 0
        elif extract != 1:
            logger.info("Found no media files in %s. Sending to %s to process" % (outputDestination, sectionName))
            status = 0
        else:
            logger.warning("Found no media files in %s" % outputDestination)

    # Only these sections can handling failed downloads so make sure everything else gets through without the check for failed
    if not sectionName in ['CouchPotato', 'SickBeard', 'NzbDrone']:
        status = 0

    logger.info("Calling %s:%s to post-process:%s" % (sectionName, usercat, inputName))

    if core.TORRENT_CHMOD_DIRECTORY:
        core.rchmod(outputDestination, core.TORRENT_CHMOD_DIRECTORY)

    result = [ 0, "" ]
    if sectionName == 'UserScript':
        result = external_script(outputDestination, inputName, inputCategory, section[usercat])

    elif sectionName == 'CouchPotato':
        result = core.autoProcessMovie().process(sectionName,outputDestination, inputName, status, clientAgent, inputHash,
                                                       inputCategory)
    elif sectionName in ['SickBeard','NzbDrone']:
        if inputHash:
            inputHash = inputHash.upper()
        result = core.autoProcessTV().processEpisode(sectionName,outputDestination, inputName, status, clientAgent,
                                                           inputHash, inputCategory)
    elif sectionName == 'HeadPhones':
        result = core.autoProcessMusic().process(sectionName,outputDestination, inputName, status, clientAgent, inputCategory)
    elif sectionName == 'Mylar':
        result = core.autoProcessComics().processEpisode(sectionName,outputDestination, inputName, status, clientAgent,
                                                               inputCategory)
    elif sectionName == 'Gamez':
        result = core.autoProcessGames().process(sectionName,outputDestination, inputName, status, clientAgent, inputCategory)

    plex_update(inputCategory)

    if result[0] != 0:
        if not core.TORRENT_RESUME_ON_FAILURE:
            logger.error("A problem was reported in the autoProcess* script. torrent won't resume seeding (settings)")
        elif clientAgent != 'manual':
            logger.error(
                "A problem was reported in the autoProcess* script. If torrent was paused we will resume seeding")
            core.resume_torrent(clientAgent, inputHash, inputID, inputName)

    else:
        if clientAgent != 'manual':
            # update download status in our DB
            core.update_downloadInfoStatus(inputName, 1)

            # remove torrent
            core.remove_torrent(clientAgent, inputHash, inputID, inputName)

        if not sectionName == 'UserScript':  # for user script, we assume this is cleaned by the script or option USER_SCRIPT_CLEAN
            # cleanup our processing folders of any misc unwanted files and empty directories
            core.cleanDir(outputDestination, sectionName, inputCategory)

    return result
Esempio n. 11
0
    def process(self,
                section,
                dirName,
                inputName=None,
                status=0,
                clientAgent="manual",
                inputCategory=None):
        status = int(status)

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        apikey = cfg["apikey"]
        wait_for = int(cfg["wait_for"])
        ssl = int(cfg.get("ssl", 0))
        web_root = cfg.get("web_root", "")
        remote_path = int(cfg.get("remote_path", 0))
        protocol = "https://" if ssl else "http://"
        status = int(status)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))

        url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root)
        if not server_responding(url):
            logger.error("Server did not respond. Exiting", section)
            return [
                1, "{0}: Failed to post-process - {1} did not respond.".format(
                    section, section)
            ]

        if not os.path.isdir(dirName) and os.path.isfile(
                dirName
        ):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(
                dirName, media=False, audio=True, meta=False,
                archives=False) and listMediaFiles(dirName,
                                                   media=False,
                                                   audio=False,
                                                   meta=False,
                                                   archives=True) and extract:
            logger.debug(
                'Checking for archives to extract in directory: {0}'.format(
                    dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        if listMediaFiles(
                dirName, media=False, audio=True, meta=False,
                archives=False) and status:
            logger.info(
                "Status shown as failed from Downloader, but valid video files found. Setting as successful.",
                section)
            status = 0

        if status == 0:

            params = {
                'apikey': apikey,
                'cmd': "forceProcess",
                'dir': remoteDir(dirName) if remote_path else dirName
            }

            res = self.forceProcess(params, url, apikey, inputName, dirName,
                                    section, wait_for)
            if res[0] in [0, 1]:
                return res

            params = {
                'apikey':
                apikey,
                'cmd':
                "forceProcess",
                'dir':
                os.path.split(remoteDir(dirName))[0]
                if remote_path else os.path.split(dirName)[0]
            }

            res = self.forceProcess(params, url, apikey, inputName, dirName,
                                    section, wait_for)
            if res[0] in [0, 1]:
                return res

            # The status hasn't changed. uTorrent can resume seeding now.
            logger.warning(
                "The music album does not appear to have changed status after {0} minutes. Please check your Logs"
                .format(wait_for), section)
            return [
                1, "{0}: Failed to post-process - No change in wanted status".
                format(section)
            ]

        else:
            logger.warning("FAILED DOWNLOAD DETECTED", section)
            return [
                1,
                "{0}: Failed to post-process. {1} does not support failed downloads"
                .format(section, section)
            ]
Esempio n. 12
0
    def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", inputCategory=None):
        status = int(status)

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        apikey = cfg["apikey"]
        wait_for = int(cfg["wait_for"])
        ssl = int(cfg.get("ssl", 0))
        web_root = cfg.get("web_root", "")
        remote_path = int(cfg.get("remote_path", 0))
        protocol = "https://" if ssl else "http://"
        status = int(status)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))

        url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root)
        if not server_responding(url):
            logger.error("Server did not respond. Exiting", section)
            return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]

        if not os.path.isdir(dirName) and os.path.isfile(dirName):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and listMediaFiles(dirName, media=False, audio=False, meta=False, archives=True) and extract:
            logger.debug('Checking for archives to extract in directory: {0}'.format(dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        if listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and status:
            logger.info("Status shown as failed from Downloader, but valid video files found. Setting as successful.", section)
            status = 0

        if status == 0:

            params = {
                'apikey': apikey,
                'cmd': "forceProcess",
                'dir': remoteDir(dirName) if remote_path else dirName
            }

            release_status = self.get_status(url, apikey, dirName)
            if not release_status:
                logger.error("Could not find a status for {0}, is it in the wanted list ?".format(inputName), section)

            logger.debug("Opening URL: {0} with PARAMS: {1}".format(url, params), section)

            try:
                r = requests.get(url, params=params, verify=False, timeout=(30, 300))
            except requests.ConnectionError:
                logger.error("Unable to open URL {0}".format(url), section)
                return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]

            logger.debug("Result: {0}".format(r.text), section)

            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 [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]
            elif r.text == "OK":
                logger.postprocess("SUCCESS: Post-Processing started for {0} in folder {1} ...".format(inputName, dirName), section)
            else:
                logger.error("FAILED: Post-Processing has NOT started for {0} in folder {1}. exiting!".format(inputName, dirName), section)
                return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)]

        else:
            logger.warning("FAILED DOWNLOAD DETECTED", section)
            return [1, "{0}: Failed to post-process. {1} does not support failed downloads".format(section, section)]

        # we will now wait for this album to be processed before returning to TorrentToMedia and unpausing.
        timeout = time.time() + 60 * wait_for
        while time.time() < timeout:
            current_status = self.get_status(url, apikey, dirName)
            if current_status is not None and current_status != release_status:  # Something has changed. CPS must have processed this movie.
                logger.postprocess("SUCCESS: This release is now marked as status [{0}]".format(current_status), section)
                return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]
            if not os.path.isdir(dirName):
                logger.postprocess("SUCCESS: The input directory {0} has been removed Processing must have finished.".format(dirName), section)
                return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]
            time.sleep(10 * wait_for)

        # The status hasn't changed. uTorrent can resume seeding now.
        logger.warning("The music album does not appear to have changed status after {0} minutes. Please check your Logs".format(wait_for), section)
        return [1, "{0}: Failed to post-process - No change in wanted status".format(section)]
Esempio n. 13
0
    def checkfiles(self,
                   section,
                   dirName,
                   inputName=None,
                   failed=False,
                   clientAgent="manual",
                   download_id=None,
                   inputCategory=None,
                   failureLink=None):
        cfg = dict(core.CFG[section][inputCategory])
        # auto-detect correct fork
        #fork, fork_params = autoFork(section, inputCategory)

        nzbExtractionBy = cfg.get("nzbExtractionBy", "Downloader")
        status = int(failed)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))

        if not os.path.isdir(dirName) and os.path.isfile(
                dirName
        ):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        # Attempt to create the directory if it doesn't exist and ignore any
        # error stating that it already exists. This fixes a bug where SickRage
        # won't process the directory because it doesn't exist.
        try:
            os.makedirs(dirName)  # Attempt to create the directory
        except OSError as e:
            # Re-raise the error if it wasn't about the directory not existing
            if e.errno != errno.EEXIST:
                raise

        #if 'process_method' not in fork_params or (clientAgent in ['nzbget', 'sabnzbd'] and nzbExtractionBy != "Destination"):
        #if inputName:
        #    process_all_exceptions(inputName, dirName)
        #    inputName, dirName = convert_to_ascii(inputName, dirName)

        # Now check if tv files exist in destination.
            if not listMediaFiles(
                    dirName, media=True, audio=False, meta=False,
                    archives=False):
                if listMediaFiles(dirName,
                                  media=False,
                                  audio=False,
                                  meta=False,
                                  archives=True) and extract:
                    logger.debug(
                        'Checking for archives to extract in directory: {0}'.
                        format(dirName))
                    core.extractFiles(dirName)
                    inputName, dirName = convert_to_ascii(inputName, dirName)

            if listMediaFiles(
                    dirName, media=True, audio=False, meta=False,
                    archives=False
            ):  # Check that a video exists. if not, assume failed.
                flatten(dirName)

        # Check video files for corruption
        good_files = 0
        num_files = 0
        for video in listMediaFiles(dirName,
                                    media=True,
                                    audio=False,
                                    meta=False,
                                    archives=False):
            num_files += 1
            if transcoder.isVideoGood(video, status):
                good_files += 1
                import_subs(video)
        if num_files > 0:
            if good_files == num_files and not status == 0:
                logger.info('Found Valid Videos. Setting status Success')
                status = 0
                failed = 0
            if good_files < num_files and status == 0:
                logger.info('Found corrupt videos. Setting status Failed')
                status = 1
                failed = 1
                if 'NZBOP_VERSION' in os.environ and os.environ[
                        'NZBOP_VERSION'][0:5] >= '14.0':
                    print('[NZB] MARK=BAD')
                if failureLink:
                    failureLink += '&corrupt=true'

        elif nzbExtractionBy == "Destination":
            logger.info(
                "Check for media files ignored because nzbExtractionBy is set to Destination."
            )
            if int(failed) == 0:
                logger.info("Setting Status Success.")
                status = 0
                failed = 0
            else:
                logger.info(
                    "Downloader reported an error during download or verification. Processing this as a failed download."
                )
                status = 1
                failed = 1
        else:
            logger.warning(
                "No media files found in directory {0}. Processing this as a failed download"
                .format(dirName), section)
            status = 1
            failed = 1

        return status
Esempio n. 14
0
    def processEpisode(self, section, dirName, inputName=None, failed=False, clientAgent="manual", download_id=None, inputCategory=None, failureLink=None):

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        ssl = int(cfg.get("ssl", 0))
        web_root = cfg.get("web_root", "")
        protocol = "https://" if ssl else "http://"
        username = cfg.get("username", "")
        password = cfg.get("password", "")
        apikey = cfg.get("apikey", "")

        if server_responding("{0}{1}:{2}{3}".format(protocol, host, port, web_root)):
            # auto-detect correct fork
            fork, fork_params = autoFork(section, inputCategory)
        elif not username and not apikey:
            logger.info('No SickBeard username or Sonarr apikey entered. Performing transcoder functions only')
            fork, fork_params = "None", {}
        else:
            logger.error("Server did not respond. Exiting", section)
            return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]

        delete_failed = int(cfg.get("delete_failed", 0))
        nzbExtractionBy = cfg.get("nzbExtractionBy", "Downloader")
        process_method = cfg.get("process_method")
        if  clientAgent == core.TORRENT_CLIENTAGENT and core.USELINK == "move-sym":
            process_method = "symlink"
        remote_path = int(cfg.get("remote_path", 0))
        wait_for = int(cfg.get("wait_for", 2))
        force = int(cfg.get("force", 0))
        delete_on = int(cfg.get("delete_on", 0))
        ignore_subs = int(cfg.get("ignore_subs", 0))
        status = int(failed)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))
        #get importmode, default to "Move" for consistency with legacy
        importMode = cfg.get("importMode","Move")

        if not os.path.isdir(dirName) and os.path.isfile(dirName):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        # Attempt to create the directory if it doesn't exist and ignore any
        # error stating that it already exists. This fixes a bug where SickRage
        # won't process the directory because it doesn't exist.
        try:
            os.makedirs(dirName)  # Attempt to create the directory
        except OSError as e:
            # Re-raise the error if it wasn't about the directory not existing
            if e.errno != errno.EEXIST:
                raise

        if 'process_method' not in fork_params or (clientAgent in ['nzbget', 'sabnzbd'] and nzbExtractionBy != "Destination"):
            if inputName:
                process_all_exceptions(inputName, dirName)
                inputName, dirName = convert_to_ascii(inputName, dirName)

            # Now check if tv files exist in destination. 
            if not listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False):
                if listMediaFiles(dirName, media=False, audio=False, meta=False, archives=True) and extract:
                    logger.debug('Checking for archives to extract in directory: {0}'.format(dirName))
                    core.extractFiles(dirName)
                    inputName, dirName = convert_to_ascii(inputName, dirName)

            if listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False):  # Check that a video exists. if not, assume failed.
                flatten(dirName)

        # Check video files for corruption
        good_files = 0
        num_files = 0
        for video in listMediaFiles(dirName, media=True, audio=False, meta=False, archives=False):
            num_files += 1
            if transcoder.isVideoGood(video, status):
                good_files += 1
                import_subs(video)
        if num_files > 0:
            if good_files == num_files and not status == 0:
                logger.info('Found Valid Videos. Setting status Success')
                status = 0
                failed = 0
            if good_files < num_files and status == 0:
                logger.info('Found corrupt videos. Setting status Failed')
                status = 1
                failed = 1
                if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
                    print('[NZB] MARK=BAD')
                if failureLink:
                    failureLink += '&corrupt=true'
        elif clientAgent == "manual":
            logger.warning("No media files found in directory {0} to manually process.".format(dirName), section)
            return [0, ""]  # Success (as far as this script is concerned)
        elif nzbExtractionBy == "Destination":
            logger.info("Check for media files ignored because nzbExtractionBy is set to Destination.")
            if int(failed) == 0:
                logger.info("Setting Status Success.")
                status = 0
                failed = 0
            else:
                logger.info("Downloader reported an error during download or verification. Processing this as a failed download.")
                status = 1
                failed = 1
        else:
            logger.warning("No media files found in directory {0}. Processing this as a failed download".format(dirName), section)
            status = 1
            failed = 1
            if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][0:5] >= '14.0':
                print('[NZB] MARK=BAD')

        if status == 0 and core.TRANSCODE == 1:  # only transcode successful downloads
            result, newDirName = transcoder.Transcode_directory(dirName)
            if result == 0:
                logger.debug("SUCCESS: Transcoding succeeded for files in {0}".format(dirName), section)
                dirName = newDirName

                chmod_directory = int(str(cfg.get("chmodDirectory", "0")), 8)
                logger.debug("Config setting 'chmodDirectory' currently set to {0}".format(oct(chmod_directory)), section)
                if chmod_directory:
                    logger.info("Attempting to set the octal permission of '{0}' on directory '{1}'".format(oct(chmod_directory), dirName), section)
                    core.rchmod(dirName, chmod_directory)
            else:
                logger.error("FAILED: Transcoding failed for files in {0}".format(dirName), section)
                return [1, "{0}: Failed to post-process - Transcoding failed".format(section)]

        # configure SB params to pass
        fork_params['quiet'] = 1
        fork_params['proc_type'] = 'manual'
        if inputName is not None:
            fork_params['nzbName'] = inputName

        for param in copy.copy(fork_params):
            if param == "failed":
                fork_params[param] = failed
                del fork_params['proc_type']
                if "type" in fork_params:
                    del fork_params['type']

            if param == "return_data":
                fork_params[param] = 0
                del fork_params['quiet']

            if param == "type":
                fork_params[param] = 'manual'
                if "proc_type" in fork_params:
                    del fork_params['proc_type']

            if param in ["dirName", "dir", "proc_dir", "process_directory", "path"]:
                fork_params[param] = dirName
                if remote_path:
                    fork_params[param] = remoteDir(dirName)

            if param == "process_method":
                if process_method:
                    fork_params[param] = process_method
                else:
                    del fork_params[param]

            if param in ["force", "force_replace"]:
                if force:
                    fork_params[param] = force
                else:
                    del fork_params[param]

            if param in ["delete_on", "delete"]:
                if delete_on:
                    fork_params[param] = delete_on
                else:
                    del fork_params[param]

            if param == "ignore_subs":
                if ignore_subs:
                    fork_params[param] = ignore_subs
                else:
                    del fork_params[param]

            if param == "force_next":
                fork_params[param] = 1

        # delete any unused params so we don't pass them to SB by mistake
        [fork_params.pop(k) for k, v in fork_params.items() if v is None]

        if status == 0:
            if section == "NzbDrone" and not apikey:
                logger.info('No Sonarr apikey entered. Processing completed.')
                return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]
            logger.postprocess("SUCCESS: The download succeeded, sending a post-process request", section)
        else:
            core.FAILED = True
            if failureLink:
                reportNzb(failureLink, clientAgent)
            if 'failed' in fork_params:
                logger.postprocess("FAILED: The download failed. Sending 'failed' process request to {0} branch".format(fork), section)
            elif section == "NzbDrone":
                logger.postprocess("FAILED: The download failed. Sending failed download to {0} for CDH processing".format(fork), section)
                return [1, "{0}: Download Failed. Sending back to {1}".format(section, section)]  # Return as failed to flag this in the downloader.
            else:
                logger.postprocess("FAILED: The download failed. {0} branch does not handle failed downloads. Nothing to process".format(fork), section)
                if delete_failed and os.path.isdir(dirName) and not os.path.dirname(dirName) == dirName:
                    logger.postprocess("Deleting failed files and folder {0}".format(dirName), section)
                    rmDir(dirName)
                return [1, "{0}: Failed to post-process. {1} does not support failed downloads".format(section, section)]  # Return as failed to flag this in the downloader.

        url = None
        if section == "SickBeard":
            if apikey:
                url = "{0}{1}:{2}{3}/api/{4}/?cmd=postprocess".format(protocol, host, port, web_root, apikey)
            else:
                url = "{0}{1}:{2}{3}/home/postprocess/processEpisode".format(protocol, host, port, web_root)
        elif section == "NzbDrone":
            url = "{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}
            # params = {'sortKey': 'series.title', 'page': 1, 'pageSize': 1, 'sortDir': 'asc'}
            if remote_path:
                logger.debug("remote_path: {0}".format(remoteDir(dirName)), section)
                data = {"name": "DownloadedEpisodesScan", "path": remoteDir(dirName), "downloadClientId": download_id, "importMode": importMode}
            else:
                logger.debug("path: {0}".format(dirName), section)
                data = {"name": "DownloadedEpisodesScan", "path": dirName, "downloadClientId": download_id, "importMode": importMode}
            if not download_id:
                data.pop("downloadClientId")
            data = json.dumps(data)

        try:
            if section == "SickBeard":
                logger.debug("Opening URL: {0} with params: {1}".format(url, fork_params), section)
                s = requests.Session()
                if not apikey and username and password:
                    login = "******".format(protocol, host, port, web_root)
                    login_params = {'username': username, 'password': password}
                    r = s.get(login, verify=False, timeout=(30,60))
                    if r.status_code == 401 and r.cookies.get('_xsrf'):
                        login_params['_xsrf'] = r.cookies.get('_xsrf')
                    s.post(login, data=login_params, stream=True, verify=False, timeout=(30, 60))
                r = s.get(url, auth=(username, password), params=fork_params, stream=True, verify=False, timeout=(30, 1800))
            elif section == "NzbDrone":
                logger.debug("Opening URL: {0} with data: {1}".format(url, data), section)
                r = requests.post(url, data=data, headers=headers, stream=True, verify=False, timeout=(30, 1800))
        except requests.ConnectionError:
            logger.error("Unable to open URL: {0}".format(url), section)
            return [1, "{0}: Failed to post-process - Unable to connect to {1}".format(section, section)]

        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 [1, "{0}: Failed to post-process - Server returned status {1}".format(section, r.status_code)]

        Success = False
        Queued = False
        Started = False
        if section == "SickBeard":
            if apikey:
                if r.json()['result'] == 'success':
                    Success = True
            else:
                for line in r.iter_lines():
                    if line:
                        logger.postprocess("{0}".format(line), section)
                        if "Moving file from" in line:
                            inputName = os.path.split(line)[1]
                        if "added to the queue" in line:
                            Queued = True
                        if "Processing succeeded" in line or "Successfully processed" in line:
                            Success = True

            if Queued:
                time.sleep(60)
        elif section == "NzbDrone":
            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
                Started = False

        if status != 0 and delete_failed and not os.path.dirname(dirName) == dirName:
            logger.postprocess("Deleting failed files and folder {0}".format(dirName), section)
            rmDir(dirName)

        if Success:
            return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]
        elif section == "NzbDrone" and Started:
            n = 0
            params = {}
            url = "{0}/{1}".format(url, scan_id)
            while n < 6:  # set up wait_for minutes to see if command completes..
                time.sleep(10 * wait_for)
                command_status = self.command_complete(url, params, headers, section)
                if command_status and command_status in ['completed', 'failed']:
                    break
                n += 1
            if command_status:
                logger.debug("The Scan command return status: {0}".format(command_status), section)
            if not os.path.exists(dirName):
                logger.debug("The directory {0} has been removed. Renaming was successful.".format(dirName), section)
                return [0, "{0}: Successfully post-processed {1}".format(section, inputName)]
            elif command_status and 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 and 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 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)]
            else:
                logger.warning("The Scan command did not return a valid status. Renaming was not successful.", section)
                return [1, "{0}: Failed to post-process {1}".format(section, inputName)]
        else:
            return [1, "{0}: Failed to post-process - Returned log from {1} was not as expected.".format(section, section)]  # We did not receive Success confirmation.
Esempio n. 15
0
    def process(self,
                section,
                dirName,
                inputName=None,
                status=0,
                clientAgent="manual",
                inputCategory=None):
        status = int(status)

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        apikey = cfg["apikey"]
        wait_for = int(cfg["wait_for"])
        ssl = int(cfg.get("ssl", 0))
        web_root = cfg.get("web_root", "")
        remote_path = int(cfg.get("remote_path", 0))
        protocol = "https://" if ssl else "http://"
        status = int(status)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))

        url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root)
        if not server_responding(url):
            logger.error("Server did not respond. Exiting", section)
            return [
                1, "{0}: Failed to post-process - {1} did not respond.".format(
                    section, section)
            ]

        if not os.path.isdir(dirName) and os.path.isfile(
                dirName
        ):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(
                dirName, media=False, audio=True, meta=False,
                archives=False) and listMediaFiles(dirName,
                                                   media=False,
                                                   audio=False,
                                                   meta=False,
                                                   archives=True) and extract:
            logger.debug(
                'Checking for archives to extract in directory: {0}'.format(
                    dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        if listMediaFiles(
                dirName, media=False, audio=True, meta=False,
                archives=False) and status:
            logger.info(
                "Status shown as failed from Downloader, but valid video files found. Setting as successful.",
                section)
            status = 0

        if status == 0:

            params = {
                'apikey': apikey,
                'cmd': "forceProcess",
                'dir': remoteDir(dirName) if remote_path else dirName
            }

            release_status = self.get_status(url, apikey, dirName)
            if not release_status:
                logger.error(
                    "Could not find a status for {0}, is it in the wanted list ?"
                    .format(inputName), section)

            logger.debug(
                "Opening URL: {0} with PARAMS: {1}".format(url, params),
                section)

            try:
                r = requests.get(url,
                                 params=params,
                                 verify=False,
                                 timeout=(30, 300))
            except requests.ConnectionError:
                logger.error("Unable to open URL {0}".format(url), section)
                return [
                    1,
                    "{0}: Failed to post-process - Unable to connect to {1}".
                    format(section, section)
                ]

            logger.debug("Result: {0}".format(r.text), section)

            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 [
                    1,
                    "{0}: Failed to post-process - Server returned status {1}".
                    format(section, r.status_code)
                ]
            elif r.text == "OK":
                logger.postprocess(
                    "SUCCESS: Post-Processing started for {0} in folder {1} ..."
                    .format(inputName, dirName), section)
            else:
                logger.error(
                    "FAILED: Post-Processing has NOT started for {0} in folder {1}. exiting!"
                    .format(inputName, dirName), section)
                return [
                    1,
                    "{0}: Failed to post-process - Returned log from {1} was not as expected."
                    .format(section, section)
                ]

        else:
            logger.warning("FAILED DOWNLOAD DETECTED", section)
            return [
                1,
                "{0}: Failed to post-process. {1} does not support failed downloads"
                .format(section, section)
            ]

        # we will now wait for this album to be processed before returning to TorrentToMedia and unpausing.
        timeout = time.time() + 60 * wait_for
        while time.time() < timeout:
            current_status = self.get_status(url, apikey, dirName)
            if current_status is not None and current_status != release_status:  # Something has changed. CPS must have processed this movie.
                logger.postprocess(
                    "SUCCESS: This release is now marked as status [{0}]".
                    format(current_status), section)
                return [
                    0, "{0}: Successfully post-processed {1}".format(
                        section, inputName)
                ]
            if not os.path.isdir(dirName):
                logger.postprocess(
                    "SUCCESS: The input directory {0} has been removed Processing must have finished."
                    .format(dirName), section)
                return [
                    0, "{0}: Successfully post-processed {1}".format(
                        section, inputName)
                ]
            time.sleep(10 * wait_for)

        # The status hasn't changed. uTorrent can resume seeding now.
        logger.warning(
            "The music album does not appear to have changed status after {0} minutes. Please check your Logs"
            .format(wait_for), section)
        return [
            1,
            "{0}: Failed to post-process - No change in wanted status".format(
                section)
        ]
Esempio n. 16
0
    def processEpisode(self,
                       section,
                       dirName,
                       inputName=None,
                       failed=False,
                       clientAgent="manual",
                       download_id=None,
                       inputCategory=None,
                       failureLink=None):

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        ssl = int(cfg.get("ssl", 0))
        web_root = cfg.get("web_root", "")
        protocol = "https://" if ssl else "http://"
        username = cfg.get("username", "")
        password = cfg.get("password", "")
        apikey = cfg.get("apikey", "")

        if server_responding("{0}{1}:{2}{3}".format(protocol, host, port,
                                                    web_root)):
            # auto-detect correct fork
            fork, fork_params = autoFork(section, inputCategory)
        elif not username and not apikey:
            logger.info(
                'No SickBeard username or Sonarr apikey entered. Performing transcoder functions only'
            )
            fork, fork_params = "None", {}
        else:
            logger.error("Server did not respond. Exiting", section)
            return [
                1, "{0}: Failed to post-process - {1} did not respond.".format(
                    section, section)
            ]

        delete_failed = int(cfg.get("delete_failed", 0))
        nzbExtractionBy = cfg.get("nzbExtractionBy", "Downloader")
        process_method = cfg.get("process_method")
        if clientAgent == core.TORRENT_CLIENTAGENT and core.USELINK == "move-sym":
            process_method = "symlink"
        remote_path = int(cfg.get("remote_path", 0))
        wait_for = int(cfg.get("wait_for", 2))
        force = int(cfg.get("force", 0))
        delete_on = int(cfg.get("delete_on", 0))
        ignore_subs = int(cfg.get("ignore_subs", 0))
        status = int(failed)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))
        #get importmode, default to "Move" for consistency with legacy
        importMode = cfg.get("importMode", "Move")

        if not os.path.isdir(dirName) and os.path.isfile(
                dirName
        ):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        # Attempt to create the directory if it doesn't exist and ignore any
        # error stating that it already exists. This fixes a bug where SickRage
        # won't process the directory because it doesn't exist.
        try:
            os.makedirs(dirName)  # Attempt to create the directory
        except OSError as e:
            # Re-raise the error if it wasn't about the directory not existing
            if e.errno != errno.EEXIST:
                raise

        if 'process_method' not in fork_params or (
                clientAgent in ['nzbget', 'sabnzbd']
                and nzbExtractionBy != "Destination"):
            if inputName:
                process_all_exceptions(inputName, dirName)
                inputName, dirName = convert_to_ascii(inputName, dirName)

            # Now check if tv files exist in destination.
            if not listMediaFiles(
                    dirName, media=True, audio=False, meta=False,
                    archives=False):
                if listMediaFiles(dirName,
                                  media=False,
                                  audio=False,
                                  meta=False,
                                  archives=True) and extract:
                    logger.debug(
                        'Checking for archives to extract in directory: {0}'.
                        format(dirName))
                    core.extractFiles(dirName)
                    inputName, dirName = convert_to_ascii(inputName, dirName)

            if listMediaFiles(
                    dirName, media=True, audio=False, meta=False,
                    archives=False
            ):  # Check that a video exists. if not, assume failed.
                flatten(dirName)

        # Check video files for corruption
        good_files = 0
        num_files = 0
        for video in listMediaFiles(dirName,
                                    media=True,
                                    audio=False,
                                    meta=False,
                                    archives=False):
            num_files += 1
            if transcoder.isVideoGood(video, status):
                good_files += 1
                import_subs(video)
        if num_files > 0:
            if good_files == num_files and not status == 0:
                logger.info('Found Valid Videos. Setting status Success')
                status = 0
                failed = 0
            if good_files < num_files and status == 0:
                logger.info('Found corrupt videos. Setting status Failed')
                status = 1
                failed = 1
                if 'NZBOP_VERSION' in os.environ and os.environ[
                        'NZBOP_VERSION'][0:5] >= '14.0':
                    print('[NZB] MARK=BAD')
                if failureLink:
                    failureLink += '&corrupt=true'
        elif clientAgent == "manual":
            logger.warning(
                "No media files found in directory {0} to manually process.".
                format(dirName), section)
            return [0, ""]  # Success (as far as this script is concerned)
        elif nzbExtractionBy == "Destination":
            logger.info(
                "Check for media files ignored because nzbExtractionBy is set to Destination."
            )
            if int(failed) == 0:
                logger.info("Setting Status Success.")
                status = 0
                failed = 0
            else:
                logger.info(
                    "Downloader reported an error during download or verification. Processing this as a failed download."
                )
                status = 1
                failed = 1
        else:
            logger.warning(
                "No media files found in directory {0}. Processing this as a failed download"
                .format(dirName), section)
            status = 1
            failed = 1
            if 'NZBOP_VERSION' in os.environ and os.environ['NZBOP_VERSION'][
                    0:5] >= '14.0':
                print('[NZB] MARK=BAD')

        if status == 0 and core.TRANSCODE == 1:  # only transcode successful downloads
            result, newDirName = transcoder.Transcode_directory(dirName)
            if result == 0:
                logger.debug(
                    "SUCCESS: Transcoding succeeded for files in {0}".format(
                        dirName), section)
                dirName = newDirName

                chmod_directory = int(str(cfg.get("chmodDirectory", "0")), 8)
                logger.debug(
                    "Config setting 'chmodDirectory' currently set to {0}".
                    format(oct(chmod_directory)), section)
                if chmod_directory:
                    logger.info(
                        "Attempting to set the octal permission of '{0}' on directory '{1}'"
                        .format(oct(chmod_directory), dirName), section)
                    core.rchmod(dirName, chmod_directory)
            else:
                logger.error(
                    "FAILED: Transcoding failed for files in {0}".format(
                        dirName), section)
                return [
                    1,
                    "{0}: Failed to post-process - Transcoding failed".format(
                        section)
                ]

        # configure SB params to pass
        fork_params['quiet'] = 1
        fork_params['proc_type'] = 'manual'
        if inputName is not None:
            fork_params['nzbName'] = inputName

        for param in copy.copy(fork_params):
            if param == "failed":
                fork_params[param] = failed
                del fork_params['proc_type']
                if "type" in fork_params:
                    del fork_params['type']

            if param == "return_data":
                fork_params[param] = 0
                del fork_params['quiet']

            if param == "type":
                fork_params[param] = 'manual'
                if "proc_type" in fork_params:
                    del fork_params['proc_type']

            if param in [
                    "dirName", "dir", "proc_dir", "process_directory", "path"
            ]:
                fork_params[param] = dirName
                if remote_path:
                    fork_params[param] = remoteDir(dirName)

            if param == "process_method":
                if process_method:
                    fork_params[param] = process_method
                else:
                    del fork_params[param]

            if param in ["force", "force_replace"]:
                if force:
                    fork_params[param] = force
                else:
                    del fork_params[param]

            if param in ["delete_on", "delete"]:
                if delete_on:
                    fork_params[param] = delete_on
                else:
                    del fork_params[param]

            if param == "ignore_subs":
                if ignore_subs:
                    fork_params[param] = ignore_subs
                else:
                    del fork_params[param]

            if param == "force_next":
                fork_params[param] = 1

        # delete any unused params so we don't pass them to SB by mistake
        [fork_params.pop(k) for k, v in fork_params.items() if v is None]

        if status == 0:
            if section == "NzbDrone" and not apikey:
                logger.info('No Sonarr apikey entered. Processing completed.')
                return [
                    0, "{0}: Successfully post-processed {1}".format(
                        section, inputName)
                ]
            logger.postprocess(
                "SUCCESS: The download succeeded, sending a post-process request",
                section)
        else:
            core.FAILED = True
            if failureLink:
                reportNzb(failureLink, clientAgent)
            if 'failed' in fork_params:
                logger.postprocess(
                    "FAILED: The download failed. Sending 'failed' process request to {0} branch"
                    .format(fork), section)
            elif section == "NzbDrone":
                logger.postprocess(
                    "FAILED: The download failed. Sending failed download to {0} for CDH processing"
                    .format(fork), section)
                return [
                    1, "{0}: Download Failed. Sending back to {1}".format(
                        section, section)
                ]  # Return as failed to flag this in the downloader.
            else:
                logger.postprocess(
                    "FAILED: The download failed. {0} branch does not handle failed downloads. Nothing to process"
                    .format(fork), section)
                if delete_failed and os.path.isdir(
                        dirName) and not os.path.dirname(dirName) == dirName:
                    logger.postprocess(
                        "Deleting failed files and folder {0}".format(dirName),
                        section)
                    rmDir(dirName)
                return [
                    1,
                    "{0}: Failed to post-process. {1} does not support failed downloads"
                    .format(section, section)
                ]  # Return as failed to flag this in the downloader.

        url = None
        if section == "SickBeard":
            if apikey:
                url = "{0}{1}:{2}{3}/api/{4}/?cmd=postprocess".format(
                    protocol, host, port, web_root, apikey)
            else:
                url = "{0}{1}:{2}{3}/home/postprocess/processEpisode".format(
                    protocol, host, port, web_root)
        elif section == "NzbDrone":
            url = "{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}
            # params = {'sortKey': 'series.title', 'page': 1, 'pageSize': 1, 'sortDir': 'asc'}
            if remote_path:
                logger.debug("remote_path: {0}".format(remoteDir(dirName)),
                             section)
                data = {
                    "name": "DownloadedEpisodesScan",
                    "path": remoteDir(dirName),
                    "downloadClientId": download_id,
                    "importMode": importMode
                }
            else:
                logger.debug("path: {0}".format(dirName), section)
                data = {
                    "name": "DownloadedEpisodesScan",
                    "path": dirName,
                    "downloadClientId": download_id,
                    "importMode": importMode
                }
            if not download_id:
                data.pop("downloadClientId")
            data = json.dumps(data)

        try:
            if section == "SickBeard":
                logger.debug(
                    "Opening URL: {0} with params: {1}".format(
                        url, fork_params), section)
                s = requests.Session()
                if not apikey and username and password:
                    login = "******".format(
                        protocol, host, port, web_root)
                    login_params = {'username': username, 'password': password}
                    r = s.get(login, verify=False, timeout=(30, 60))
                    if r.status_code == 401 and r.cookies.get('_xsrf'):
                        login_params['_xsrf'] = r.cookies.get('_xsrf')
                    s.post(login,
                           data=login_params,
                           stream=True,
                           verify=False,
                           timeout=(30, 60))
                r = s.get(url,
                          auth=(username, password),
                          params=fork_params,
                          stream=True,
                          verify=False,
                          timeout=(30, 1800))
            elif section == "NzbDrone":
                logger.debug(
                    "Opening URL: {0} with data: {1}".format(url, data),
                    section)
                r = requests.post(url,
                                  data=data,
                                  headers=headers,
                                  stream=True,
                                  verify=False,
                                  timeout=(30, 1800))
        except requests.ConnectionError:
            logger.error("Unable to open URL: {0}".format(url), section)
            return [
                1, "{0}: Failed to post-process - Unable to connect to {1}".
                format(section, section)
            ]

        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 [
                1, "{0}: Failed to post-process - Server returned status {1}".
                format(section, r.status_code)
            ]

        Success = False
        Queued = False
        Started = False
        if section == "SickBeard":
            if apikey:
                if r.json()['result'] == 'success':
                    Success = True
            else:
                for line in r.iter_lines():
                    if line:
                        logger.postprocess("{0}".format(line), section)
                        if "Moving file from" in line:
                            inputName = os.path.split(line)[1]
                        if "added to the queue" in line:
                            Queued = True
                        if "Processing succeeded" in line or "Successfully processed" in line:
                            Success = True

            if Queued:
                time.sleep(60)
        elif section == "NzbDrone":
            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
                Started = False

        if status != 0 and delete_failed and not os.path.dirname(
                dirName) == dirName:
            logger.postprocess(
                "Deleting failed files and folder {0}".format(dirName),
                section)
            rmDir(dirName)

        if Success:
            return [
                0, "{0}: Successfully post-processed {1}".format(
                    section, inputName)
            ]
        elif section == "NzbDrone" and Started:
            n = 0
            params = {}
            url = "{0}/{1}".format(url, scan_id)
            while n < 6:  # set up wait_for minutes to see if command completes..
                time.sleep(10 * wait_for)
                command_status = self.command_complete(url, params, headers,
                                                       section)
                if command_status and command_status in [
                        'completed', 'failed'
                ]:
                    break
                n += 1
            if command_status:
                logger.debug(
                    "The Scan command return status: {0}".format(
                        command_status), section)
            if not os.path.exists(dirName):
                logger.debug(
                    "The directory {0} has been removed. Renaming was successful."
                    .format(dirName), section)
                return [
                    0, "{0}: Successfully post-processed {1}".format(
                        section, inputName)
                ]
            elif command_status and 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 and 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 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)
                ]
            else:
                logger.warning(
                    "The Scan command did not return a valid status. Renaming was not successful.",
                    section)
                return [
                    1, "{0}: Failed to post-process {1}".format(
                        section, inputName)
                ]
        else:
            return [
                1,
                "{0}: Failed to post-process - Returned log from {1} was not as expected."
                .format(section, section)
            ]  # We did not receive Success confirmation.
Esempio n. 17
0
    def process(self, section, dirName, inputName=None, status=0, clientAgent="manual", inputCategory=None):
        status = int(status)

        cfg = dict(core.CFG[section][inputCategory])

        host = cfg["host"]
        port = cfg["port"]
        apikey = cfg["apikey"]
        wait_for = int(cfg["wait_for"])
        ssl = int(cfg.get("ssl", 0))
        web_root = cfg.get("web_root", "")
        remote_path = int(cfg.get("remote_path", 0))
        protocol = "https://" if ssl else "http://"
        status = int(status)
        if status > 0 and core.NOEXTRACTFAILED:
            extract = 0
        else:
            extract = int(cfg.get("extract", 0))

        url = "{0}{1}:{2}{3}/api".format(protocol, host, port, web_root)
        if not server_responding(url):
            logger.error("Server did not respond. Exiting", section)
            return [1, "{0}: Failed to post-process - {1} did not respond.".format(section, section)]

        if not os.path.isdir(dirName) and os.path.isfile(dirName):  # If the input directory is a file, assume single file download and split dir/name.
            dirName = os.path.split(os.path.normpath(dirName))[0]

        SpecificPath = os.path.join(dirName, str(inputName))
        cleanName = os.path.splitext(SpecificPath)
        if cleanName[1] == ".nzb":
            SpecificPath = cleanName[0]
        if os.path.isdir(SpecificPath):
            dirName = SpecificPath

        process_all_exceptions(inputName, dirName)
        inputName, dirName = convert_to_ascii(inputName, dirName)

        if not listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and listMediaFiles(dirName, media=False, audio=False, meta=False, archives=True) and extract:
            logger.debug('Checking for archives to extract in directory: {0}'.format(dirName))
            core.extractFiles(dirName)
            inputName, dirName = convert_to_ascii(inputName, dirName)

        if listMediaFiles(dirName, media=False, audio=True, meta=False, archives=False) and status:
            logger.info("Status shown as failed from Downloader, but valid video files found. Setting as successful.", section)
            status = 0

        if status == 0:

            params = {
                'apikey': apikey,
                'cmd': "forceProcess",
                'dir': remoteDir(dirName) if remote_path else dirName
            }

            res = self.forceProcess(params, url, apikey, inputName, dirName, section, wait_for)
            if res[0] in [0, 1]:
                return res

            params = {
                'apikey': apikey,
                'cmd': "forceProcess",
                'dir': os.path.split(remoteDir(dirName))[0] if remote_path else os.path.split(dirName)[0]
            }

            res = self.forceProcess(params, url, apikey, inputName, dirName, section, wait_for)
            if res[0] in [0, 1]:
                return res

            # The status hasn't changed. uTorrent can resume seeding now.
            logger.warning("The music album does not appear to have changed status after {0} minutes. Please check your Logs".format(wait_for), section)
            return [1, "{0}: Failed to post-process - No change in wanted status".format(section)]

        else:
            logger.warning("FAILED DOWNLOAD DETECTED", section)
            return [1, "{0}: Failed to post-process. {1} does not support failed downloads".format(section, section)]