# Buggy code
def download_subtitles(self, lang=None):
    """Download subtitles.

    :param lang:
    :type lang: string
    """
    if not self.is_location_valid():
        logger.debug(
            "{id}: {show} {ep} file doesn't exist, can't download subtitles",
            id=self.show.indexerid,
            show=self.show.name,
            ep=(
                episode_num(self.season, self.episode)
                or episode_num(self.season, self.episode, numbering="absolute")
            ),
        )
        return

    new_subtitles = subtitles.download_subtitles(self, lang=lang)
    if new_subtitles:
        self.subtitles = subtitles.merge_subtitles(self.subtitles, new_subtitles)

    self.subtitles_searchcount += 1 if self.subtitles_searchcount else 1
    self.subtitles_lastsearch = datetime.datetime.now().strftime(dateTimeFormat)
    logger.debug(
        "{id}: Saving last subtitles search to database", id=self.show.indexerid
    )
    self.save_to_db()

    if new_subtitles:
        subtitle_list = ", ".join(
            [subtitles.name_from_code(code) for code in new_subtitles]
        )
        logger.info(
            "{id}: Downloaded {subs} subtitles for {show} {ep}",
            id=self.show.indexerid,
            subs=subtitle_list,
            show=self.show.name,
            ep=(
                episode_num(self.season, self.episode)
                or episode_num(self.season, self.episode, numbering="absolute")
            ),
        )

        try:
            notifiers.notify_subtitle_download(self.pretty_name(), subtitle_list)
        except RequestException as e:
            logger.debug(
                "Unable to send subtitle download notification. Error: {error}",
                error=e.message,
            )
    else:
        logger.info(
            "{id}: No subtitles found for {show} {ep}",
            id=self.show.indexerid,
            show=self.show.name,
            ep=(
                episode_num(self.season, self.episode)
                or episode_num(self.season, self.episode, numbering="absolute")
            ),
        )

    return new_subtitles


