Skip to content

Commit

Permalink
Fixes #331, #435 and #440: 'year' and 'part' shouldn't be detected wh…
Browse files Browse the repository at this point in the history
…en in 'episode_title' location
  • Loading branch information
ratoaq2 committed May 21, 2017
1 parent 268167b commit 09e9246
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 16 deletions.
86 changes: 73 additions & 13 deletions guessit/rules/properties/episode_title.py
Expand Up @@ -5,7 +5,7 @@
"""
from collections import defaultdict

from rebulk import Rebulk, Rule, AppendMatch, RenameMatch, POST_PROCESS
from rebulk import Rebulk, Rule, AppendMatch, RemoveMatch, RenameMatch, POST_PROCESS

from ..common import seps, title_seps
from ..common.formatters import cleanup
Expand All @@ -19,15 +19,75 @@ def episode_title():
:return: Created Rebulk object
:rtype: Rebulk
"""
rebulk = Rebulk().rules(EpisodeTitleFromPosition,
AlternativeTitleReplace,
previous_names = ('episode', 'episode_details', 'episode_count',
'season', 'season_count', 'date', 'title', 'year')

rebulk = Rebulk().rules(RemoveConflictsWithEpisodeTitle(previous_names),
EpisodeTitleFromPosition(previous_names),
AlternativeTitleReplace(previous_names),
TitleToEpisodeTitle,
Filepart3EpisodeTitle,
Filepart2EpisodeTitle,
RenameEpisodeTitleWhenMovieType)
return rebulk


class RemoveConflictsWithEpisodeTitle(Rule):
"""
Remove conflicting matches that might lead to wrong episode_title parsing.
"""

priority = 64
consequence = RemoveMatch

def __init__(self, previous_names):
super(RemoveConflictsWithEpisodeTitle, self).__init__()
self.previous_names = previous_names
self.next_names = ('streaming_service', 'screen_size', 'format',
'video_codec', 'audio_codec', 'other', 'container')
self.affected_if_holes_after = ('part', )
self.affected_names = ('part', 'year')

def when(self, matches, context):
to_remove = []
for filepart in matches.markers.named('path'):
for match in matches.range(filepart.start, filepart.end,
predicate=lambda m: m.name in self.affected_names):
before = matches.previous(match, index=0,
predicate=lambda m, fp=filepart: not m.private and m.start >= fp.start)
if not before or before.name not in self.previous_names:
continue

after = matches.next(match, index=0,
predicate=lambda m, fp=filepart: not m.private and m.end <= fp.end)
if not after or after.name not in self.next_names:
continue

group = matches.markers.at_match(match, predicate=lambda m: m.name == 'group', index=0)

def has_value_in_same_group(current_match, current_group=group):
"""Return true if current match has value and belongs to the current group."""
return current_match.value.strip(seps) and (
current_group == matches.markers.at_match(current_match,
predicate=lambda mm: mm.name == 'group', index=0)
)

holes_before = matches.holes(before.end, match.start, predicate=has_value_in_same_group)
holes_after = matches.holes(match.end, after.start, predicate=has_value_in_same_group)

if not holes_before and not holes_after:
continue

if match.name in self.affected_if_holes_after and not holes_after:
continue

to_remove.append(match)
if match.parent:
to_remove.append(match.parent)

return to_remove


class TitleToEpisodeTitle(Rule):
"""
If multiple different title are found, convert the one following episode number to episode_title.
Expand Down Expand Up @@ -65,12 +125,14 @@ class EpisodeTitleFromPosition(TitleBaseRule):
"""
dependency = TitleToEpisodeTitle

def __init__(self, previous_names):
super(EpisodeTitleFromPosition, self).__init__('episode_title', ['title'])
self.previous_names = previous_names

def hole_filter(self, hole, matches):
episode = matches.previous(hole,
lambda previous: any(name in previous.names
for name in ['episode', 'episode_details',
'episode_count', 'season', 'season_count',
'date', 'title', 'year']),
for name in self.previous_names),
0)

