# Buggy code
def get_url(
    url, post_data=None, params=None, headers=None, timeout=30, session=None, **kwargs
):
    """Return data retrieved from the url provider."""
    response_type = kwargs.pop("returns", "response")
    stream = kwargs.pop("stream", False)
    hooks, cookies, verify, proxies = request_defaults(kwargs)
    method = "POST" if post_data else "GET"

    try:
        req = requests.Request(
            method,
            url,
            data=post_data,
            params=params,
            hooks=hooks,
            headers=headers,
            cookies=cookies,
        )
        prepped = session.prepare_request(req)
        resp = session.send(
            prepped,
            stream=stream,
            verify=verify,
            proxies=proxies,
            timeout=timeout,
            allow_redirects=True,
        )

        if not resp.ok:
            # Try to bypass CloudFlare's anti-bot protection
            if (
                resp.status_code == 503
                and resp.headers.get("server") == "cloudflare-nginx"
            ):
                cf_prepped = prepare_cf_req(session, req)
                if cf_prepped:
                    cf_resp = session.send(
                        cf_prepped,
                        stream=stream,
                        verify=verify,
                        proxies=proxies,
                        timeout=timeout,
                        allow_redirects=True,
                    )
                    if cf_resp.ok:
                        return cf_resp

            logger.debug(
                "Requested url {url} returned status code {status}: {desc}".format(
                    url=resp.url,
                    status=resp.status_code,
                    desc=http_code_description(resp.status_code),
                )
            )

            if response_type and response_type != "response":
                return None

    except requests.exceptions.RequestException as e:
        logger.debug("Error requesting url {url}. Error: {err_msg}", url=url, err_msg=e)
        return None
    except Exception as e:
        if "ECONNRESET" in e or (hasattr(e, "errno") and e.errno == errno.ECONNRESET):
            logger.warning(
                "Connection reset by peer accessing url {url}. Error: {err_msg}".format(
                    url=url, err_msg=e
                )
            )
        else:
            logger.info(
                "Unknown exception in url {url}. Error: {err_msg}", url=url, err_msg=e
            )
            logger.debug(traceback.format_exc())
        return None

    if not response_type or response_type == "response":
        return resp
    else:
        warnings.warn(
            "Returning {0} instead of {1} will be deprecated in the near future!".format(
                response_type, "response"
            ),
            PendingDeprecationWarning,
        )
        if response_type == "json":
            try:
                return resp.json()
            except ValueError:
                return {}
        else:
            return getattr(resp, response_type, None)


# 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 get_url(
    url, post_data=None, params=None, headers=None, timeout=30, session=None, **kwargs
):
    """Return data retrieved from the url provider."""
    response_type = kwargs.pop("returns", "response")
    stream = kwargs.pop("stream", False)
    hooks, cookies, verify, proxies = request_defaults(kwargs)
    method = "POST" if post_data else "GET"

    try:
        req = requests.Request(
            method,
            url,
            data=post_data,
            params=params,
            hooks=hooks,
            headers=headers,
            cookies=cookies,
        )
        prepped = session.prepare_request(req)
        resp = session.send(
            prepped,
            stream=stream,
            verify=verify,
            proxies=proxies,
            timeout=timeout,
            allow_redirects=True,
        )

        if not resp.ok:
            # Try to bypass CloudFlare's anti-bot protection
            if (
                resp.status_code == 503
                and resp.headers.get("server") == "cloudflare-nginx"
            ):
                cf_prepped = prepare_cf_req(session, req)
                if cf_prepped:
                    cf_resp = session.send(
                        cf_prepped,
                        stream=stream,
                        verify=verify,
                        proxies=proxies,
                        timeout=timeout,
                        allow_redirects=True,
                    )
                    if cf_resp.ok:
                        return cf_resp

            logger.debug(
                "Requested url {url} returned status code {status}: {desc}".format(
                    url=resp.url,
                    status=resp.status_code,
                    desc=http_code_description(resp.status_code),
                )
            )

            if response_type and response_type != "response":
                return None

    except (requests.exceptions.RequestException, socket.gaierror) as e:
        logger.debug("Error requesting url {url}. Error: {err_msg}", url=url, err_msg=e)
        return None
    except Exception as e:
        if "ECONNRESET" in e or (hasattr(e, "errno") and e.errno == errno.ECONNRESET):
            logger.warning(
                "Connection reset by peer accessing url {url}. Error: {err_msg}".format(
                    url=url, err_msg=e
                )
            )
        else:
            logger.info(
                "Unknown exception in url {url}. Error: {err_msg}", url=url, err_msg=e
            )
            logger.debug(traceback.format_exc())
        return None

    if not response_type or response_type == "response":
        return resp
    else:
        warnings.warn(
            "Returning {0} instead of {1} will be deprecated in the near future!".format(
                response_type, "response"
            ),
            PendingDeprecationWarning,
        )
        if response_type == "json":
            try:
                return resp.json()
            except ValueError:
                return {}
        else:
            return getattr(resp, response_type, None)