Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: ProtocolError: (u'api.broadcasthe.net/', 524, 'Origin Time-out', <httplib.HTTPMessage instance at 0x65effda0>)

### INFO
**Python Version**: `2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2]`
**Operating System**: `Linux-4.9.35-v7+-armv7l-with-debian-8.0`
**Locale**: `UTF-8`
**Branch**: [develop](../tree/develop)
**Database**: `44.9`
**Commit**: pymedusa/Medusa@f1c04d4bbeec508f8e8d59648f9c30858fa020a9
**Link to Log**: https://gist.github.com/447b35a960c9507d72e1fa46f021ee07
### ERROR
<pre>
2018-05-20 18:06:29 ERROR    SEARCHQUEUE-DAILY-SEARCH :: [BTN] :: [f1c04d4] JSON-RPC protocol error while accessing provider. Error: ''
Traceback (most recent call last):
  File "/home/pi/Medusa/<a href="../blob/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/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/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/ext/jsonrpclib/jsonrpc.py#L650">ext/jsonrpclib/jsonrpc.py</a>", line 650, in __call__
    return self.__send(self.__name, args)
  File "/home/pi/Medusa/<a href="../blob/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/ext/jsonrpclib/jsonrpc.py#L531">ext/jsonrpclib/jsonrpc.py</a>", line 531, in _request
    response = self._run_request(request)
  File "/home/pi/Medusa/<a href="../blob/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/ext/jsonrpclib/jsonrpc.py#L564">ext/jsonrpclib/jsonrpc.py</a>", line 564, 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 "/home/pi/Medusa/<a href="../blob/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/ext/jsonrpclib/jsonrpc.py#L387">ext/jsonrpclib/jsonrpc.py</a>", line 387, in single_request
    response.msg)
ProtocolError: (u'api.broadcasthe.net/', 524, 'Origin Time-out', <httplib.HTTPMessage instance at 0x65effda0>)
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators

[APP SUBMITTED]: ProtocolError: (u'api.broadcasthe.net/', 524, 'Origin Time-out', <httplib.HTTPMessage instance at 0x65effda0>)

### INFO
**Python Version**: `2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2]`
**Operating System**: `Linux-4.9.35-v7+-armv7l-with-debian-8.0`
**Locale**: `UTF-8`
**Branch**: [develop](../tree/develop)
**Database**: `44.9`
**Commit**: pymedusa/Medusa@f1c04d4bbeec508f8e8d59648f9c30858fa020a9
**Link to Log**: https://gist.github.com/447b35a960c9507d72e1fa46f021ee07
### ERROR
<pre>
2018-05-20 18:06:29 ERROR    SEARCHQUEUE-DAILY-SEARCH :: [BTN] :: [f1c04d4] JSON-RPC protocol error while accessing provider. Error: ''
Traceback (most recent call last):
  File "/home/pi/Medusa/<a href="../blob/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/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/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/ext/jsonrpclib/jsonrpc.py#L650">ext/jsonrpclib/jsonrpc.py</a>", line 650, in __call__
    return self.__send(self.__name, args)
  File "/home/pi/Medusa/<a href="../blob/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/ext/jsonrpclib/jsonrpc.py#L531">ext/jsonrpclib/jsonrpc.py</a>", line 531, in _request
    response = self._run_request(request)
  File "/home/pi/Medusa/<a href="../blob/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/ext/jsonrpclib/jsonrpc.py#L564">ext/jsonrpclib/jsonrpc.py</a>", line 564, 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 "/home/pi/Medusa/<a href="../blob/f1c04d4bbeec508f8e8d59648f9c30858fa020a9/ext/jsonrpclib/jsonrpc.py#L387">ext/jsonrpclib/jsonrpc.py</a>", line 387, in single_request
    response.msg)
ProtocolError: (u'api.broadcasthe.net/', 524, 'Origin Time-out', <httplib.HTTPMessage instance at 0x65effda0>)
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators



Discussion:
Fixed with https://github.com/pymedusa/Medusa/commit/11f3b5097c4dbfa0ac814b24fe8ce07546afcec5

