Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rule for anime season ep #10534

Merged
merged 3 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 103 additions & 0 deletions medusa/name_parser/rules/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,108 @@ def when(self, matches, context):
return to_remove, to_append


class AnimeWithMultipleSeasons(Rule):
"""Add season to title and remove episode for specific anime patterns.

There are animes where the title contains the format of `Title Season 2 - 01`
If the title can be found in expected titles, we can asume that the value after the dash (-)
is an episode.

Medusa rule:
- The title is found in expected titles.
- The value after the dash, is the episode.

e.g.: [Tsundere-Raws] Tate no Yuusha no Nariagari Season 2 - 03 VOSTFR (CR) [WEB 720p x264 AAC].mkv

guessit -t episode "[Tsundere-Raws] Tate no Yuusha no Nariagari Season 2 - 03 VOSTFR (CR) [WEB 720p x264 AAC].mkv"

without this rule:
For: [Tsundere-Raws] Tate no Yuusha no Nariagari Season 2 - 03.mkv
GuessIt found: {
"release_group": "Tsundere-Raws",
"title": "Tate no Yuusha no Nariagari",
"season": [
2,
3
],
"container": "mkv",
"mimetype": "video/x-matroska",
"type": "episode"
}

with this rule:
For: [Tsundere-Raws] Tate no Yuusha no Nariagari Season 2 - 03.mkv
GuessIt found: {
"release_group": "Tsundere-Raws",
"title": "Tate no Yuusha no Nariagari Season 2",
"episode": 3,
"container": "mkv",
"mimetype": "video/x-matroska",
"type": "episode"
}
"""

priority = POST_PROCESS
consequence = [RemoveMatch, AppendMatch]
ends_with_digit = re.compile(r'(_|\W)\d+$')

def when(self, matches, context):
"""Evaluate the rule.

:param matches:
:type matches: rebulk.match.Matches
:param context:
:type context: dict
:return:
"""
is_anime = context.get('show_type') == 'anime' or matches.tagged('anime')
if not is_anime:
return

titles = matches.named('title')
if not titles:
return

episodes = matches.named('episode')
if episodes:
return

seasons = matches.named('season')
if not seasons or len(seasons) > 1:
return

initiator_value = seasons[0].initiator.value
if '-' not in initiator_value:
return

if initiator_value.split('-')[0].strip() not in titles[0].value:
return

to_remove = []
to_append = []

fileparts = matches.markers.named('path')
for filepart in marker_sorted(fileparts, matches):
seasons = sorted(matches.range(filepart.start, filepart.end,
predicate=lambda match: match.name == 'season'))

title = matches.previous(seasons[0], index=-1,
predicate=lambda match: match.name == 'title' and match.end <= filepart.end)

if not title or not self.ends_with_digit.search(str(title.value)):
continue

season = seasons[0]
new_episode = copy.copy(season)
new_episode.name = 'episode'
new_episode.value = season.value

to_remove.append(season)
to_append.append(new_episode)

return to_remove, to_append


class OnePreGroupAsMultiEpisode(Rule):
"""Remove last episode (one) and add the first episode as absolute.

Expand Down Expand Up @@ -1797,6 +1899,7 @@ def rules():
FixInvalidAbsoluteReleaseGroups,
AnimeWithSeasonAbsoluteEpisodeNumbers,
AnimeWithSeasonMultipleEpisodeNumbers,
AnimeWithMultipleSeasons,
AnimeAbsoluteEpisodeNumbers,
AbsoluteEpisodeNumbers,
AbsoluteEpisodeWithX26Y,
Expand Down
1 change: 1 addition & 0 deletions tests/test_guessit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def show_list(create_tvshow):
create_tvshow(indexerid=17, name='Show! Name 2', anime=1),
create_tvshow(indexerid=18, name='24'), # expected titles shouldn't contain numbers
create_tvshow(indexerid=18, name='9-1-1'), # The dash in the title makes it an expected title
create_tvshow(indexerid=18, name='Tate no Yuusha no Nariagari Season 2'), # The number 2 will qualify this as an expected title
]


Expand Down
10 changes: 10 additions & 0 deletions tests/test_guessit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4589,3 +4589,13 @@
release_group: "Brawl in Cell Block 9-1-1"
container: mkv
type: episode

? "[Tsundere-Raws] Tate no Yuusha no Nariagari Season 2 - 09.mkv"
: options: {show_type: anime}
title: Tate no Yuusha no Nariagari Season 2
absolute_episode: 9
episode: 9
release_group: Tsundere-Raws
container: mkv
mimetype: "video/x-matroska"
type: episode