class Filepart2EpisodeTitle(Rule):
    """
    If we have at least 2 filepart structured like this:

    Serie name SO1/E01-episode_title.mkv
    AAAAAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBB

    If BBBB contains episode and AAA contains a hole followed by seasonNumber
    Then title is to be found in AAAA.
    """
    consequence = AppendMatch('title')

    def when(self, matches, context):
        fileparts = matches.markers.named('path')
        if len(fileparts) < 2:
            return

        filename = fileparts[-1]
        directory = fileparts[-2]

        episode_number = matches.range(filename.start, filename.end,
                                       lambda match: match.name == 'episode',
                                       0)
        if episode_number:
            season = matches.range(directory.start, directory.end,
                                   lambda match: match.name == 'season', 0)
            if season:
                hole = matches.holes(directory.start,
                                     directory.end,
                                     formatter=cleanup,
                                     seps=title_seps,
                                     predicate=lambda match: match.value,
                                     index=0)
                if hole:
                    return hole
Exemple #2
0
class Filepart2EpisodeTitle(Rule):
    """
    If we have at least 2 filepart structured like this:

    Serie name SO1/E01-episode_title.mkv
    AAAAAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBB

    If BBBB contains episode and AAA contains a hole followed by seasonNumber
    then title is to be found in AAAA.

    or

    Serie name/SO1E01-episode_title.mkv
    AAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBB

    If BBBB contains season and episode and AAA contains a hole
    then title is to be found in AAAA.
    """
    consequence = AppendMatch('title')

    def when(self, matches, context):  # pylint:disable=inconsistent-return-statements
        if matches.tagged('filepart-title'):
            return

        fileparts = matches.markers.named('path')
        if len(fileparts) < 2:
            return

        filename = fileparts[-1]
        directory = fileparts[-2]

        episode_number = matches.range(filename.start, filename.end,
                                       lambda match: match.name == 'episode',
                                       0)
        if episode_number:
            season = (matches.range(directory.start, directory.end,
                                    lambda match: match.name == 'season', 0) or
                      matches.range(filename.start, filename.end,
                                    lambda match: match.name == 'season', 0))
            if season:
                hole = matches.holes(
                    directory.start,
                    directory.end,
                    ignore=or_(lambda match: 'weak-episode' in match.tags,
                               TitleBaseRule.is_ignored),
                    formatter=cleanup,
                    seps=title_seps,
                    predicate=lambda match: match.value,
                    index=0)
                if hole:
                    hole.tags.append('filepart-title')
                    return hole
class Filepart3EpisodeTitle(Rule):
    """
    If we have at least 3 filepart structured like this:

    Serie name/SO1/E01-episode_title.mkv
    AAAAAAAAAA/BBB/CCCCCCCCCCCCCCCCCCCC

    Serie name/SO1/episode_title-E01.mkv
    AAAAAAAAAA/BBB/CCCCCCCCCCCCCCCCCCCC

    If CCCC contains episode and BBB contains seasonNumber
    Then title is to be found in AAAA.
    """
    consequence = AppendMatch('title')

    def when(self, matches, context):  # pylint:disable=inconsistent-return-statements
        if matches.tagged('filepart-title'):
            return

        fileparts = matches.markers.named('path')
        if len(fileparts) < 3:
            return

        filename = fileparts[-1]
        directory = fileparts[-2]
        subdirectory = fileparts[-3]

        episode_number = matches.range(filename.start, filename.end,
                                       lambda match: match.name == 'episode',
                                       0)
        if episode_number:
            season = matches.range(directory.start, directory.end,
                                   lambda match: match.name == 'season', 0)

            if season:
                hole = matches.holes(
                    subdirectory.start,
                    subdirectory.end,
                    ignore=lambda match: 'weak-episode' in match.tags,
                    formatter=cleanup,
                    seps=title_seps,
                    predicate=lambda match: match.value,
                    index=0)
                if hole:
                    return hole