# Buggy code
def _api_call(self, params=None, results_per_page=300, offset=0):
    """Call provider API."""
    parsed_json = {}

    try:
        server = jsonrpclib.Server(self.urls["base_url"])
        parsed_json = server.getTorrents(
            self.api_key, params or {}, int(results_per_page), int(offset)
        )
        time.sleep(cpu_presets[app.CPU_PRESET])
    except jsonrpclib.jsonrpc.ProtocolError as error:
        if error.message[1] == "Invalid API Key":
            log.warning("Incorrect authentication credentials.")
        elif error.message[1] == "Call Limit Exceeded":
            log.warning("You have exceeded the limit of 150 calls per hour.")
        else:
            log.error(
                "JSON-RPC protocol error while accessing provider. Error: {msg!r}",
                {"msg": error.message[1]},
            )

    except (socket.error, socket.timeout, ValueError) as error:
        log.warning("Error while accessing provider. Error: {msg}", {"msg": error})
    return parsed_json


# Traceback (ProtocolError)
2017-07-13 21:11:12 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [AlphaRatio] :: [ad5ec6b] Unable to match Big.Pacific.S01E04.1080p.WEB.h264-CookieMonster to a series in your database. Parser result: title: Big Pacific, season: 1, episode: [4], screen_size: 1080p, format: WEB-DL, video_codec: h264, release_group: CookieMonster, type: episode, parsing_time: 0.580655097961, total_time: 1.39559602737, absolute_episode: [], quality: 1080p WEB-DL
2017-07-13 21:11:14 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [AlphaRatio] :: [ad5ec6b] Unable to match Big.Pacific.S01E04.WEB.h264-CookieMonster to a series in your database. Parser result: title: Big Pacific, season: 1, episode: [4], format: WEB-DL, video_codec: h264, release_group: CookieMonster, type: episode, parsing_time: 1.17723989487, total_time: 1.72887206078, absolute_episode: [], quality: SDTV
2017-07-13 21:11:14 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [AlphaRatio] :: [ad5ec6b] Hit old cached items, not parsing any more for: alpharatio
2017-07-13 21:11:14 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HoundDawgs] :: [ad5ec6b] Updating providers cache
2017-07-13 21:11:14 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HoundDawgs] :: [ad5ec6b] Search mode: RSS
2017-07-13 21:11:15 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HoundDawgs] :: [ad5ec6b] GET URL: https://hounddawgs.org/torrents.php?searchstr=&filter_cat%5B57%5D=1&filter_cat%5B85%5D=1&filter_cat%5B74%5D=1&filter_cat%5B58%5D=1&order_by=s3&filter_cat%5B93%5D=1&searchimdb=&userid=&searchtags=&filter_cat%5B92%5D=1&order_way=desc&type= [Status: 302]
2017-07-13 21:11:15 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HoundDawgs] :: [ad5ec6b] User-Agent: python-requests/2.10.0
2017-07-13 21:11:15 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HoundDawgs] :: [ad5ec6b] Error requesting url https://hounddawgs.org/torrents.php. Error: ('Connection aborted.', BadStatusLine("''",))
2017-07-13 21:11:15 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HoundDawgs] :: [ad5ec6b] No data returned from provider
2017-07-13 21:11:15 INFO     SEARCHQUEUE-DAILY-SEARCH :: [HoundDawgs] :: [ad5ec6b] Removing cache entries older than 15 days from hounddawgs
2017-07-13 21:11:16 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Updating providers cache
2017-07-13 21:11:16 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Search mode: RSS
2017-07-13 21:11:16 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] GET URL: https://hd4free.xyz/searchapi.php?tv=true&username=**********&apikey=**********
2017-07-13 21:11:16 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] User-Agent: python-requests/2.10.0
2017-07-13 21:11:16 INFO     SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Removing cache entries older than 15 days from hd4free
2017-07-13 21:11:17 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Younger.S04E03.Forged.in.Fire.720p.AMZN.WEB-DL.DDP2.0.H.264-KiNGS to a series in your database. Parser result: title: Younger, season: 4, episode: [3], episode_title: Forged in Fire, screen_size: 720p, streaming_service: Amazon Prime, format: WEB-DL, audio_codec: EAC3, audio_channels: 2.0, video_codec: h264, release_group: KiNGS, type: episode, parsing_time: 0.602144956589, total_time: 1.16483283043, absolute_episode: [], quality: 720p WEB-DL
2017-07-13 21:11:18 DEBUG    Thread-20 :: [ad5ec6b] Broken providers already updated in the last hour
2017-07-13 21:11:19 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Younger.S04E01.Post.Truth.720p.AMZN.WEB-DL.DDP2.0.H.264-KiNGS to a series in your database. Parser result: title: Younger, season: 4, episode: [1], episode_title: Post Truth, screen_size: 720p, streaming_service: Amazon Prime, format: WEB-DL, audio_codec: EAC3, audio_channels: 2.0, video_codec: h264, release_group: KiNGS, type: episode, parsing_time: 0.407486200333, total_time: 1.7918279171, absolute_episode: [], quality: 720p WEB-DL
2017-07-13 21:11:21 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Storage.Wars.Northern.Treasures.S01E25.720p.WEB.h264-CookieMonster to a series in your database. Parser result: title: Storage Wars Northern Treasures, season: 1, episode: [25], screen_size: 720p, format: WEB-DL, video_codec: h264, release_group: CookieMonster, type: episode, parsing_time: 0.921990871429, total_time: 1.97401809692, absolute_episode: [], quality: 720p WEB-DL
2017-07-13 21:11:23 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Storage.Wars.Northern.Treasures.S01E24.720p.WEB.h264-CookieMonster to a series in your database. Parser result: title: Storage Wars Northern Treasures, season: 1, episode: [24], screen_size: 720p, format: WEB-DL, video_codec: h264, release_group: CookieMonster, type: episode, parsing_time: 0.776463031769, total_time: 2.11796307564, absolute_episode: [], quality: 720p WEB-DL
2017-07-13 21:11:25 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Younger.S04E02.Gettin.Hygge.With.It.1080p.AMZN.WEB-DL.DDP2.0.H.264-KiNGS to a series in your database. Parser result: title: Younger, season: 4, episode: [2], episode_title: Gettin Hygge With, language: it, screen_size: 1080p, streaming_service: Amazon Prime, format: WEB-DL, audio_codec: EAC3, audio_channels: 2.0, video_codec: h264, release_group: KiNGS, type: episode, parsing_time: 0.5841588974, total_time: 1.36835694313, absolute_episode: [], quality: 1080p WEB-DL
2017-07-13 21:11:26 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Younger.S04E01.Post.Truth.1080p.AMZN.WEB-DL.DDP2.0.H.264-KiNGS to a series in your database. Parser result: title: Younger, season: 4, episode: [1], episode_title: Post Truth, screen_size: 1080p, streaming_service: Amazon Prime, format: WEB-DL, audio_codec: EAC3, audio_channels: 2.0, video_codec: h264, release_group: KiNGS, type: episode, parsing_time: 0.40128493309, total_time: 0.92666387558, absolute_episode: [], quality: 1080p WEB-DL
2017-07-13 21:11:27 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Younger.S04E03.Forged.in.Fire.1080p.AMZN.WEB-DL.DDP2.0.H.264-KiNGS to a series in your database. Parser result: title: Younger, season: 4, episode: [3], episode_title: Forged in Fire, screen_size: 1080p, streaming_service: Amazon Prime, format: WEB-DL, audio_codec: EAC3, audio_channels: 2.0, video_codec: h264, release_group: KiNGS, type: episode, parsing_time: 0.344965934753, total_time: 0.819895982742, absolute_episode: [], quality: 1080p WEB-DL
2017-07-13 21:11:27 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Younger.S04E02.Gettin.Hygge.With.It.720p.AMZN.WEB-DL.DDP2.0.H.264-KiNGS to a series in your database. Parser result: title: Younger, season: 4, episode: [2], episode_title: Gettin Hygge With, language: it, screen_size: 720p, streaming_service: Amazon Prime, format: WEB-DL, audio_codec: EAC3, audio_channels: 2.0, video_codec: h264, release_group: KiNGS, type: episode, parsing_time: 0.323064088821, total_time: 0.91934800148, absolute_episode: [], quality: 720p WEB-DL
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Unable to match Storage.Wars.Northern.Treasures.S01E23.720p.WEB.h264-CookieMonster to a series in your database. Parser result: title: Storage Wars Northern Treasures, season: 1, episode: [23], screen_size: 720p, format: WEB-DL, video_codec: h264, release_group: CookieMonster, type: episode, parsing_time: 0.439090967178, total_time: 1.28332304955, absolute_episode: [], quality: 720p WEB-DL
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [HD4Free] :: [ad5ec6b] Hit old cached items, not parsing any more for: hd4free
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [ad5ec6b] Updating providers cache
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [ad5ec6b] Search mode: RSS
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [ad5ec6b] GET URL: https://sceneaccess.eu/all?search=&method=1&c11=11&c17=17&c26=26&c27=27&c33=33&c34=34&c44=44&c45=45 [Status: 302]
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [ad5ec6b] User-Agent: python-requests/2.10.0
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [ad5ec6b] GET URL: https://sceneaccess.eu/login [Status: 200]
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [ad5ec6b] User-Agent: python-requests/2.10.0
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [ad5ec6b] Data returned from provider does not contain any torrents
2017-07-13 21:11:29 INFO     SEARCHQUEUE-DAILY-SEARCH :: [SceneAccess] :: [ad5ec6b] Removing cache entries older than 15 days from sceneaccess
2017-07-13 21:11:29 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Updating providers cache
2017-07-13 21:11:30 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Search mode: RSS
2017-07-13 21:11:30 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] GET URL: https://danishbits.org/couchpotato.php?search=.&latest=true&user=**********&passkey=********** [Status: 200]
2017-07-13 21:11:30 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] User-Agent: python-requests/2.10.0
2017-07-13 21:11:30 INFO     SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Removing cache entries older than 15 days from danishbits
2017-07-13 21:11:32 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Unable to match Vi.Elsker.Biler.S03E04.DANiSH.HDTV.x264-RCDiVX to a series in your database. Parser result: title: Vi Elsker Biler, season: 3, episode: [4], episode_title: DANiSH, format: HDTV, video_codec: h264, video_encoder: x264, release_group: RCDiVX, type: episode, parsing_time: 0.455077886581, total_time: 1.47522807121, absolute_episode: [], quality: SDTV
2017-07-13 21:11:33 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Unable to match Gutsful.S01E09.HDTV.x264-FiHTV to a series in your database. Parser result: title: Gutsful, season: 1, episode: [9], format: HDTV, video_codec: h264, video_encoder: x264, release_group: FiHTV, type: episode, parsing_time: 0.390126943588, total_time: 1.42503809929, absolute_episode: [], quality: SDTV
2017-07-13 21:11:36 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Unable to match Vi.Elsker.Biler.S03E04.DANiSH.720p.HDTV.x264-DiViSiON to a series in your database. Parser result: title: Vi Elsker Biler, season: 3, episode: [4], episode_title: DANiSH, screen_size: 720p, format: HDTV, video_codec: h264, video_encoder: x264, release_group: DiViSiON, type: episode, parsing_time: 0.607612133026, total_time: 2.74137496948, absolute_episode: [], quality: 720p HDTV
2017-07-13 21:11:39 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Unable to match Gutsful.S01E09.720p.HDTV.x264-FiHTV to a series in your database. Parser result: title: Gutsful, season: 1, episode: [9], screen_size: 720p, format: HDTV, video_codec: h264, video_encoder: x264, release_group: FiHTV, type: episode, parsing_time: 1.22523903847, total_time: 2.83811092377, absolute_episode: [], quality: 720p HDTV
2017-07-13 21:11:41 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Unable to match Pranksters.Paradise.S01E03.DANiSH.720p.HDTV.x264-DiViSiON to a series in your database. Parser result: title: Pranksters Paradise, season: 1, episode: [3], episode_title: DANiSH, screen_size: 720p, format: HDTV, video_codec: h264, video_encoder: x264, release_group: DiViSiON, type: episode, parsing_time: 0.646375179291, total_time: 1.97697496414, absolute_episode: [], quality: 720p HDTV
2017-07-13 21:11:42 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Unable to match ElectroSluts.17.07.13.Arabelle.Raphael.And.Ela.Darling.XXX.720p.MP4-KTR to a series in your database. Parser result: title: ElectroSluts, date: 2013-07-17, episode_title: Arabelle Raphael And Ela Darling, other: XXX, screen_size: 720p, container: mp4, release_group: KTR, type: episode, parsing_time: 0.661869049072, total_time: 1.15857791901, absolute_episode: [], quality: Unknown, episode: [], season: None
2017-07-13 21:11:42 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [Danishbits] :: [ad5ec6b] Hit old cached items, not parsing any more for: danishbits
2017-07-13 21:11:42 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [BTN] :: [ad5ec6b] Updating providers cache
2017-07-13 21:11:42 DEBUG    SEARCHQUEUE-DAILY-SEARCH :: [BTN] :: [ad5ec6b] Search mode: RSS
2017-07-13 21:11:57 ERROR    SEARCHQUEUE-DAILY-SEARCH :: [BTN] :: [ad5ec6b] DailySearchQueueItem Exception, error: <ProtocolError for api.broadcasthe.net: 524 Origin Time-out>
Traceback (most recent call last):
  File "/home/pi/Medusa/medusa/search/queue.py", line 262, in run
    found_results = search_for_needed_episodes(force=self.force)
  File "/home/pi/Medusa/medusa/search/core.py", line 444, in search_for_needed_episodes
    cur_provider.cache.update_cache()
  File "/home/pi/Medusa/medusa/tv/cache.py", line 196, in update_cache
    data = self._get_rss_data()
  File "/home/pi/Medusa/medusa/tv/cache.py", line 179, in _get_rss_data
    return {'entries': self.provider.search(self.search_params)}
  File "/home/pi/Medusa/medusa/providers/torrent/json/btn.py", line 94, in search
    response = self._api_call(search_params)
  File "/home/pi/Medusa/medusa/providers/torrent/json/btn.py", line 246, in _api_call
    int(offset)
  File "/home/pi/Medusa/lib/jsonrpclib/jsonrpc.py", line 288, in __call__
    return self.__send(self.__name, args)
  File "/home/pi/Medusa/lib/jsonrpclib/jsonrpc.py", line 237, in _request
    response = self._run_request(request)
  File "/home/pi/Medusa/lib/jsonrpclib/jsonrpc.py", line 255, in _run_request
    verbose=self.__verbose
  File "/usr/lib/python2.7/xmlrpclib.py", line 1273, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1321, in single_request
    response.msg,
ProtocolError: <ProtocolError for api.broadcasthe.net: 524 Origin Time-out>

# Fixed code
def _api_call(self, params=None, results_per_page=300, offset=0):
    """Call provider API."""
    parsed_json = {}

    try:
        server = jsonrpclib.Server(self.urls["base_url"])
        parsed_json = server.getTorrents(
            self.api_key, params or {}, int(results_per_page), int(offset)
        )
        time.sleep(cpu_presets[app.CPU_PRESET])
    except jsonrpclib.jsonrpc.ProtocolError as error:
        if error.message == (-32001, "Invalid API Key"):
            log.warning("Incorrect authentication credentials.")
        elif error.message == (-32002, "Call Limit Exceeded"):
            log.warning("You have exceeded the limit of 150 calls per hour.")
        else:
            log.error(
                "JSON-RPC protocol error while accessing provider. Error: {msg!r}",
                {"msg": error.message},
            )

    except (socket.error, socket.timeout, ValueError) as error:
        log.warning("Error while accessing provider. Error: {msg}", {"msg": error})
    return parsed_json