### INFO
**Python Version**: `2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2]`
**Operating System**: `Linux-4.9.35-v7+-armv7l-with-debian-8.0`
**Locale**: `UTF-8`
**Branch**: [bugfix/imdb-none](../tree/bugfix/imdb-none)
**Database**: `44.12`
**Commit**: pymedusa/Medusa@4e21883bbedad8c4346c14b063ade74d1ac31893
**Link to Log**: https://gist.github.com/52265d085f9dcb7a4d203201ef191e5c
### ERROR
<pre>
2018-08-29 01:56:47 ERROR    SEARCHQUEUE-DAILY-SEARCH :: [BTN] :: [4e21883] JSON-RPC protocol error while accessing provider. Error: u'api.broadcasthe.net/'
Traceback (most recent call last):
  File "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/medusa/providers/torrent/json/btn.py#L251">medusa/providers/torrent/json/btn.py</a>", line 251, in _api_call
    int(offset)
  File "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/ext/jsonrpclib/jsonrpc.py#L650">ext/jsonrpclib/jsonrpc.py</a>", line 650, in __call__
    return self.__send(self.__name, args)
  File "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/ext/jsonrpclib/jsonrpc.py#L531">ext/jsonrpclib/jsonrpc.py</a>", line 531, in _request
    response = self._run_request(request)
  File "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/ext/jsonrpclib/jsonrpc.py#L564">ext/jsonrpclib/jsonrpc.py</a>", line 564, 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 "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/ext/jsonrpclib/jsonrpc.py#L387">ext/jsonrpclib/jsonrpc.py</a>", line 387, in single_request
    response.msg)
ProtocolError: (u'api.broadcasthe.net/', 524, 'Timeout', <httplib.HTTPMessage instance at 0x5aee8580>)
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators

@sharkykh 
Fixed with https://github.com/pymedusa/Medusa/commit/11f3b5097c4dbfa0ac814b24fe8ce07546afcec5

### INFO
**Python Version**: `2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2]`
**Operating System**: `Linux-4.9.35-v7+-armv7l-with-debian-8.0`
**Locale**: `UTF-8`
**Branch**: [bugfix/imdb-none](../tree/bugfix/imdb-none)
**Database**: `44.12`
**Commit**: pymedusa/Medusa@4e21883bbedad8c4346c14b063ade74d1ac31893
**Link to Log**: https://gist.github.com/52265d085f9dcb7a4d203201ef191e5c
### ERROR
<pre>
2018-08-29 01:56:47 ERROR    SEARCHQUEUE-DAILY-SEARCH :: [BTN] :: [4e21883] JSON-RPC protocol error while accessing provider. Error: u'api.broadcasthe.net/'
Traceback (most recent call last):
  File "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/medusa/providers/torrent/json/btn.py#L251">medusa/providers/torrent/json/btn.py</a>", line 251, in _api_call
    int(offset)
  File "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/ext/jsonrpclib/jsonrpc.py#L650">ext/jsonrpclib/jsonrpc.py</a>", line 650, in __call__
    return self.__send(self.__name, args)
  File "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/ext/jsonrpclib/jsonrpc.py#L531">ext/jsonrpclib/jsonrpc.py</a>", line 531, in _request
    response = self._run_request(request)
  File "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/ext/jsonrpclib/jsonrpc.py#L564">ext/jsonrpclib/jsonrpc.py</a>", line 564, 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 "/home/pi/Medusa/<a href="../blob/4e21883bbedad8c4346c14b063ade74d1ac31893/ext/jsonrpclib/jsonrpc.py#L387">ext/jsonrpclib/jsonrpc.py</a>", line 387, in single_request
    response.msg)
ProtocolError: (u'api.broadcasthe.net/', 524, 'Timeout', <httplib.HTTPMessage instance at 0x5aee8580>)
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators

@sharkykh 