# Traceback (gaierror)
2017-01-16 09:18:56 DEBUG    FINDSUBTITLES :: [49af1f0] [opensubtitles:pt   ] score = 355/352 for A.Series.of.Unfortunate.Events.s01e01.WebRip.x264-FS.srt
2017-01-16 09:18:56 DEBUG    FINDSUBTITLES :: [49af1f0] [opensubtitles:pt   ] score = 331/352 for A Series of Unfortunate Events s01e01.srt
2017-01-16 09:18:57 INFO     FINDSUBTITLES :: [49af1f0] Found subtitle for A.Series.of.Unfortunate.Events.S01E01.1080p.WEBRip.DD5.1.x264.mkv in opensubtitles provider with language por
2017-01-16 09:18:57 DEBUG    FINDSUBTITLES :: [49af1f0] Setting permissions for /media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E01.1080p.WEBRip.DD5.1.x264.pt.srt to 438 as parent directory has 511
2017-01-16 09:18:57 DEBUG    FINDSUBTITLES :: [49af1f0] history.logSubtitle opensubtitles, por
2017-01-16 09:18:57 DEBUG    FINDSUBTITLES :: [49af1f0] Cached video information under key medusa.subtitles:video|/media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E01.1080p.WEBRip.DD5.1.x264.mkv was invalidated
2017-01-16 09:18:57 DEBUG    FINDSUBTITLES :: [49af1f0] Scanning video /media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E01.1080p.WEBRip.DD5.1.x264.mkv...
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Starting release refiner [extension=release, release_name=None, release_file=None]
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] No release name for /media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E01.1080p.WEBRip.DD5.1.x264.mkv
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Refining using TVShow information.
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute series_tvdb_id changed from None to 306304
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute series_imdb_id changed from None to tt4834206
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute year changed from None to 2017
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Refining using TVEpisode information.
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute title changed from None to The Bad Beginning: Part One
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute tvdb_id changed from None to 5778150
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute release_group changed from None to FS
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] Video information cached under key medusa.subtitles:video|/media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E01.1080p.WEBRip.DD5.1.x264.mkv
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] 306304: Subtitle changes detected for this show A Series of Unfortunate Events S01E01. Current subtitles: [u'eng', u'pob', u'por', u'und']
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] 306304: Saving subtitles changes to database
2017-01-16 09:18:58 DEBUG    FINDSUBTITLES :: [49af1f0] 306304: Saving last subtitles search to database
2017-01-16 09:18:59 INFO     FINDSUBTITLES :: [49af1f0] 306304: Downloaded Portuguese subtitles for A Series of Unfortunate Events S01E01
2017-01-16 09:18:59 DEBUG    FINDSUBTITLES :: [49af1f0] Parsed '/media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E01.1080p.WEBRip.DD5.1.x264' into title: A Series of Unfortunate Events, season: 1, episode: [1], screen_size: 1080p, format: WEBRip, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, video_encoder: x264, type: episode, parsing_time: 0.458311080933, total_time: 0.458526849747, absolute_episode: [], quality: 1080p WEB-DL
2017-01-16 09:18:59 DEBUG    FINDSUBTITLES :: [49af1f0] Using cached parse result for 'A.Series.of.Unfortunate.Events.S01E01.1080p.WEBRip.DD5.1.x264'
2017-01-16 09:18:59 DEBUG    FINDSUBTITLES :: [49af1f0] Found codec for A Series of Unfortunate Events The Bad Beginning: Part One
2017-01-16 09:18:59 DEBUG    FINDSUBTITLES :: [49af1f0] 306304: Formatting pattern: %SN - S%0SE%0E - %EN -> A Series of Unfortunate Events - S01E01 - The Bad Beginning Part One
2017-01-16 09:18:59 DEBUG    FINDSUBTITLES :: [49af1f0] Notification for PLEX not enabled, skipping this notification
2017-01-16 09:18:59 DEBUG    FINDSUBTITLES :: [49af1f0] Notification for NMJ scan update not enabled, skipping this notification
2017-01-16 09:18:59 DEBUG    FINDSUBTITLES :: [49af1f0] NMA: Sending notice with details: event="Subtitle Download Finished", message="A Series of Unfortunate Events - S01E01 - The Bad Beginning Part One: Portuguese", priority=0, batch=False
2017-01-16 09:19:00 INFO     FINDSUBTITLES :: [49af1f0] NMA: Notification sent to NotifyMyAndroid
2017-01-16 09:19:00 DEBUG    FINDSUBTITLES :: [49af1f0] Checking subtitle candidates for A Series of Unfortunate Events S01E03 (A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.mkv)
2017-01-16 09:19:00 DEBUG    FINDSUBTITLES :: [49af1f0] Found cached video information under key medusa.subtitles:video|/media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.mkv
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [opensubtitles:en   ] score = 359/352 for A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [opensubtitles:pt-BR] score = 340/352 for A.Series.of.Unfortunate.Events.s01e03.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [opensubtitles:pt   ] score = 340/352 for A.Series.of.Unfortunate.Events.s01e03.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [opensubtitles:en   ] score = 340/352 for A.Series.of.Unfortunate.Events.s01e03.WebRip.x264-FS-eng.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 339/352 for A.Series.of.Unfortunate.Events.S01E03.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 339/352 for A.Series.of.Unfortunate.Events.s01e03.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [     addic7ed:en   ] score = 331/352 for FS
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [opensubtitles:pt   ] score = 331/352 for A Series of Unfortunate Events s01e03.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [     addic7ed:en   ] score = 330/352 for FS
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 309/352 for A.Series.of.Unfortunate.Events.S01E07.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 309/352 for A.Series.of.Unfortunate.Events.S01E04.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 309/352 for A.Series.of.Unfortunate.Events.S01E02.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 309/352 for A.Series.of.Unfortunate.Events.S01E06.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 309/352 for A.Series.of.Unfortunate.Events.S01E08.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 309/352 for A.Series.of.Unfortunate.Events.S01E05.WebRip.x264-FS.srt
2017-01-16 09:19:09 DEBUG    FINDSUBTITLES :: [49af1f0] [   legendastv:pt-BR] score = 309/352 for A.Series.of.Unfortunate.Events.S01E01.WebRip.x264-FS.srt
2017-01-16 09:19:13 INFO     FINDSUBTITLES :: [49af1f0] Found subtitle for A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.mkv in opensubtitles provider with language eng
2017-01-16 09:19:13 DEBUG    FINDSUBTITLES :: [49af1f0] Setting permissions for /media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.en.srt to 438 as parent directory has 511
2017-01-16 09:19:13 DEBUG    FINDSUBTITLES :: [49af1f0] history.logSubtitle opensubtitles, eng
2017-01-16 09:19:13 DEBUG    FINDSUBTITLES :: [49af1f0] Cached video information under key medusa.subtitles:video|/media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.mkv was invalidated
2017-01-16 09:19:13 DEBUG    FINDSUBTITLES :: [49af1f0] Scanning video /media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.mkv...
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Starting release refiner [extension=release, release_name=None, release_file=None]
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] No release name for /media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.mkv
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Refining using TVShow information.
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute series_tvdb_id changed from None to 306304
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute series_imdb_id changed from None to tt4834206
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute year changed from None to 2017
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Refining using TVEpisode information.
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute title changed from None to The Reptile Room: Part One
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Attribute tvdb_id changed from None to 5922016
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] Video information cached under key medusa.subtitles:video|/media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM.mkv
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] 306304: Subtitle changes detected for this show A Series of Unfortunate Events S01E03. Current subtitles: [u'eng', u'fre', u'pol', u'und']
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] 306304: Saving subtitles changes to database
2017-01-16 09:19:15 DEBUG    FINDSUBTITLES :: [49af1f0] 306304: Saving last subtitles search to database
2017-01-16 09:19:15 INFO     FINDSUBTITLES :: [49af1f0] 306304: Downloaded English subtitles for A Series of Unfortunate Events S01E03
2017-01-16 09:19:16 DEBUG    FINDSUBTITLES :: [49af1f0] Parsed '/media/extssd/Shows/A Series of Unfortunate Events/Season 01/A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM' into title: A Series of Unfortunate Events, season: 1, episode: [3], screen_size: 1080p, streaming_service: Netflix, format: WEBRip, audio_codec: AC3, audio_channels: 5.1, video_codec: h264, video_encoder: x264, release_group: ViSUM, type: episode, parsing_time: 0.47635102272, total_time: 0.476587057114, absolute_episode: [], quality: 1080p WEB-DL
2017-01-16 09:19:16 DEBUG    FINDSUBTITLES :: [49af1f0] Using cached parse result for 'A.Series.of.Unfortunate.Events.S01E03.1080p.NF.WEBRip.DD5.1.x264-ViSUM'
2017-01-16 09:19:16 DEBUG    FINDSUBTITLES :: [49af1f0] Found codec for A Series of Unfortunate Events The Reptile Room: Part One
2017-01-16 09:19:16 DEBUG    FINDSUBTITLES :: [49af1f0] 306304: Formatting pattern: %SN - S%0SE%0E - %EN -> A Series of Unfortunate Events - S01E03 - The Reptile Room Part One
2017-01-16 09:19:16 DEBUG    FINDSUBTITLES :: [49af1f0] Notification for PLEX not enabled, skipping this notification
2017-01-16 09:19:16 DEBUG    FINDSUBTITLES :: [49af1f0] Notification for NMJ scan update not enabled, skipping this notification
2017-01-16 09:19:16 DEBUG    FINDSUBTITLES :: [49af1f0] NMA: Sending notice with details: event="Subtitle Download Finished", message="A Series of Unfortunate Events - S01E03 - The Reptile Room Part One: English", priority=0, batch=False
2017-01-16 09:19:26 ERROR    FINDSUBTITLES :: [49af1f0] Unable to find subtitles for A Series of Unfortunate Events S01E03. Error: error -2 : Name or service not known
Traceback (most recent call last):
  File "/home/pi/Medusa/medusa/subtitles.py", line 1031, in run
    episode_object.download_subtitles()
  File "/home/pi/Medusa/medusa/tv.py", line 2141, in download_subtitles
    notifiers.notify_subtitle_download(self.pretty_name(), subtitle_list)
  File "/home/pi/Medusa/medusa/notifiers/__init__.py", line 82, in notify_subtitle_download
    n.notify_subtitle_download(ep_name, lang)
  File "/home/pi/Medusa/medusa/notifiers/nma.py", line 25, in notify_subtitle_download
    message=ep_name + ": " + lang)
  File "/home/pi/Medusa/medusa/notifiers/nma.py", line 62, in _sendNMA
    response = p.push(application=title, event=event, description=message, priority=nma_priority, batch_mode=batch)
  File "/home/pi/Medusa/lib/pynma/pynma.py", line 106, in push
    res = self.callapi('POST', ADD_PATH, datas)
  File "/home/pi/Medusa/lib/pynma/pynma.py", line 119, in callapi
    http_handler.request(method, path, urlencode(args), headers)
  File "/usr/lib/python2.7/httplib.py", line 1039, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1073, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 1035, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 879, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 841, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 1242, in connect
    HTTPConnection.connect(self)
  File "/usr/lib/python2.7/httplib.py", line 822, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known

