Beispiel #1
0
def validateDir(path, dirName, nzbNameOriginal, failed, result):
    """
    Check if directory is valid for processing

    :param path: Path to use
    :param dirName: Directory to check
    :param nzbNameOriginal: Original NZB name
    :param failed: Previously failed objects
    :param result: Previous results
    :return: True if dir is valid for processing, False if not
    """

    IGNORED_FOLDERS = [".AppleDouble", ".@__thumb", "@eaDir"]

    folder_name = os.path.basename(dirName)
    if folder_name in IGNORED_FOLDERS:
        return False

    result.output += logHelper("Processing folder " + dirName, sickrage.srLogger.DEBUG)

    if folder_name.startswith("_FAILED_"):
        result.output += logHelper("The directory name indicates it failed to extract.", sickrage.srLogger.DEBUG)
        failed = True
    elif folder_name.startswith("_UNDERSIZED_"):
        result.output += logHelper(
            "The directory name indicates that it was previously rejected for being undersized.",
            sickrage.srLogger.DEBUG,
        )
        failed = True
    elif folder_name.upper().startswith("_UNPACK"):
        result.output += logHelper(
            "The directory name indicates that this release is in the process of being unpacked.",
            sickrage.srLogger.DEBUG,
        )
        result.missedfiles.append(dirName + " : Being unpacked")
        return False

    if failed:
        process_failed(os.path.join(path, dirName), nzbNameOriginal, result)
        result.missedfiles.append(dirName + " : Failed download")
        return False

    if is_hidden_folder(os.path.join(path, dirName)):
        result.output += logHelper("Ignoring hidden folder: " + dirName, sickrage.srLogger.DEBUG)
        result.missedfiles.append(dirName + " : Hidden folder")
        return False

    # make sure the dir isn't inside a show dir
    for sqlShow in main_db.MainDB().select("SELECT * FROM tv_shows"):
        if (
            dirName.lower().startswith(os.path.realpath(sqlShow[b"location"]).lower() + os.sep)
            or dirName.lower() == os.path.realpath(sqlShow[b"location"]).lower()
        ):
            result.output += logHelper(
                "Cannot process an episode that's already been moved to its show dir, skipping " + dirName,
                sickrage.srLogger.WARNING,
            )
            return False

    # Get the videofile list for the next checks
    allFiles = []
    allDirs = []
    for _, processdir, fileList in os.walk(os.path.join(path, dirName), topdown=False):
        allDirs += processdir
        allFiles += fileList

    videoFiles = [x for x in allFiles if isMediaFile(x)]
    allDirs.append(dirName)

    # check if the dir have at least one tv video file
    for video in videoFiles:
        try:
            NameParser().parse(video, cache_result=False)
            return True
        except (InvalidNameException, InvalidShowException):
            pass

    for proc_dir in allDirs:
        try:
            NameParser().parse(proc_dir, cache_result=False)
            return True
        except (InvalidNameException, InvalidShowException):
            pass

    if sickrage.srConfig.UNPACK:
        # Search for packed release
        packedFiles = [x for x in allFiles if isRarFile(x)]

        for packed in packedFiles:
            try:
                NameParser().parse(packed, cache_result=False)
                return True
            except (InvalidNameException, InvalidShowException):
                pass

    result.output += logHelper(dirName + " : No processable items found in folder", sickrage.srLogger.DEBUG)
    return False
