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

    :param matches:
    :type matches: rebulk.match.Matches
    :param context:
    :type context: dict
    :return:
    """
    # Do not concatenate the titles if it's not an anime and there are languages
    if not matches.tagged("anime") and matches.named("language"):
        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

        if matches.range(
            filepart.start,
            filepart.end,
            predicate=lambda match: (
                match.name == "alternative_title"
                and match.value.lower() in self.blacklist
            ),
        ):
            continue

        alternative_titles = matches.range(
            filepart.start,
            filepart.end,
            predicate=lambda match: match.name == "alternative_title",
        )
        if not alternative_titles:
            continue

        previous = title
        alias = copy.copy(title)
        alias.name = "alias"
        alias.value = title.value

        # extended title is the concatenation between title and all alternative titles
        for alternative_title in alternative_titles:
            holes = matches.holes(start=previous.end, end=alternative_title.start)
            # if the separator is a dash, add an extra space before and after
            separators = [
                " " + h.value + " " if h.value == "-" else h.value for h in holes
            ]
            separator = " ".join(separators) if separators else " "
            alias.value += separator + alternative_title.value

            previous = alternative_title

        alias.end = previous.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:
    """
    # Do not concatenate the titles if it's not an anime and there are languages
    if not matches.tagged("anime") and matches.named("language"):
        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

        if matches.range(
            filepart.start,
            filepart.end,
            predicate=lambda match: (
                match.name == "alternative_title"
                and match.value.lower() in self.blacklist
            ),
        ):
            continue

        alternative_titles = matches.range(
            filepart.start,
            filepart.end,
            predicate=lambda match: match.name == "alternative_title",
        )
        if not alternative_titles:
            continue

        previous = title
        alias = copy.copy(title)
        alias.name = "alias"
        alias.value = title.value

        # extended title is the concatenation between title and all alternative titles
        for alternative_title in alternative_titles:
            holes = matches.holes(start=previous.end, end=alternative_title.start)
            # if the separator is a dash, add an extra space before and after
            separators = [
                " " + h.value + " " if h.value == "-" else h.value for h in holes
            ]
            separator = " ".join(separators) if separators else " "
            alias.value += separator + alternative_title.value

            previous = alternative_title

        alias.end = previous.end
        return [alias]