# Fixed code
def download_subtitles(self, lang=None):
    """Download subtitles.

    :param lang:
    :type lang: string
    """
    if not self.is_location_valid():
        logger.debug(
            "{id}: {show} {ep} file doesn't exist, can't download subtitles",
            id=self.show.indexerid,
            show=self.show.name,
            ep=(
                episode_num(self.season, self.episode)
                or episode_num(self.season, self.episode, numbering="absolute")
            ),
        )
        return

    new_subtitles = subtitles.download_subtitles(self, lang=lang)
    if new_subtitles:
        self.subtitles = subtitles.merge_subtitles(self.subtitles, new_subtitles)

    self.subtitles_searchcount += 1 if self.subtitles_searchcount else 1
    self.subtitles_lastsearch = datetime.datetime.now().strftime(dateTimeFormat)
    logger.debug(
        "{id}: Saving last subtitles search to database", id=self.show.indexerid
    )
    self.save_to_db()

    if new_subtitles:
        subtitle_list = ", ".join(
            [subtitles.name_from_code(code) for code in new_subtitles]
        )
        logger.info(
            "{id}: Downloaded {subs} subtitles for {show} {ep}",
            id=self.show.indexerid,
            subs=subtitle_list,
            show=self.show.name,
            ep=(
                episode_num(self.season, self.episode)
                or episode_num(self.season, self.episode, numbering="absolute")
            ),
        )

        notifiers.notify_subtitle_download(self.pretty_name(), subtitle_list)
    else:
        logger.info(
            "{id}: No subtitles found for {show} {ep}",
            id=self.show.indexerid,
            show=self.show.name,
            ep=(
                episode_num(self.season, self.episode)
                or episode_num(self.season, self.episode, numbering="absolute")
            ),
        )

    return new_subtitles