Beispiel #2
0
def validateDir(path, dirName, nzbNameOriginal, failed, result):
    """
    Check if directory is valid for processing

    :param path: Path to use
    :param dirName: Directory to check
    :param nzbNameOriginal: Original NZB name
    :param failed: Previously failed objects
    :param result: Previous results
    :return: True if dir is valid for processing, False if not
    """

    IGNORED_FOLDERS = ['.AppleDouble', '.@__thumb', '@eaDir']

    folder_name = os.path.basename(dirName)
    if folder_name in IGNORED_FOLDERS:
        return False

    result.output += logHelper("Processing folder " + dirName,
                               sickrage.srLogger.DEBUG)

    if folder_name.startswith('_FAILED_'):
        result.output += logHelper(
            "The directory name indicates it failed to extract.",
            sickrage.srLogger.DEBUG)
        failed = True
    elif folder_name.startswith('_UNDERSIZED_'):
        result.output += logHelper(
            "The directory name indicates that it was previously rejected for being undersized.",
            sickrage.srLogger.DEBUG)
        failed = True
    elif folder_name.upper().startswith('_UNPACK'):
        result.output += logHelper(
            "The directory name indicates that this release is in the process of being unpacked.",
            sickrage.srLogger.DEBUG)
        result.missedfiles.append(dirName + " : Being unpacked")
        return False

    if failed:
        process_failed(os.path.join(path, dirName), nzbNameOriginal, result)
        result.missedfiles.append(dirName + " : Failed download")
        return False

    if is_hidden_folder(os.path.join(path, dirName)):
        result.output += logHelper("Ignoring hidden folder: " + dirName,
                                   sickrage.srLogger.DEBUG)
        result.missedfiles.append(dirName + " : Hidden folder")
        return False

    # make sure the dir isn't inside a show dir
    for sqlShow in main_db.MainDB().select("SELECT * FROM tv_shows"):
        if dirName.lower().startswith(os.path.realpath(sqlShow[b"location"]).lower() + os.sep) or \
                        dirName.lower() == os.path.realpath(sqlShow[b"location"]).lower():
            result.output += logHelper(
                "Cannot process an episode that's already been moved to its show dir, skipping "
                + dirName, sickrage.srLogger.WARNING)
            return False

    # Get the videofile list for the next checks
    allFiles = []
    allDirs = []
    for _, processdir, fileList in os.walk(os.path.join(path, dirName),
                                           topdown=False):
        allDirs += processdir
        allFiles += fileList

    videoFiles = [x for x in allFiles if isMediaFile(x)]
    allDirs.append(dirName)

    # check if the dir have at least one tv video file
    for video in videoFiles:
        try:
            NameParser().parse(video, cache_result=False)
            return True
        except (InvalidNameException, InvalidShowException):
            pass

    for proc_dir in allDirs:
        try:
            NameParser().parse(proc_dir, cache_result=False)
            return True
        except (InvalidNameException, InvalidShowException):
            pass

    if sickrage.srConfig.UNPACK:
        # Search for packed release
        packedFiles = [x for x in allFiles if isRarFile(x)]

        for packed in packedFiles:
            try:
                NameParser().parse(packed, cache_result=False)
                return True
            except (InvalidNameException, InvalidShowException):
                pass

    result.output += logHelper(
        dirName + " : No processable items found in folder",
        sickrage.srLogger.DEBUG)
    return False
