# Buggy code
def __init__(self, *args, **kwargs):  # pylint: disable=too-many-locals,too-many-arguments
    """Tmdb api constructor."""
    super(Tmdb, self).__init__(*args, **kwargs)

    self.tmdb = tmdb
    self.tmdb.API_KEY = TMDB_API_KEY
    self.tmdb.REQUESTS_SESSION = self.config["session"]
    self.tmdb_configuration = self.tmdb.Configuration()
    try:
        self.response = self.tmdb_configuration.info()
    except RequestException as e:
        raise IndexerUnavailable(
            "Indexer TMDB is unavailable at this time. Cause: {cause}".format(cause=e)
        )

    self.config["artwork_prefix"] = "{base_url}{image_size}{file_path}"

    # An api to indexer series/episode object mapping
    self.series_map = {
        "id": "id",
        "name": "seriesname",
        "original_name": "aliasnames",
        "overview": "overview",
        "air_date": "firstaired",
        "first_air_date": "firstaired",
        "backdrop_path": "fanart",
        "url": "show_url",
        "episode_number": "episodenumber",
        "season_number": "seasonnumber",
        "dvd_episode_number": "dvd_episodenumber",
        "last_air_date": "airs_dayofweek",
        "last_updated": "lastupdated",
        "network_id": "networkid",
        "vote_average": "contentrating",
        "poster_path": "poster",
        "genres": "genre",
        "type": "classification",
        "networks": "network",
        "episode_run_time": "runtime",
    }

    self.episodes_map = {
        "id": "id",
        "name": "episodename",
        "overview": "overview",
        "air_date": "firstaired",
        "episode_run_time": "runtime",
        "episode_number": "episodenumber",
        "season_number": "seasonnumber",
        "vote_average": "contentrating",
        "still_path": "filename",
    }