crc32 = matches.named('crc32')
Expand All @@ -88,9 +150,6 @@ def should_remove(self, match, matches, filepart, hole, context):
return False
return super(EpisodeTitleFromPosition, self).should_remove(match, matches, filepart, hole, context)

def __init__(self):
super(EpisodeTitleFromPosition, self).__init__('episode_title', ['title'])

def when(self, matches, context):
if matches.named('episode_title'):
return
Expand All @@ -104,6 +163,10 @@ class AlternativeTitleReplace(Rule):
dependency = EpisodeTitleFromPosition
consequence = RenameMatch

def __init__(self, previous_names):
super(AlternativeTitleReplace, self).__init__()
self.previous_names = previous_names

def when(self, matches, context):
if matches.named('episode_title'):
return
Expand All @@ -115,10 +178,7 @@ def when(self, matches, context):
if main_title:
episode = matches.previous(main_title,
lambda previous: any(name in previous.names
for name in ['episode', 'episode_details',
'episode_count', 'season',
'season_count',
'date', 'title', 'year']),
for name in self.previous_names),
0)

crc32 = matches.named('crc32')
Expand Down
1 change: 1 addition & 0 deletions guessit/rules/properties/website.py
Expand Up @@ -85,6 +85,7 @@ class ValidateWebsitePrefix(Rule):
"""
Validate website prefixes
"""
priority = 64
consequence = RemoveMatch

def when(self, matches, context):
Expand Down
46 changes: 45 additions & 1 deletion guessit/test/episodes.yml
Expand Up @@ -3856,4 +3856,48 @@
format: HDTV
video_codec: h264
release_group: MiNDTHEGAP
type: episode
type: episode

? -feud.s01e05.and.the.winner.is.(the.oscars.of.1963).720p.amzn.webrip.dd5.1.x264-casstudio.mkv
: year: 1963

? feud.s01e05.and.the.winner.is.(the.oscars.of.1963).720p.amzn.webrip.dd5.1.x264-casstudio.mkv
: title: feud
season: 1
episode: 5
episode_title: and the winner is
screen_size: 720p
streaming_service: Amazon Prime
format: WEBRip
audio_codec: DolbyDigital
audio_channels: '5.1'
video_codec: h264
release_group: casstudio
container: mkv
type: episode

? Adventure.Time.S08E16.Elements.Part.1.Skyhooks.720p.WEB-DL.AAC2.0.H.264-RTN.mkv
: title: Adventure Time
season: 8
episode: 16
season: 8
episode: 16
episode_title: Elements Part 1 Skyhooks
screen_size: 720p
format: WEB-DL
audio_codec: AAC
audio_channels: '2.0'
video_codec: h264
release_group: RTN
container: mkv
type: episode

? D:\TV\SITCOMS (CLASSIC)\That '70s Show\Season 07\That '70s Show - S07E22 - 2000 Light Years from Home.mkv
: title: That '70s Show
season: 7
episode: 22
episode_title: 2000 Light Years from Home
other: Classic
container: mkv
mimetype: video/x-matroska
type: episode
5 changes: 3 additions & 2 deletions guessit/test/various.yml
Expand Up @@ -761,14 +761,15 @@
type: episode
video_codec: h264

# Episode title is indeed 'October 8, 2014'
# https://thetvdb.com/?tab=episode&seriesid=82483&seasonid=569935&id=4997362&lid=7
? The Soup - 11x41 - October 8, 2014.mp4
: container: mp4
episode: 41
episode_title: October 8
episode_title: October 8, 2014
season: 11
title: The Soup
type: episode
year: 2014

? Red.Rock.S02E59.WEB-DLx264-JIVE
: episode: 59
Expand Down

0 comments on commit 09e9246

Please sign in to comment.