# Buggy code
def _api_call(self, apikey, params=None, results_per_page=300, offset=0):
    """Call provider API."""
    server = jsonrpclib.Server(self.urls["base_url"])
    parsed_json = {}

    try:
        parsed_json = server.getTorrents(
            apikey, 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":
            logger.log("Incorrect authentication credentials.", logger.WARNING)
        elif error.message[1] == "Call Limit Exceeded":
            logger.log(
                "You have exceeded the limit of 150 calls per hour.", logger.WARNING
            )
        else:
            logger.log(
                "JSON-RPC protocol error while accessing provider. Error: {msg!r}".format(
                    msg=error.message[1]
                ),
                logger.ERROR,
            )
        return {}

    except socket.timeout:
        logger.log("Timeout while accessing provider", logger.WARNING)

    except socket.error as error:
        # Note that sometimes timeouts are thrown as socket errors
        logger.log(
            "Socket error while accessing provider. Error: {msg}".format(msg=error[1]),
            logger.WARNING,
        )

    except Exception as error:
        logger.log(
            "Unknown error while accessing provider. Error: {msg}".format(msg=error),
            logger.ERROR,
        )

    return parsed_json


# Traceback (ValueError)
2016-11-28 22:40:30 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] GET URL: https://www.torrentbytes.net/browse.php?search=Britain%27s+Greatest+Bridges+S01E01&c41=1&c38=1&c37=1&c33=1&c32=1 [Status: 200]
2016-11-28 22:40:30 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] Data returned from provider does not contain any torrents
2016-11-28 22:40:30 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] Search string: Britains Greatest Bridges S01E01
2016-11-28 22:40:44 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] GET URL: https://www.torrentbytes.net/browse.php?search=Britains+Greatest+Bridges+S01E01&c41=1&c38=1&c37=1&c33=1&c32=1 [Status: 200]
2016-11-28 22:40:44 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] Data returned from provider does not contain any torrents
2016-11-28 22:40:44 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] Search string: Britain's Greatest Bridges - S01E01
2016-11-28 22:40:59 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] GET URL: https://www.torrentbytes.net/browse.php?search=Britain%27s+Greatest+Bridges+-+S01E01&c41=1&c38=1&c37=1&c33=1&c32=1 [Status: 200]
2016-11-28 22:40:59 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] Data returned from provider does not contain any torrents
2016-11-28 22:40:59 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] Fallback season pack search initiate
2016-11-28 22:40:59 INFO     SEARCHQUEUE-MANUAL-21756 :: [TorrentBytes] :: [b88941f] Performing season pack search for Britain's Greatest Bridges
2016-11-28 22:40:59 INFO     SEARCHQUEUE-MANUAL-21756 :: [BTN] :: [b88941f] Performing episode search for Britain's Greatest Bridges
2016-11-28 22:40:59 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [BTN] :: [b88941f] Search mode: Episode
2016-11-28 22:40:59 DEBUG    SEARCHQUEUE-MANUAL-21756 :: [BTN] :: [b88941f] Search string: [{u'category': u'Episode', u'series': u'Britains.Greatest.Bridges.-', u'name': u'S01E01'}, {u'category': u'Episode', u'series': u'Britains.Greatest.Bridges.-', u'name': u'S01E01'}, {u'category': u'Episode', u'series': u'Britains.Greatest.Bridges.-', u'name': u'S01E01'}]
2016-11-28 22:40:59 ERROR    SEARCHQUEUE-MANUAL-21756 :: [BTN] :: [b88941f] Unknown error while accessing provider. Error: No JSON object could be decoded
Traceback (most recent call last):
  File "/home/home/SickRage/**********/providers/torrent/json/btn.py", line 255, in _api_call
    parsed_json = server.getTorrents(apikey, params or {}, int(results_per_page), int(offset))
  File "/home/home/SickRage/lib/jsonrpclib/jsonrpc.py", line 288, in __call__
    return self.__send(self.__name, args)
  File "/home/home/SickRage/lib/jsonrpclib/jsonrpc.py", line 237, in _request
    response = self._run_request(request)
  File "/home/home/SickRage/lib/jsonrpclib/jsonrpc.py", line 267, in _run_request
    return_obj = loads(response)
  File "/home/home/SickRage/lib/jsonrpclib/jsonrpc.py", line 543, in loads
    result = jloads(data)
  File "/home/home/SickRage/lib/jsonrpclib/jsonrpc.py", line 106, in jloads
    return json.loads(json_string)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

# Fixed code
def _api_call(self, apikey, 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(
            apikey, 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":
            logger.log("Incorrect authentication credentials.", logger.WARNING)
        elif error.message[1] == "Call Limit Exceeded":
            logger.log(
                "You have exceeded the limit of 150 calls per hour.", logger.WARNING
            )
        else:
            logger.log(
                "JSON-RPC protocol error while accessing provider. Error: {msg!r}".format(
                    msg=error.message[1]
                ),
                logger.ERROR,
            )

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