# Traceback (ZeroDivisionError)
2018-04-14 23:42:05 DEBUG    ThreadPoolExecutor-0_1 :: [b4c38d5] 78783: Saving to database: Hunting Nazi Treasure
2018-04-14 23:42:20 DEBUG    ThreadPoolExecutor-0_1 :: [b4c38d5] 78783: Queueing show refresh for Hunting Nazi Treasure
2018-04-14 23:42:20 DEBUG    ThreadPoolExecutor-0_1 :: [b4c38d5] Finished editing show: Hunting Nazi Treasure
2018-04-14 23:42:22 INFO     SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Performing refresh on Hunting Nazi Treasure
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Loading all episodes from the show directory: /media/ssd/Shows/Hunting Nazi Treasure
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Found files: [u'/media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv', u'/media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E02.The.Mystery.of.Rommels.Gold.HDTV.x264-CAFFEiNE.mkv', u'/media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E03.Hitlers.Obsession.HDTV.x264-CAFFEiNE.mkv', u'/media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E04.Nazi.Gold.HDTV.x264-CAFFEiNE.mkv', u'/media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E06.Blood.Treasure.HDTV.x264-CAFFEiNE.mkv', u'/media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E07.City.of.Loot.HDTV.x264-CAFFEiNE.mkv', u'/media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E08.Stealing.Italy.HDTV.x264-CAFFEiNE.mkv']
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Creating episode from: /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Creating episode object from /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Scene numbering enabled series Hunting Nazi Treasure using indexer numbering: S01E01
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Converted parsed result /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv into title: Hunting Nazi Treasure, season: 1, episode: [1], episode_title: Pilot, episode_details: Pilot, format: HDTV, video_codec: h264, video_encoder: x264, release_group: CAFFEiNE, container: mkv, mimetype: video/x-matroska, type: episode, parsing_time: 0.366448879242, total_time: None, absolute_episode: [], quality: SDTV
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Matched release /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv to a series in your database: Hunting Nazi Treasure
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Parsed /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv into title: Hunting Nazi Treasure, season: 1, episode: [1], episode_title: Pilot, episode_details: Pilot, format: HDTV, video_codec: h264, video_encoder: x264, release_group: CAFFEiNE, container: mkv, mimetype: video/x-matroska, type: episode, parsing_time: 0.366448879242, total_time: 0.390615940094, absolute_episode: [], quality: SDTV
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv parsed to Hunting Nazi Treasure S01E01
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Setter sets location to /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Checking if /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.nfo exists: True
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Checking if /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE-thumb.jpg exists: False
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Setter sets location to /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Checking if /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.nfo exists: True
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Checking if /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE-thumb.jpg exists: False
2018-04-14 23:42:22 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Not changing current status 'Downloaded (SDTV)' based on file: /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.mkv. Reason: New file is the same as current file
2018-04-14 23:42:23 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Transaction with 1 queries executed
2018-04-14 23:42:23 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Checking if /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE.nfo exists: True
2018-04-14 23:42:23 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Checking if /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE-thumb.jpg exists: False
2018-04-14 23:42:23 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Matched release /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE to a series in your database: Hunting Nazi Treasure
2018-04-14 23:42:23 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Parsed /media/ssd/Shows/Hunting Nazi Treasure/Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE into title: Hunting Nazi Treasure, season: 1, episode: [1], episode_title: Pilot, episode_details: Pilot, format: HDTV, video_codec: h264, video_encoder: x264, release_group: CAFFEiNE, type: episode, parsing_time: 0.411699056625, total_time: 0.412950992584, absolute_episode: [], quality: SDTV
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Matched release Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE to a series in your database: Hunting Nazi Treasure
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Parsed Hunting.Nazi.Treasure.S01E01.Pilot.HDTV.x264-CAFFEiNE into title: Hunting Nazi Treasure, season: 1, episode: [1], episode_title: Pilot, episode_details: Pilot, format: HDTV, video_codec: h264, video_encoder: x264, release_group: CAFFEiNE, type: episode, parsing_time: 0.245923995972, total_time: 0.247278928757, absolute_episode: [], quality: SDTV
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Found codec for Hunting Nazi Treasure Pilot
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Formatting pattern: %SN - S%0SE01 - %EN -> Hunting Nazi Treasure - S01E01 - Pilot
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Metadata provider KODI 12+ creating episode thumbnail for Hunting Nazi Treasure - S01E01 - Pilot
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Using language from show settings: en
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] GET URL: https://api.themoviedb.org/3/configuration?api_key=**********
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] User-Agent: Medusa/0.1.23 (Linux; 4.9.35-v7+; 5b3a1fbc-4032-11e8-8594-b827eb6b4d4c)
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Using language from show settings: en
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] GET URL: https://api.themoviedb.org/3/configuration?api_key=**********
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] User-Agent: Medusa/0.1.23 (Linux; 4.9.35-v7+; 5b3a1fbc-4032-11e8-8594-b827eb6b4d4c)
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Configured language en override show language of en
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Getting all series data for 78783
2018-04-14 23:42:24 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Getting all show data for 78783
2018-04-14 23:42:25 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] GET URL: https://api.themoviedb.org/3/tv/78783?api_key=**********&language=en%2Cnull [Status: 200]
2018-04-14 23:42:25 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] User-Agent: Medusa/0.1.23 (Linux; 4.9.35-v7+; 5b3a1fbc-4032-11e8-8594-b827eb6b4d4c)
2018-04-14 23:42:26 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] GET URL: https://api.themoviedb.org/3/tv/78783/external_ids?api_key=**********
2018-04-14 23:42:26 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] User-Agent: Medusa/0.1.23 (Linux; 4.9.35-v7+; 5b3a1fbc-4032-11e8-8594-b827eb6b4d4c)
2018-04-14 23:42:26 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Getting all episodes of 78783
2018-04-14 23:42:26 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] GET URL: https://api.themoviedb.org/3/tv/78783/season/1?api_key=**********&language=en [Status: 200]
2018-04-14 23:42:26 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] User-Agent: Medusa/0.1.23 (Linux; 4.9.35-v7+; 5b3a1fbc-4032-11e8-8594-b827eb6b4d4c)
2018-04-14 23:42:26 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Getting show banners for 78783
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] GET URL: https://api.themoviedb.org/3/tv/78783/images?api_key=**********&params=include_image_language [Status: 200]
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] User-Agent: Medusa/0.1.23 (Linux; 4.9.35-v7+; 5b3a1fbc-4032-11e8-8594-b827eb6b4d4c)
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Adding base url for image: /kOiXiSQNHiDNMRqvcNmSXnLao6F.jpg
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Adding base url for image: /kOiXiSQNHiDNMRqvcNmSXnLao6F.jpg
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Adding base url for image: /kOiXiSQNHiDNMRqvcNmSXnLao6F.jpg
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Adding base url for image: /kOiXiSQNHiDNMRqvcNmSXnLao6F.jpg
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Adding base url for image: /kOiXiSQNHiDNMRqvcNmSXnLao6F.jpg
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Adding base url for image: /kOiXiSQNHiDNMRqvcNmSXnLao6F.jpg
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Adding base url for image: /kOiXiSQNHiDNMRqvcNmSXnLao6F.jpg
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] No banners found for 78783
2018-04-14 23:42:27 DEBUG    SHOWQUEUE-REFRESH :: [b4c38d5] Found 0 total fanarts for series 78783
2018-04-14 23:42:27 ERROR    SHOWQUEUE-REFRESH :: [b4c38d5] 78783: Error while refreshing show Hunting Nazi Treasure. Error: division by zero
Traceback (most recent call last):
  File "/home/pi/Medusa/medusa/show_queue.py", line 637, in run
    self.show.refresh_dir()
  File "/home/pi/Medusa/medusa/tv/series.py", line 1754, in refresh_dir
    self.load_episodes_from_dir()
  File "/home/pi/Medusa/medusa/tv/series.py", line 932, in load_episodes_from_dir
    cur_episode = self.make_ep_from_file(os.path.join(self.location, media_file))
  File "/home/pi/Medusa/medusa/tv/series.py", line 1398, in make_ep_from_file
    **********_ep.create_meta_files()
  File "/home/pi/Medusa/medusa/tv/episode.py", line 1111, in create_meta_files
    self.__create_thumbnail(metadata_provider)
  File "/home/pi/Medusa/medusa/tv/episode.py", line 1134, in __create_thumbnail
    result = metadata_provider.create_episode_thumb(self) or result
  File "/home/pi/Medusa/medusa/metadata/generic.py", line 331, in create_episode_thumb
    return self.save_thumbnail(ep_obj)
  File "/home/pi/Medusa/medusa/metadata/generic.py", line 497, in save_thumbnail
    thumb_data = self._retrieve_show_image(u'thumbnail', ep_obj.series, ep_obj)
  File "/home/pi/Medusa/medusa/metadata/generic.py", line 734, in _retrieve_show_image
    indexer_show_obj = self._get_show_data(show_obj)
  File "/home/pi/Medusa/medusa/metadata/generic.py", line 857, in _get_show_data
    my_show = self.indexer_api[int(series_id)]
  File "/home/pi/Medusa/medusa/indexers/indexer_base.py", line 402, in __getitem__
    self._get_show_data(key, self.config['language'])
  File "/home/pi/Medusa/medusa/indexers/tmdb/tmdb.py", line 469, in _get_show_data
    self._parse_images(sid)
  File "/home/pi/Medusa/medusa/indexers/tmdb/tmdb.py", line 370, in _parse_images
    self._save_images(sid, _images)
  File "/home/pi/Medusa/medusa/indexers/indexer_base.py", line 395, in _save_images
    self._save_images_by_type(img_type, series_id, images_by_type)
  File "/home/pi/Medusa/medusa/indexers/indexer_base.py", line 296, in _save_images_by_type
    rating_mean, rating_dev, ratings = pop_stats(merged_images, 'rating')
  File "/home/pi/Medusa/medusa/indexers/indexer_base.py", line 229, in pop_stats
    mean = total / num_values