Resolving patch:
diff --git a/medusa/clients/torrent/utorrent_client.py b/medusa/clients/torrent/utorrent_client.py
--- a/medusa/clients/torrent/utorrent_client.py
+++ b/medusa/clients/torrent/utorrent_client.py
@@ -14,6 +14,7 @@
 from medusa.logger.adapters.style import BraceAdapter
 
 from requests.compat import urljoin
+from requests.exceptions import RequestException
 
 log = BraceAdapter(logging.getLogger(__name__))
 log.logger.addHandler(logging.NullHandler())
@@ -73,11 +74,18 @@ def _request(self, method='get', params=None, data=None, files=None, cookies=Non
         return super(UTorrentAPI, self)._request(method=method, params=ordered_params, data=data, files=files)
 
     def _get_auth(self):
-        self.response = self.session.get(urljoin(self.url, 'token.html'), verify=False)
+        try:
+            self.response = self.session.get(urljoin(self.url, 'token.html'), verify=False)
+        except RequestException as error:
+            log.warning('Unable to authenticate with uTorrent client: {0!r}', error)
+            return None
+
         if not self.response.status_code == 404:
             self.auth = re.findall('<div.*?>(.*?)</', self.response.text)[0]
             return self.auth
 
+        return None
+
     def _add_torrent_uri(self, result):
         """Send an 'add-url' download request to uTorrent when search provider is using a magnet link."""
         # Set proper subfolder as download destination for uTorrent torrent
diff --git a/medusa/providers/torrent/html/yggtorrent.py b/medusa/providers/torrent/html/yggtorrent.py
--- a/medusa/providers/torrent/html/yggtorrent.py
+++ b/medusa/providers/torrent/html/yggtorrent.py
@@ -36,7 +36,7 @@ def __init__(self):
         self.password = None
 
         # URLs
-        self.url = 'https://ww4.yggtorrent.is'
+        self.url = 'https://www.yggtorrent.is'
         self.urls = {
             'auth': urljoin(self.url, 'user/ajax_usermenu'),
             'login': urljoin(self.url, 'user/login'),
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
@@ -31,6 +31,29 @@
 # https://web.archive.org/web/20160316073644/http://btnapps.net/docs.php
 # https://web.archive.org/web/20160425205926/http://btnapps.net/apigen/class-btnapi.html
 
+def normalize_protocol_error(error):
+    """
+    Convert ProtocolError exception to a comparable code and message tuple.
+
+    :param error: Exception instance
+    :return: Tuple containing (code, message)
+    """
+    try:
+        # error.args = ('api.broadcasthe.net', 524, 'Timeout', )
+        code = error.args[1]
+        message = error.args[2]
+    except IndexError:
+        # error.args = ((-32001, 'Invalid API Key', ), )
+        try:
+            code, message = error.args[0]
+        except ValueError:
+            # error.args = ('reason', )
+            code = None
+            message = error.args[0]
+
+    return code, message
+
+
 class BTNProvider(TorrentProvider):
     """BTN Torrent provider."""
 
@@ -252,17 +275,17 @@ 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:
-            message = error.args[0]
-            if message == (-32001, 'Invalid API Key'):
+            code, message = normalize_protocol_error(error)
+            if (code, message) == (-32001, 'Invalid API Key'):
                 log.warning('Incorrect authentication credentials.')
-            elif message == (-32002, 'Call Limit Exceeded'):
+            elif (code, message) == (-32002, 'Call Limit Exceeded'):
                 log.warning('You have exceeded the limit of 150 calls per hour.')
-            elif isinstance(message, tuple) and message[1] in (500, 524, ):
+            elif code in (500, 524):
                 log.warning('Provider is currently unavailable. Error: {code} {text}',
-                            {'code': message[1], 'text': message[2]})
+                            {'code': code, 'text': message})
             else:
                 log.error('JSON-RPC protocol error while accessing provider. Error: {msg!r}',
-                          {'msg': message})
+                          {'msg': error.args})
 
         except (socket.error, ValueError) as error:
             log.warning('Error while accessing provider. Error: {msg!r}', {'msg': error})


Test patch: