diff --git a/guessit/rules/common/formatters.py b/guessit/rules/common/formatters.py index 272c6a9..b0b10d8 100644 --- a/guessit/rules/common/formatters.py +++ b/guessit/rules/common/formatters.py @@ -13,6 +13,7 @@ for sep in seps: if sep not in _excluded_clean_chars: clean_chars += sep + def cleanup(input_string): """ Removes and strip separators from input_string (but keep ',;' characters) @@ -35,3 +36,40 @@ def strip(input_string): :rtype: """ return input_string.strip(seps) + + +def reorder_title(title, articles=('the',), separators=(',', ', ')): + """ + Reorder the title + :param title: + :type title: + :param articles: + :type articles: + :param separators: + :type separators: + :return: + :rtype: + """ + ltitle = title.lower() + for article in articles: + for separator in separators: + suffix = separator + article + if ltitle[-len(suffix):] == suffix: + return title[-len(suffix) + len(separator):] + ' ' + title[:-len(suffix)] + return title + + +def chain(*formatters): + """ + Chain formatter functions + :param functions: + :type functions: + :return: + :rtype: + """ + def formatters_chain(input_string): # pylint:disable=missing-docstring + for formatter in formatters: + input_string = formatter(input_string) + return input_string + + return formatters_chain diff --git a/guessit/rules/properties/episode_title.py b/guessit/rules/properties/episode_title.py index 96ab593..640210d 100644 --- a/guessit/rules/properties/episode_title.py +++ b/guessit/rules/properties/episode_title.py @@ -5,7 +5,7 @@ Episode title """ from rebulk import Rebulk, AppendMatchRule -from ..common.formatters import cleanup +from ..common.formatters import cleanup, reorder_title, chain class EpisodeTitleFromPosition(AppendMatchRule): @@ -19,12 +19,17 @@ class EpisodeTitleFromPosition(AppendMatchRule): filename = matches.markers.named('path', -1) start, end = filename.span - second_hole = matches.holes(start, end + 1, formatter=cleanup, predicate=lambda hole: hole.value, index=1) + second_hole = matches.holes(start, end + 1, formatter=chain(cleanup, reorder_title), + predicate=lambda hole: hole.value, index=1) if second_hole: episode = matches.previous(second_hole, lambda previous: previous.name in ['episodeNumber', 'season'], 0) if episode: - second_hole.name = 'episodeTitle' - return second_hole + group_markers = matches.markers.named('group') + title = second_hole.crop(group_markers, index=0) + + if title and title.value: + title.name = 'episodeTitle' + return title EPISODE_TITLE = Rebulk().rules(EpisodeTitleFromPosition) diff --git a/guessit/rules/properties/title.py b/guessit/rules/properties/title.py index 8461f89..ca8e6b2 100644 --- a/guessit/rules/properties/title.py +++ b/guessit/rules/properties/title.py @@ -3,14 +3,15 @@ """ Title """ -from rebulk import Rebulk, AppendMatchRule, RemoveMatchRule +from rebulk import Rebulk, RemoveMatchRule, AppendRemoveMatchRule -from ..common.formatters import cleanup +from ..common.formatters import cleanup, reorder_title, chain from ..common.comparators import marker_sorted from ..common import seps +from rebulk.rules import AppendRemoveMatchRule -class TitleFromPosition(AppendMatchRule): +class TitleFromPosition(AppendRemoveMatchRule): """ Add title match in existing matches """ @@ -30,7 +31,8 @@ class TitleFromPosition(AppendMatchRule): """ start, end = filepart.span - first_hole = matches.holes(start, end + 1, formatter=cleanup, ignore=TitleFromPosition.ignore_language, + first_hole = matches.holes(start, end + 1, formatter=chain(cleanup, reorder_title), + ignore=TitleFromPosition.ignore_language, predicate=lambda hole: hole.value, index=0) to_remove = [] @@ -117,12 +119,6 @@ class TitleFromPosition(AppendMatchRule): return ret, to_remove - def then(self, matches, when_response, context): - titles, to_remove = when_response - super(TitleFromPosition, self).then(matches, titles, context) - for to_remove in when_response[1]: - matches.remove(to_remove) - class PreferTitleWithYear(RemoveMatchRule): """ diff --git a/guessit/test/series.yml b/guessit/test/series.yml index 249125a..f151b0a 100644 --- a/guessit/test/series.yml +++ b/guessit/test/series.yml @@ -20,3 +20,25 @@ releaseGroup: AlFleNi-TeaM website: tvu.org.ru extension: avi + +? Series/Treme/Treme.1x03.Right.Place,.Wrong.Time.HDTV.XviD-NoTV.avi +: title: Treme + season: 1 + episodeNumber: 3 + episodeTitle: Right Place, Wrong Time + format: HDTV + videoCodec: XviD + releaseGroup: NoTV + +? Series/Duckman/Duckman - S1E13 Joking The Chicken (unedited).avi +: title: Duckman + season: 1 + episodeNumber: 13 + episodeTitle: Joking The Chicken + +? Series/Simpsons/Saison 12 Français/Simpsons,.The.12x08.A.Bas.Le.Sergent.Skinner.FR.avi +: title: The Simpsons + season: 12 + episodeNumber: 8 + episodeTitle: A Bas Le Sergent Skinner + language: French