Beispiel #3
0
def processDir(
    dirName,
    nzbName=None,
    process_method=None,
    force=False,
    is_priority=None,
    delete_on=False,
    failed=False,
    proc_type="auto",
    **kwargs
):
    """
    Scans through the files in dirName and processes whatever media files it finds

    :param dirName: The folder name to look in
    :param nzbName: The NZB name which resulted in this folder being downloaded
    :param force: True to postprocess already postprocessed files
    :param failed: Boolean for whether or not the download failed
    :param proc_type: Type of postprocessing auto or manual
    """

    result = ProcessResult()

    result.output += logHelper("Processing folder %s" % dirName, sickrage.srLogger.DEBUG)

    result.output += logHelper("TV_DOWNLOAD_DIR: %s" % sickrage.srConfig.TV_DOWNLOAD_DIR, sickrage.srLogger.DEBUG)
    postpone = False

    # if they passed us a real dir then assume it's the one we want
    if os.path.isdir(dirName):
        dirName = os.path.realpath(dirName)

    # if the client and SickRage are not on the same machine translate the Dir in a network dir
    elif (
        sickrage.srConfig.TV_DOWNLOAD_DIR
        and os.path.isdir(sickrage.srConfig.TV_DOWNLOAD_DIR)
        and os.path.normpath(dirName) != os.path.normpath(sickrage.srConfig.TV_DOWNLOAD_DIR)
    ):
        dirName = os.path.join(sickrage.srConfig.TV_DOWNLOAD_DIR, os.path.abspath(dirName).split(os.path.sep)[-1])
        result.output += logHelper("Trying to use folder %s" % dirName, sickrage.srLogger.DEBUG)

    # if we didn't find a real dir then quit
    if not os.path.isdir(dirName):
        result.output += logHelper(
            "Unable to figure out what folder to process. If your downloader and SiCKRAGE aren't on the same PC make sure you fill out your TV download dir in the config.",
            sickrage.srLogger.DEBUG,
        )
        return result.output

    path, dirs, files = get_path_dir_files(dirName, nzbName, proc_type)

    files = [x for x in files if notTorNZBFile(x)]
    SyncFiles = [x for x in files if isSyncFile(x)]

    # Don't post process if files are still being synced and option is activated
    if SyncFiles and sickrage.srConfig.POSTPONE_IF_SYNC_FILES:
        postpone = True

    nzbNameOriginal = nzbName

    if not postpone:
        result.output += logHelper("PostProcessing Path: %s" % path, sickrage.srLogger.INFO)
        result.output += logHelper("PostProcessing Dirs: [%s]" % dirs, sickrage.srLogger.DEBUG)

        rarFiles = [x for x in files if isRarFile(x)]
        rarContent = unRAR(path, rarFiles, force, result)
        files += rarContent
        videoFiles = [x for x in files if isMediaFile(x)]
        videoInRar = [x for x in rarContent if isMediaFile(x)]

        result.output += logHelper("PostProcessing Files: [%s]" % ", ".join(files), sickrage.srLogger.DEBUG)
        result.output += logHelper("PostProcessing VideoFiles: [%s]" % ", ".join(videoFiles), sickrage.srLogger.DEBUG)
        result.output += logHelper("PostProcessing RarContent: [%s]" % ", ".join(rarContent), sickrage.srLogger.DEBUG)
        result.output += logHelper("PostProcessing VideoInRar: [%s]" % ", ".join(videoInRar), sickrage.srLogger.DEBUG)

        # If nzbName is set and there's more than one videofile in the folder, files will be lost (overwritten).
        if len(videoFiles) >= 2:
            nzbName = None

        if not process_method:
            process_method = sickrage.srConfig.PROCESS_METHOD

        result.result = True

        # Don't Link media when the media is extracted from a rar in the same path
        if process_method in ("hardlink", "symlink") and videoInRar:
            process_media(path, videoInRar, nzbName, "move", force, is_priority, result)
            delete_files(path, rarContent, result)
            for video in set(videoFiles) - set(videoInRar):
                process_media(path, [video], nzbName, process_method, force, is_priority, result)
        elif sickrage.srConfig.DELRARCONTENTS and videoInRar:
            process_media(path, videoInRar, nzbName, process_method, force, is_priority, result)
            delete_files(path, rarContent, result, True)
            for video in set(videoFiles) - set(videoInRar):
                process_media(path, [video], nzbName, process_method, force, is_priority, result)
        else:
            for video in videoFiles:
                process_media(path, [video], nzbName, process_method, force, is_priority, result)

    else:
        result.output += logHelper("Found temporary sync files, skipping post processing for: %s" % path)
        result.output += logHelper("Sync Files: [%s] in path %s" % (", ".join(SyncFiles), path))
        result.missedfiles.append("%s : Syncfiles found" % path)

    # Process Video File in all TV Subdir
    for curDir in [x for x in dirs if validateDir(path, x, nzbNameOriginal, failed, result)]:

        result.result = True

        for processPath, _, fileList in os.walk(os.path.join(path, curDir), topdown=False):

            if not validateDir(path, processPath, nzbNameOriginal, failed, result):
                continue

            postpone = False

            SyncFiles = [x for x in fileList if isSyncFile(x)]

            # Don't post process if files are still being synced and option is activated
            if SyncFiles and sickrage.srConfig.POSTPONE_IF_SYNC_FILES:
                postpone = True

            if not postpone:
                rarFiles = [x for x in fileList if isRarFile(x)]
                rarContent = unRAR(processPath, rarFiles, force, result)
                fileList = set(fileList + rarContent)
                videoFiles = [x for x in fileList if isMediaFile(x)]
                videoInRar = [x for x in rarContent if isMediaFile(x)]
                notwantedFiles = [x for x in fileList if x not in videoFiles]
                if notwantedFiles:
                    result.output += logHelper(
                        "Found unwanted files: [%s]" % ", ".join(notwantedFiles), sickrage.srLogger.DEBUG
                    )

                # Don't Link media when the media is extracted from a rar in the same path
                if process_method in ("hardlink", "symlink") and videoInRar:
                    process_media(processPath, videoInRar, nzbName, "move", force, is_priority, result)
                    process_media(
                        processPath,
                        set(videoFiles) - set(videoInRar),
                        nzbName,
                        process_method,
                        force,
                        is_priority,
                        result,
                    )
                    delete_files(processPath, rarContent, result)
                elif sickrage.srConfig.DELRARCONTENTS and videoInRar:
                    process_media(processPath, videoInRar, nzbName, process_method, force, is_priority, result)
                    process_media(
                        processPath,
                        set(videoFiles) - set(videoInRar),
                        nzbName,
                        process_method,
                        force,
                        is_priority,
                        result,
                    )
                    delete_files(processPath, rarContent, result, True)
                else:
                    process_media(processPath, videoFiles, nzbName, process_method, force, is_priority, result)

                    # Delete all file not needed
                    if (
                        process_method != "move" or not result.result or (proc_type == "manual" and not delete_on)
                    ):  # Avoid to delete files if is Manual PostProcessing
                        continue

                    delete_files(processPath, notwantedFiles, result)

                    if (
                        (not sickrage.srConfig.NO_DELETE or proc_type == "manual")
                        and process_method == "move"
                        and os.path.normpath(processPath) != os.path.normpath(sickrage.srConfig.TV_DOWNLOAD_DIR)
                    ):
                        if delete_folder(processPath, check_empty=True):
                            result.output += logHelper("Deleted folder: %s" % processPath, sickrage.srLogger.DEBUG)
            else:
                result.output += logHelper("Found temporary sync files, skipping post processing for: %s" % processPath)
                result.output += logHelper("Sync Files: [%s] in path %s" % (", ".join(SyncFiles), processPath))
                result.missedfiles.append("%s : Syncfiles found" % processPath)

    if result.aggresult:
        result.output += logHelper("Processing completed")
        if result.missedfiles:
            result.output += logHelper("I did encounter some unprocessable items: [%s]" % ", ".join(result.missedfiles))
    else:
        result.output += logHelper(
            "Problem(s) during processing, failed the following files/folders:  [%s]" % ", ".join(result.missedfiles),
            sickrage.srLogger.WARNING,
        )

    return result.output
Beispiel #4
0
def processDir(dirName,
               nzbName=None,
               process_method=None,
               force=False,
               is_priority=None,
               delete_on=False,
               failed=False,
               proc_type="auto",
               **kwargs):
    """
    Scans through the files in dirName and processes whatever media files it finds

    :param dirName: The folder name to look in
    :param nzbName: The NZB name which resulted in this folder being downloaded
    :param force: True to postprocess already postprocessed files
    :param failed: Boolean for whether or not the download failed
    :param proc_type: Type of postprocessing auto or manual
    """

    result = ProcessResult()

    result.output += logHelper("Processing folder %s" % dirName,
                               sickrage.srLogger.DEBUG)

    result.output += logHelper(
        "TV_DOWNLOAD_DIR: %s" % sickrage.srConfig.TV_DOWNLOAD_DIR,
        sickrage.srLogger.DEBUG)
    postpone = False

    # if they passed us a real dir then assume it's the one we want
    if os.path.isdir(dirName):
        dirName = os.path.realpath(dirName)

    # if the client and SickRage are not on the same machine translate the Dir in a network dir
    elif sickrage.srConfig.TV_DOWNLOAD_DIR and os.path.isdir(sickrage.srConfig.TV_DOWNLOAD_DIR) \
            and os.path.normpath(dirName) != os.path.normpath(sickrage.srConfig.TV_DOWNLOAD_DIR):
        dirName = os.path.join(sickrage.srConfig.TV_DOWNLOAD_DIR,
                               os.path.abspath(dirName).split(os.path.sep)[-1])
        result.output += logHelper("Trying to use folder %s" % dirName,
                                   sickrage.srLogger.DEBUG)

    # if we didn't find a real dir then quit
    if not os.path.isdir(dirName):
        result.output += logHelper(
            "Unable to figure out what folder to process. If your downloader and SiCKRAGE aren't on the same PC make sure you fill out your TV download dir in the config.",
            sickrage.srLogger.DEBUG)
        return result.output

    path, dirs, files = get_path_dir_files(dirName, nzbName, proc_type)

    files = [x for x in files if notTorNZBFile(x)]
    SyncFiles = [x for x in files if isSyncFile(x)]

    # Don't post process if files are still being synced and option is activated
    if SyncFiles and sickrage.srConfig.POSTPONE_IF_SYNC_FILES:
        postpone = True

    nzbNameOriginal = nzbName

    if not postpone:
        result.output += logHelper("PostProcessing Path: %s" % path,
                                   sickrage.srLogger.INFO)
        result.output += logHelper("PostProcessing Dirs: [%s]" % dirs,
                                   sickrage.srLogger.DEBUG)

        rarFiles = [x for x in files if isRarFile(x)]
        rarContent = unRAR(path, rarFiles, force, result)
        files += rarContent
        videoFiles = [x for x in files if isMediaFile(x)]
        videoInRar = [x for x in rarContent if isMediaFile(x)]

        result.output += logHelper(
            "PostProcessing Files: [%s]" % ", ".join(files),
            sickrage.srLogger.DEBUG)
        result.output += logHelper(
            "PostProcessing VideoFiles: [%s]" % ", ".join(videoFiles),
            sickrage.srLogger.DEBUG)
        result.output += logHelper(
            "PostProcessing RarContent: [%s]" % ", ".join(rarContent),
            sickrage.srLogger.DEBUG)
        result.output += logHelper(
            "PostProcessing VideoInRar: [%s]" % ", ".join(videoInRar),
            sickrage.srLogger.DEBUG)

        # If nzbName is set and there's more than one videofile in the folder, files will be lost (overwritten).
        if len(videoFiles) >= 2:
            nzbName = None

        if not process_method:
            process_method = sickrage.srConfig.PROCESS_METHOD

        result.result = True

        # Don't Link media when the media is extracted from a rar in the same path
        if process_method in ('hardlink', 'symlink') and videoInRar:
            process_media(path, videoInRar, nzbName, 'move', force,
                          is_priority, result)
            delete_files(path, rarContent, result)
            for video in set(videoFiles) - set(videoInRar):
                process_media(path, [video], nzbName, process_method, force,
                              is_priority, result)
        elif sickrage.srConfig.DELRARCONTENTS and videoInRar:
            process_media(path, videoInRar, nzbName, process_method, force,
                          is_priority, result)
            delete_files(path, rarContent, result, True)
            for video in set(videoFiles) - set(videoInRar):
                process_media(path, [video], nzbName, process_method, force,
                              is_priority, result)
        else:
            for video in videoFiles:
                process_media(path, [video], nzbName, process_method, force,
                              is_priority, result)

    else:
        result.output += logHelper(
            "Found temporary sync files, skipping post processing for: %s" %
            path)
        result.output += logHelper("Sync Files: [%s] in path %s" %
                                   (", ".join(SyncFiles), path))
        result.missedfiles.append("%s : Syncfiles found" % path)

    # Process Video File in all TV Subdir
    for curDir in [
            x for x in dirs
            if validateDir(path, x, nzbNameOriginal, failed, result)
    ]:

        result.result = True

        for processPath, _, fileList in os.walk(os.path.join(path, curDir),
                                                topdown=False):

            if not validateDir(path, processPath, nzbNameOriginal, failed,
                               result):
                continue

            postpone = False

            SyncFiles = [x for x in fileList if isSyncFile(x)]

            # Don't post process if files are still being synced and option is activated
            if SyncFiles and sickrage.srConfig.POSTPONE_IF_SYNC_FILES:
                postpone = True

            if not postpone:
                rarFiles = [x for x in fileList if isRarFile(x)]
                rarContent = unRAR(processPath, rarFiles, force, result)
                fileList = set(fileList + rarContent)
                videoFiles = [x for x in fileList if isMediaFile(x)]
                videoInRar = [x for x in rarContent if isMediaFile(x)]
                notwantedFiles = [x for x in fileList if x not in videoFiles]
                if notwantedFiles:
                    result.output += logHelper(
                        "Found unwanted files: [%s]" %
                        ", ".join(notwantedFiles), sickrage.srLogger.DEBUG)

                # Don't Link media when the media is extracted from a rar in the same path
                if process_method in ('hardlink', 'symlink') and videoInRar:
                    process_media(processPath, videoInRar, nzbName, 'move',
                                  force, is_priority, result)
                    process_media(processPath,
                                  set(videoFiles) - set(videoInRar), nzbName,
                                  process_method, force, is_priority, result)
                    delete_files(processPath, rarContent, result)
                elif sickrage.srConfig.DELRARCONTENTS and videoInRar:
                    process_media(processPath, videoInRar, nzbName,
                                  process_method, force, is_priority, result)
                    process_media(processPath,
                                  set(videoFiles) - set(videoInRar), nzbName,
                                  process_method, force, is_priority, result)
                    delete_files(processPath, rarContent, result, True)
                else:
                    process_media(processPath, videoFiles, nzbName,
                                  process_method, force, is_priority, result)

                    # Delete all file not needed
                    if process_method != "move" or not result.result \
                            or (
                                            proc_type == "manual" and not delete_on):  # Avoid to delete files if is Manual PostProcessing
                        continue

                    delete_files(processPath, notwantedFiles, result)

                    if (not sickrage.srConfig.NO_DELETE or proc_type == "manual") and process_method == "move" and \
                                    os.path.normpath(processPath) != os.path.normpath(sickrage.srConfig.TV_DOWNLOAD_DIR):
                        if delete_folder(processPath, check_empty=True):
                            result.output += logHelper(
                                "Deleted folder: %s" % processPath,
                                sickrage.srLogger.DEBUG)
            else:
                result.output += logHelper(
                    "Found temporary sync files, skipping post processing for: %s"
                    % processPath)
                result.output += logHelper("Sync Files: [%s] in path %s" %
                                           (", ".join(SyncFiles), processPath))
                result.missedfiles.append("%s : Syncfiles found" % processPath)

    if result.aggresult:
        result.output += logHelper("Processing completed")
        if result.missedfiles:
            result.output += logHelper(
                "I did encounter some unprocessable items: [%s]" %
                ", ".join(result.missedfiles))
    else:
        result.output += logHelper(
            "Problem(s) during processing, failed the following files/folders:  [%s]"
            % ", ".join(result.missedfiles), sickrage.srLogger.WARNING)

    return result.output