# Buggy code
def remove(self, indexer, indexer_id):
    """Remove cache item given indexer and indexer_id."""
    if not indexer or not indexer_id:
        return
    to_remove = [
        cached_name
        for cached_name, cached_parsed_result in iteritems(self.cache)
        if cached_parsed_result.series.indexer == indexer
        and cached_parsed_result.series.indexerid == indexer_id
    ]
    for item in to_remove:
        del self.cache[item]
        log.debug(
            "Removed parsed cached result for release: {release}".format(release=item)
        )


# Traceback (KeyError)
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Parsed the.voice.s11e15.720p.web.x264-heat into title: the voice, season: 11, episode: [15], screen_size: 720p, source: Web, video_codec: H.264, video_encoder: x264, release_group: heat, type: episode, parsing_time: 0.103935956955, total_time: 0.104511976242, absolute_episode: [], quality: 720p WEB-DL
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] File exists and the new file has the same size, aborting post-processing
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p WEB-DL ]
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] Processing succeeded for /root/wd/gnutella/new/torrents/complete/transmission/tv-shows/the.voice.s12e22.720p.web.x264-tbs/the.voice.s12e22.720p.web.x264-tbs S12E22 (10).mkv
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p WEB-DL'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Using cached parse result for The.Voice.S11E15.720p.WEB.x264-HEAT
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p WEB-DL ]
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] Processing /root/wd/gnutella/new/torrents/complete/transmission/tv-shows/the.voice.s12e22.720p.web.x264-tbs/the.voice.s12e22.720p.web.x264-tbs S12E22 (11).mkv
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p WEB-DL'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Analyzing name: the.voice.s12e22.720p.web.x264-tbs S12E22 (11).mkv
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Matched release The.Voice.S11E15.720p.HDTV.x264-ALTEREGO to a series in your database: The Voice
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Matched release the.voice.s12e22.720p.web.x264-tbs S12E22 (11).mkv to a series in your database: The Voice
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Parsed The.Voice.S11E15.720p.HDTV.x264-ALTEREGO into title: The Voice, season: 11, episode: [15], screen_size: 720p, source: HDTV, video_codec: H.264, video_encoder: x264, release_group: ALTEREGO, type: episode, parsing_time: 0.122636079788, total_time: 0.123011112213, absolute_episode: [], quality: 720p HDTV
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Parsed the.voice.s12e22.720p.web.x264-tbs S12E22 (11).mkv into title: the voice, season: 12, episode: [22], screen_size: 720p, source: Web, video_codec: H.264, video_encoder: x264, container: mkv, mimetype: video/x-matroska, type: episode, parsing_time: 0.156554937363, total_time: 0.156819820404, absolute_episode: [], quality: 720p WEB-DL
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p HDTV ]
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Parse result not sufficient (all following have to be set). Won't save release name
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Parse result (series_name): the voice
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p HDTV'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Parse result (season_number): 12
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Parse result (episode_numbers): [22]
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Using cached parse result for The.Voice.S11E15.720p.HDTV.x264-ALTEREGO
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Parse result (ab_episode_numbers): []
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p HDTV ]
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Parse result (air_date): None
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Parse result (release_group): None
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p HDTV'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Show information parsed from file name
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Retrieving episode object for The Voice S12E22
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Using cached parse result for The.Voice.S11E15.720p.HDTV.x264-ALTEREGO
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] The episode file has a quality in it, using that: 720p WEB-DL
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p HDTV ]
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Quality of the episode we are processing: 720p WEB-DL
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p HDTV'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Snatch in history: False
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Manually snatched: False
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Using cached parse result for The.Voice.S11E15.720p.HDTV.x264-ALTEREGO
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Info hash: None
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p HDTV ]
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] NZB: False
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Current quality: 720p WEB-DL
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p HDTV'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] New quality: 720p WEB-DL
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Proper: False
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Using cached parse result for The.Voice.S11E15.720p.HDTV.x264-ALTEREGO
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] This episode is a priority download: False
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p HDTV ]
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p HDTV'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] Old file: /lvm1/kodi/tv-shows/The Voice (2011)/Season 12/The Voice - S12E22 - Live Top 11 Eliminations.mkv
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] New file: /root/wd/gnutella/new/torrents/complete/transmission/tv-shows/the.voice.s12e22.720p.web.x264-tbs/the.voice.s12e22.720p.web.x264-tbs S12E22 (11).mkv
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Using cached parse result for The.Voice.S11E15.720p.HDTV.x264-ALTEREGO
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] Old size: 550.31 MB
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p HDTV ]
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] New size: 550.31 MB
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] New file is the same size.
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p HDTV'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] File exists and the new file has the same size, aborting post-processing
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] Processing succeeded for /root/wd/gnutella/new/torrents/complete/transmission/tv-shows/the.voice.s12e22.720p.web.x264-tbs/the.voice.s12e22.720p.web.x264-tbs S12E22 (11).mkv
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Using cached parse result for The.Voice.S11E15.720p.HDTV.x264-ALTEREGO
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: Allowed, Preferred = [ 720p HDTV, 720p WEB-DL, 720p BluRay ] [ 1080p HDTV, 1080p WEB-DL, 1080p BluRay ] Found = [ 720p HDTV ]
2019-12-28 20:47:13 INFO     POSTPROCESSOR :: [d0c136d] Processing /root/wd/gnutella/new/torrents/complete/transmission/tv-shows/the.voice.s12e22.720p.web.x264-tbs/the.voice.s12e22.720p.web.x264-tbs S12E22 (15).mkv
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] 247824: 'The Voice' S11E15 status is: 'Snatched'. Ignoring result with quality '720p HDTV'. Reason: New quality is same/lower quality (and not preferred). Ignoring new quality
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Analyzing name: the.voice.s12e22.720p.web.x264-tbs S12E22 (15).mkv
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] No episodes [15] of season 11 are needed with this quality for The Voice
2019-12-28 20:47:13 DEBUG    POSTPROCESSOR :: [d0c136d] Matched release the.voice.s12e22.720p.web.x264-tbs S12E22 (15).mkv to a series in your database: The Voice
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Matched release The.Voice.US.S11E15.720p.HDTV.x264 to a series in your database: The Voice
2019-12-28 20:47:13 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Hydra] :: [d0c136d] Parsed The.Voice.US.S11E15.720p.HDTV.x264 into title: The Voice, alias: The Voice US, country: US, season: 11, episode: [15], screen_size: 720p, source: HDTV, video_codec: H.264, video_encoder: x264, type: episode, parsing_time: 0.118921041489, total_time: 0.119252920151, absolute_episode: [], quality: 720p HDTV
2019-12-28 20:47:13 ERROR    POSTPROCESSOR :: [d0c136d] Exception generated: u'Ghost.Hunters.S06E09.Spirits.of.the.Night.1080i.HDTV.DD5.1.MPEG2-TrollHD'
Traceback (most recent call last):
  File "/opt/Medusa-master/medusa/scheduler.py", line 93, in run
    self.action.run(self.force)
  File "/opt/Medusa-master/medusa/process_tv.py", line 65, in run
    return ProcessResult(path, process_method).process(force=force, **kwargs)
  File "/opt/Medusa-master/medusa/process_tv.py", line 188, in process
    ignore_subs=ignore_subs)
  File "/opt/Medusa-master/medusa/process_tv.py", line 378, in process_files
    self.process_media(path, self.video_files, force, is_priority, ignore_subs)
  File "/opt/Medusa-master/medusa/process_tv.py", line 599, in process_media
    self.result = processor.process()
  File "/opt/Medusa-master/medusa/post_processor.py", line 1015, in process
    (series_obj, season, episodes, quality, version) = self._find_info()
  File "/opt/Medusa-master/medusa/post_processor.py", line 607, in _find_info
    series_obj, season, episodes, quality, version, airdate = self._parse_info()
  File "/opt/Medusa-master/medusa/post_processor.py", line 568, in _parse_info
    cur_show, cur_season, cur_episodes, cur_quality, cur_version = self._analyze_name(name)
  File "/opt/Medusa-master/medusa/post_processor.py", line 680, in _analyze_name
    parse_result = NameParser().parse(name)
  File "/opt/Medusa-master/medusa/name_parser/parser.py", line 395, in parse
    name_parser_cache.add(name, result)
  File "/opt/Medusa-master/medusa/name_parser/parser.py", line 604, in add
    self.cache.popitem(last=False)
  File "/usr/lib64/python2.7/collections.py", line 178, in popitem
    value = self.pop(key)
  File "/usr/lib64/python2.7/collections.py", line 160, in pop
    raise KeyError(key)
KeyError: u'Ghost.Hunters.S06E09.Spirits.of.the.Night.1080i.HDTV.DD5.1.MPEG2-TrollHD'

# Fixed code
def remove(self, indexer, indexer_id):
    """Remove cache item given indexer and indexer_id."""
    with self.lock:
        to_remove = [
            cached_name
            for cached_name, cached_parsed_result in iteritems(self.cache)
            if cached_parsed_result.series.indexer == indexer
            and cached_parsed_result.series.indexerid == indexer_id
        ]
        for item in to_remove:
            del self.cache[item]
            log.debug("Removed cached parse result for {name}", {"name": item})