ZeroDivisionError: division by zero

# Fixed code
def __init__(self, *args, **kwargs):  # pylint: disable=too-many-locals,too-many-arguments
    """Tmdb api constructor."""
    super(Tmdb, self).__init__(*args, **kwargs)

    self.tmdb = tmdb
    self.tmdb.API_KEY = TMDB_API_KEY
    self.tmdb.REQUESTS_SESSION = self.config["session"]
    self.tmdb_configuration = self.tmdb.Configuration()
    try:
        self.response = self.tmdb_configuration.info()
    except RequestException as e:
        raise IndexerUnavailable(
            "Indexer TMDB is unavailable at this time. Cause: {cause}".format(cause=e)
        )

    self.config["artwork_prefix"] = "{base_url}{image_size}{file_path}"

    # An api to indexer series/episode object mapping
    self.series_map = {
        "id": "id",
        "name": "seriesname",
        "original_name": "aliasnames",
        "overview": "overview",
        "air_date": "firstaired",
        "first_air_date": "firstaired",
        "backdrop_path": "fanart",
        "url": "show_url",
        "episode_number": "episodenumber",
        "season_number": "seasonnumber",
        "dvd_episode_number": "dvd_episodenumber",
        "last_air_date": "airs_dayofweek",
        "last_updated": "lastupdated",
        "network_id": "networkid",
        "vote_average": "rating",
        "poster_path": "poster",
        "genres": "genre",
        "type": "classification",
        "networks": "network",
        "episode_run_time": "runtime",
    }

    self.episodes_map = {
        "id": "id",
        "name": "episodename",
        "overview": "overview",
        "air_date": "firstaired",
        "episode_run_time": "runtime",
        "episode_number": "episodenumber",
        "season_number": "seasonnumber",
        "vote_average": "rating",
        "still_path": "filename",
    }