Skip to content

Commit

Permalink
Merge pull request #7405 from pymedusa/fix/guessit-anime-title-as-mul…
Browse files Browse the repository at this point in the history
…ti-ep

Fix guessit anime titles treated as multiple episodes. Fixes #7396
  • Loading branch information
p0psicles authored Nov 24, 2019
2 parents 07575c5 + eb8dd84 commit 7bf9a13
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 3 deletions.
106 changes: 106 additions & 0 deletions medusa/name_parser/rules/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,111 @@ def when(self, matches, context):
return to_remove, to_append


class AnimeWithSeasonMultipleEpisodeNumbers(Rule):
"""Add season to title and remove episode for specific anime patterns.
There are animes where the title contains the season number and they
are mistakenly treated as multiple episodes instead.
Medusa rule:
- The season should be part of the series name
- The first episode should be removed
e.g.: [Mad le Zisell] High Score Girl 2 - 01 [720p]
guessit -t episode "[Mad le Zisell] High Score Girl 2 - 01 [720p]"
without this rule:
For: [Mad le Zisell] High Score Girl 2 - 01 [720p]
GuessIt found: {
"release_group": "Mad le Zisell",
"title": "High Score Girl",
"episode": [
2,
1
],
"screen_size": "720p",
"type": "episode"
}
with this rule:
For: [Mad le Zisell] High Score Girl 2 - 01 [720p]
GuessIt found: {
"release_group": "Mad le Zisell",
"title": "High Score Girl 2",
"episode": "1",
"absolute_episode": "1",
"screen_size": "720p",
"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:
"""
if context.get('show_type') == 'normal' or not matches.tagged('anime'):
return

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

episodes = matches.named('episode')
if not (2 <= len(episodes) <= 4):
return

unique_eps = set([ep.initiator.value for ep in episodes])
if len(unique_eps) != 2:
return

to_remove = []
to_append = []

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

if not episodes:
if len(titles) > 1:
bad_title = sorted(titles)[0]
to_remove.append(bad_title)
continue

title = matches.previous(episodes[0], index=-1,
predicate=lambda match: match.name == 'title' and match.end <= filepart.end)
if not title or self.ends_with_digit.search(str(title.value)):
continue

for i, episode in enumerate(episodes):
if i == 0:
new_title = copy.copy(title)
if six.PY3:
new_title.value = ' '.join([title.value, str(episode.value)])
else:
new_title.value = b' '.join([title.value, str(episode.value)])
new_title.end = episode.end

to_remove.append(title)
to_append.append(new_title)
to_remove.append(episode)

if i == 1 and len(episodes) in (3, 4):
to_remove.append(episode)

return to_remove, to_append


class AnimeAbsoluteEpisodeNumbers(Rule):
"""Move episode numbers to absolute episode numbers for animes.
Expand Down Expand Up @@ -1383,6 +1488,7 @@ def rules():
FixAnimeReleaseGroup,
FixInvalidAbsoluteReleaseGroups,
AnimeWithSeasonAbsoluteEpisodeNumbers,
AnimeWithSeasonMultipleEpisodeNumbers,
AnimeAbsoluteEpisodeNumbers,
AbsoluteEpisodeNumbers,
PartsAsEpisodeNumbers,
Expand Down
30 changes: 27 additions & 3 deletions tests/test_guessit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2012,9 +2012,9 @@

# Corner Case: Episode 2 with a number after it
? "[GroupName].Show.Name.-.02.5.(Special).[BD.1080p]"
: title: Show Name
absolute_episode: 2
episode: 2
: title: Show Name 2
absolute_episode: 5
episode: 5
episode_details: Special
screen_size: 1080p
source: BDRip
Expand Down Expand Up @@ -2961,6 +2961,30 @@
mimetype: video/x-matroska
type: episode

? "[Mad le Zisell] High Score Girl 2 - 01 [720p]"
: title: High Score Girl 2
absolute_episode: 1
episode: 1
screen_size: 720p
release_group: Mad le Zisell
type: episode

? "High Score Girl 2/[Mad le Zisell] High Score Girl 2 - 01 [720p]"
: title: High Score Girl 2
absolute_episode: 1
episode: 1
screen_size: 720p
release_group: Mad le Zisell
type: episode

? "High Score Girl/[Mad le Zisell] High Score Girl 2 - 01 [720p]"
: title: High Score Girl 2
absolute_episode: 1
episode: 1
screen_size: 720p
release_group: Mad le Zisell
type: episode

# Anime show containing number in its title and show with :
? /Anime/Mobile Suit Gundam UC RE0096/Season 01/Mobile Suit Gundam UC RE0096 - s01e15 - Title
: title: Mobile Suit Gundam UC RE0096
Expand Down

0 comments on commit 7bf9a13

Please sign in to comment.