# Buggy code
def run(self):
    """Get the downloaded and/or snatched history"""
    history = History().get(self.limit, self.type).detailed

    def make_result(cur_item, cur_type):
        """
        Make an API result from a history item

        :param cur_item: to convert to API item
        :param cur_type: the type of action to return

        :returns: an API result
        """

        def convert_date(history_date):
            """
            Convert date from a history date to datetime format
            :param history_date: a date from the history
            :return: a formatted date string
            """
            return datetime.strptime(
                text_type(history_date), History.date_format
            ).strftime(dateTimeFormat)

        if cur_type in (statusStrings[cur_item.action].lower(), None):
            return {
                "date": convert_date(cur_item.date),
                "episode": cur_item.episode,
                "indexerid": cur_item.show_id,
                "provider": cur_item.provider,
                "quality": get_quality_string(cur_item.quality),
                "resource": os.path.basename(cur_item.resource),
                "resource_path": os.path.dirname(cur_item.resource),
                "season": cur_item.season,
                "show_name": cur_item.show_name,
                "status": statusStrings[cur_item.action],
                # Add tvdbid for backward compatibility
                # TODO: Make this actual tvdb id for other indexers
                "tvdbid": cur_item.show_id,
            }

    results = [make_result(x, self.type) for x in history if x]
    return _responds(RESULT_SUCCESS, results)


# Traceback (AttributeError)
2019-02-22 03:02:57 DEBUG    Thread_6 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.018264055252075195, season: None, quality: Unknown, total_time: 0.018332958221435547
2019-02-22 03:02:57 DEBUG    Thread_6 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.027241230010986328, season: None, quality: Unknown, total_time: 0.02730083465576172
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.024758100509643555, season: None, quality: Unknown, total_time: 0.02481818199157715
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.04372978210449219, season: None, quality: Unknown, total_time: 0.04378795623779297
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.024518966674804688, season: None, quality: Unknown, total_time: 0.024566173553466797
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 ERROR    Thread_6 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 130, in testNaming
    result = naming.test_name(pattern, multi, abd, sports, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 298, in test_name
    return {'name': ep.formatted_filename(pattern, multi, anime_type), 'dir': ep.formatted_dir(pattern, multi)}
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.02576303482055664, absolute_episode: [], quality: Unknown, total_time: 0.02585601806640625
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.036277055740356445, absolute_episode: [], quality: Unknown, total_time: 0.03634905815124512
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.030246973037719727, absolute_episode: [], quality: Unknown, total_time: 0.030331134796142578
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.02576303482055664, absolute_episode: [], quality: Unknown, total_time: 0.02585601806640625
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.036277055740356445, absolute_episode: [], quality: Unknown, total_time: 0.03634905815124512
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.016936779022216797, season: None, quality: Unknown, total_time: 0.01699090003967285
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.030246973037719727, absolute_episode: [], quality: Unknown, total_time: 0.030331134796142578
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Checking whether the pattern Season %S/%SN S%0SE%0E is valid for a multi episode
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.013588905334472656, season: None, quality: Unknown, total_time: 0.013649940490722656
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Show.Name.003-004.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.027174949645996094, season: None, quality: Unknown, total_time: 0.027235031127929688
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Show.Name.003-004.HDTV.x264-RLSGROUP into title: Show Name, absolute_episode: [3, 4], episode: [], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.016008853912353516, season: None, quality: Unknown, total_time: 0.016068220138549805
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.015719890594482422, absolute_episode: [], quality: Unknown, total_time: 0.0158078670501709
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.015719890594482422, absolute_episode: [], quality: Unknown, total_time: 0.0158078670501709
2019-02-22 03:02:57 ERROR    Thread_1 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 160, in isNamingValid
    is_valid = naming.check_valid_naming(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 151, in check_valid_naming
    valid = valid and validate_name(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 201, in validate_name
    new_name = ep.formatted_filename(pattern, multi, anime_type) + '.ext'
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Checking whether the pattern Season %S/%SN S%0SE%0E is valid for a multi episode
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.014174938201904297, season: None, quality: Unknown, total_time: 0.014236927032470703
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003-004.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.013724088668823242, season: None, quality: Unknown, total_time: 0.013782978057861328
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003-004.HDTV.x264-RLSGROUP into title: Show Name, absolute_episode: [3, 4], episode: [], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.015986919403076172, season: None, quality: Unknown, total_time: 0.016047000885009766
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:58 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:58 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:58 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:58 DEBUG    Thread_39 :: [ee861d0] Trying to parse Show Name S02E03.ext
2019-02-22 03:02:58 DEBUG    Thread_3 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:58 DEBUG    Thread_0 :: [ee861d0] Trying to parse Show Name S02E03.ext
2019-02-22 03:02:58 ERROR    Thread_3 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 160, in isNamingValid
    is_valid = naming.check_valid_naming(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 151, in check_valid_naming
    valid = valid and validate_name(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 201, in validate_name
    new_name = ep.formatted_filename(pattern, multi, anime_type) + '.ext'
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'

# Fixed code
def run(self):
    """Get the downloaded and/or snatched history"""
    history = History().get(self.limit, self.type).detailed

    def make_result(cur_item, cur_type):
        """
        Make an API result from a history item

        :param cur_item: to convert to API item
        :param cur_type: the type of action to return

        :returns: an API result
        """

        def convert_date(history_date):
            """
            Convert date from a history date to datetime format
            :param history_date: a date from the history
            :return: a formatted date string
            """
            return datetime.strptime(
                text_type(history_date), History.date_format
            ).strftime(dateTimeFormat)

        if cur_type in (statusStrings[cur_item.action].lower(), None):
            return {
                "date": convert_date(cur_item.date),
                "episode": cur_item.episode,
                "indexer": cur_item.indexer,
                "provider": cur_item.provider,
                "quality": get_quality_string(cur_item.quality),
                "resource": os.path.basename(cur_item.resource),
                "resource_path": os.path.dirname(cur_item.resource),
                "season": cur_item.season,
                "show_name": cur_item.show_name,
                "status": statusStrings[cur_item.action],
                # Add tvdbid for backward compatibility
                # TODO: Make this actual tvdb id for other indexers
                "tvdbid": cur_item.series_id,
            }

    results = [make_result(x, self.type) for x in history if x]
    return _responds(RESULT_SUCCESS, results)