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

    :param matches:
    :type matches: rebulk.match.Matches
    :param context:
    :type context: dict
    :return:
    """
    fileparts = matches.markers.named("path")
    for filepart in marker_sorted(fileparts, matches):
        title = matches.range(
            filepart.start,
            filepart.end,
            predicate=lambda match: match.name == "title",
            index=0,
        )
        if not title:
            continue

        after_title = matches.next(
            title,
            index=0,
            predicate=lambda match: (
                match.end <= filepart.end and match.name in self.affected_names
            ),
        )

        # only if there's a country or year
        if not after_title:
            continue

        # skip if season == year. E.g.: Show.Name.S2016E01
        if matches.conflicting(
            after_title, predicate=lambda match: match.name == "season", index=0
        ):
            continue

        # Only add country or year if the next match is season, episode or date
        next_match = matches.next(
            after_title,
            index=0,
            predicate=lambda match: match.name in ("season", "episode", "date"),
        )
        if next_match:
            alias = copy.copy(title)
            alias.name = "alias"
            alias.value = alias.value + " " + re.sub(r"\W*", "", str(after_title.raw))
            alias.end = after_title.end
            alias.raw_end = after_title.raw_end
            return alias


# Traceback (ValueError)
2016-12-27 13:45:22 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [ExtraTorrent] :: [481b019] Traceback (most recent call last):
File "/home/pi/Medusa/medusa/search/queue.py", line 251, in run
found_results = search_for_needed_episodes()
File "/home/pi/Medusa/medusa/search/core.py", line 412, in search_for_needed_episodes
cur_provider.cache.update_cache()
File "/home/pi/Medusa/medusa/tv_cache.py", line 209, in update_cache
ci = self._parse_item(item)
File "/home/pi/Medusa/medusa/tv_cache.py", line 282, in _parse_item
return self.add_cache_entry(title, url, seeders, leechers, size, pubdate)
File "/home/pi/Medusa/medusa/tv_cache.py", line 367, in add_cache_entry
parse_result = NameParser().parse(name)
File "/home/pi/Medusa/medusa/name_parser/parser.py", line 229, in parse
result = self._parse_string(name)
File "/home/pi/Medusa/medusa/name_parser/parser.py", line 63, in _parse_string
guess = guessit.guessit(name, dict(show_type=self.show_type))
File "/home/pi/Medusa/medusa/name_parser/guessit_parser.py", line 73, in guessit
result = default_api.guessit(name, options=final_options)
File "/home/pi/Medusa/lib/guessit/api.py", line 131, in guessit
raise GuessitException(string, options)
GuessitException: An internal error has occured in guessit.
===================== Guessit Exception Report =====================
version=2.1.2.dev0
string=(BBC.kids).CBeebies.cartoon.pack.MP4s.+.subs.BigJ0554
options={'allowed_languages': set(['ru', 'hu', 'en', 'nl', 'pt', 'de', 'jp', 'sv', 'it', 'fr', 'es', 'uk', 'ro', 'pl', 'he']), 'expected_group': ['TV2LAX9', 'S4L', 'DHD'], 'expected_title': ['12 Monkeys', 'The 100', 'Warehouse 13', 'Sense8', 'Outrageous 911', 'Atlas 4D', 'Batman: The 1943 Serial', '11.22.63', 'Deutschland 83', '10,000', 'Countryside 999', 'Top 20 Most Shocking', 'The \xc2\xa3100K House: Tricks of the Trade', '60 Days In', '3%', '24: Legacy', 'The 1980s: The Deadliest Decade', 'Continent 7: Antarctica'], 'allowed_countries': set(['gb', 'us']), 'episode_prefer_number': False, 'show_type': None, 'type': 'episode', 'implicit': True}
--------------------------------------------------------------------
Traceback (most recent call last):
File "/home/pi/Medusa/lib/guessit/api.py", line 120, in guessit
matches = self.rebulk.matches(string, options)
File "/home/pi/Medusa/lib/rebulk/rebulk.py", line 288, in matches
self._execute_rules(matches, context)
File "/home/pi/Medusa/lib/rebulk/rebulk.py", line 319, in _execute_rules
rules.execute_all_rules(matches, context)
File "/home/pi/Medusa/lib/rebulk/rules.py", line 318, in execute_all_rules
when_response = execute_rule(rule, matches, context)
File "/home/pi/Medusa/lib/rebulk/rules.py", line 340, in execute_rule
if when_response:
ValueError: __len__() should return >= 0
--------------------------------------------------------------------
Please report at https://github.com/guessit-io/guessit/issues.
====================================================================

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

    :param matches:
    :type matches: rebulk.match.Matches
    :param context:
    :type context: dict
    :return:
    """
    fileparts = matches.markers.named("path")
    for filepart in marker_sorted(fileparts, matches):
        title = matches.range(
            filepart.start,
            filepart.end,
            predicate=lambda match: match.name == "title",
            index=0,
        )
        if not title:
            continue

        after_title = matches.next(
            title,
            index=0,
            predicate=lambda match: (
                match.end <= filepart.end and match.name in self.affected_names
            ),
        )

        # only if there's a country or year
        if not after_title:
            continue

        # skip if season == year. E.g.: Show.Name.S2016E01
        if matches.conflicting(
            after_title, predicate=lambda match: match.name == "season", index=0
        ):
            continue

        # Only add country or year if the next match is season, episode or date
        next_match = matches.next(
            after_title,
            index=0,
            predicate=lambda match: match.name in ("season", "episode", "date"),
        )
        if next_match:
            alias = copy.copy(title)
            alias.name = "alias"
            alias.value = alias.value + " " + re.sub(r"\W*", "", str(after_title.raw))
            alias.end = after_title.end
            alias.raw_end = after_title.raw_end
            return [alias]