Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: ProtocolError: <ProtocolError for api.broadcasthe.net: 524 Origin Time-out>
### INFO
**Python Version**: `2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2]`
**Operating System**: `Linux-4.9.28-v7+-armv7l-with-debian-8.0`
**Locale**: `UTF-8`
**Branch**: [develop](../tree/develop)
**Database**: `44.8`
**Commit**: pymedusa/Medusa@ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7
**Link to Log**: https://gist.github.com/8ec8857ffce541780792b9bbb0950bcc
### ERROR
<pre>
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/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/medusa/search/queue.py#L262">medusa/search/queue.py</a>", line 262, in run
    found_results = search_for_needed_episodes(force=self.force)
  File "/home/pi/Medusa/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/medusa/search/core.py#L444">medusa/search/core.py</a>", line 444, in search_for_needed_episodes
    cur_provider.cache.update_cache()
  File "/home/pi/Medusa/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/medusa/tv/cache.py#L196">medusa/tv/cache.py</a>", line 196, in update_cache
    data = self._get_rss_data()
  File "/home/pi/Medusa/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/medusa/tv/cache.py#L179">medusa/tv/cache.py</a>", line 179, in _get_rss_data
    return {'entries': self.provider.search(self.search_params)}
  File "/home/pi/Medusa/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/medusa/providers/torrent/json/btn.py#L94">medusa/providers/torrent/json/btn.py</a>", line 94, in search
    response = self._api_call(search_params)
  File "/home/pi/Medusa/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/medusa/providers/torrent/json/btn.py#L246">medusa/providers/torrent/json/btn.py</a>", line 246, in _api_call
    int(offset)
  File "/home/pi/Medusa/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/lib/jsonrpclib/jsonrpc.py#L288">lib/jsonrpclib/jsonrpc.py</a>", line 288, in __call__
    return self.__send(self.__name, args)
  File "/home/pi/Medusa/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/lib/jsonrpclib/jsonrpc.py#L237">lib/jsonrpclib/jsonrpc.py</a>", line 237, in _request
    response = self._run_request(request)
  File "/home/pi/Medusa/<a href="../blob/ad5ec6bb0faf81fccb3ff0ca0e70f3c21b6cb8a7/lib/jsonrpclib/jsonrpc.py#L255">lib/jsonrpclib/jsonrpc.py</a>", 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>
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


Discussion:
Really strange. The ProtocolError exception should be caught in `medusa/providers/torrent/json/btn.py`. At line 249. But for some reason it got passed it.
@p0psicles  xmlrpclib.ProtocolError ?
@p0psicles this is raised by xmlrpclib, not jsonrpclib, should we catch also xmlrpclib.ProtocolError?
If both exceptions can get up there, yes. 
This seemed like a one time issue, please reopen if it happens again.
Happens everytime BTN is offline

Resolving patch:
diff --git a/medusa/providers/torrent/json/btn.py b/medusa/providers/torrent/json/btn.py
--- a/medusa/providers/torrent/json/btn.py
+++ b/medusa/providers/torrent/json/btn.py
@@ -247,14 +247,14 @@ def _api_call(self, params=None, results_per_page=300, offset=0):
             )
             time.sleep(cpu_presets[app.CPU_PRESET])
         except jsonrpclib.jsonrpc.ProtocolError as error:
-            if error.message[1] == 'Invalid API Key':
+            if error.message == (-32001, 'Invalid API Key'):
                 log.warning('Incorrect authentication credentials.')
-            elif error.message[1] == 'Call Limit Exceeded':
+            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[1]})
+                          ' Error: {msg!r}', {'msg': error.message})
 
         except (socket.error, socket.timeout, ValueError) as error:
             log.warning('Error while accessing provider.'


Test patch: