Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: TypeError: a bytes-like object is required, not 'str'

### INFO
**Python Version**: `3.5.3 (default, Sep 27 2018, 17:25:39) [GCC 6.3.0 20170516]`
**Operating System**: `Linux-4.9.0-4-amd64-x86_64-with-debian-9.6`
**Locale**: `UTF-8`
**Branch**: [feature/py3_2](../tree/feature/py3_2)
**Database**: `44.12`
**Commit**: pymedusa/Medusa@ac61f7ad628d70528cab9e8971071d79c0e90a95
**Link to Log**: https://gist.github.com/ee3a4c1fb32f4ed87dc90f4360ceba99
### ERROR
<pre>
2018-12-03 07:31:45 ERROR    SNATCHQUEUE-SNATCH-114801 :: [ac61f7a] Snatch failed! For result: [HorribleSubs].Fairy.Tail.Final.Season.-.286.[1080p].mkv
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/medusa/search/queue.py#L502">medusa/search/queue.py</a>", line 502, in run
    self.success = snatch_episode(result)
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/medusa/search/core.py#L167">medusa/search/core.py</a>", line 167, in snatch_episode
    result_downloaded = client.send_torrent(result)
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/medusa/clients/torrent/generic.py#L246">medusa/clients/torrent/generic.py</a>", line 246, in send_torrent
    if not self._get_auth():
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/medusa/clients/torrent/rtorrent_client.py#L55">medusa/clients/torrent/rtorrent_client.py</a>", line 55, in _get_auth
    self.auth = RTorrent(self.host, None, None, True)
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/lib/rtorrent/__init__.py#L87">lib/rtorrent/__init__.py</a>", line 87, in __init__
    self._verify_conn()
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/lib/rtorrent/__init__.py#L126">lib/rtorrent/__init__.py</a>", line 126, in _verify_conn
    assert 'system.client_version' in self._get_rpc_methods(
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/lib/rtorrent/__init__.py#L164">lib/rtorrent/__init__.py</a>", line 164, in _get_rpc_methods
    return(self._rpc_methods or self._update_rpc_methods())
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/lib/rtorrent/__init__.py#L154">lib/rtorrent/__init__.py</a>", line 154, in _update_rpc_methods
    self._rpc_methods = self._get_conn().system.listMethods()
  File "/usr/lib/python3.5/xmlrpc/client.py", line 1092, in __call__
    return self.__send(self.__name, args)
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/lib/rtorrent/lib/xmlrpc/scgi.py#L195">lib/rtorrent/lib/xmlrpc/scgi.py</a>", line 195, in __request
    verbose=self.__verbose
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/lib/rtorrent/lib/xmlrpc/scgi.py#L104">lib/rtorrent/lib/xmlrpc/scgi.py</a>", line 104, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/opt/medusa/<a href="../blob/ac61f7ad628d70528cab9e8971071d79c0e90a95/lib/rtorrent/lib/xmlrpc/scgi.py#L135">lib/rtorrent/lib/xmlrpc/scgi.py</a>", line 135, in single_request
    sock.send(request_body)
TypeError: a bytes-like object is required, not 'str'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators



Discussion:
#4982 
Fixed

### INFO
**Python Version**: `3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]`
**Operating System**: `Windows-7-6.1.7601-SP1`
**Locale**: `cp1255`
**Branch**: [develop](../tree/develop)
**Database**: `44.12`
**Commit**: pymedusa/Medusa@dd35d00cdf98119e5ca370e28e4278ec06f64dc1
**Link to Log**: https://gist.github.com/2e38cd3afd879f74e9876dfc04a32ad0
### ERROR
<pre>
2018-12-27 01:14:03 ERROR    TORNADO :: [dd35d00] API :: TypeError("a bytes-like object is required, not 'str'")
Traceback (most recent call last):
  File "C:\Medusa\<a href="../blob/dd35d00cdf98119e5ca370e28e4278ec06f64dc1/medusa/server/api/v1/core.py#L142">medusa\server\api\v1\core.py</a>", line 142, in get
    out_dict = _call_dispatcher(args, kwargs)
  File "C:\Medusa\<a href="../blob/dd35d00cdf98119e5ca370e28e4278ec06f64dc1/medusa/server/api/v1/core.py#L197">medusa\server\api\v1\core.py</a>", line 197, in call_dispatcher
    commands = commands.split('|')
TypeError: a bytes-like object is required, not 'str'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators

Fixed with https://github.com/pymedusa/Medusa/pull/5926

### INFO
**Python Version**: `3.7.2 (default, Feb 12 2019, 08:15:36) [Clang 10.0.0 (clang-1000.11.45.5)]`
**Operating System**: `Darwin-18.2.0-x86_64-i386-64bit`
**Locale**: `unknown`
**Branch**: [develop](../tree/develop)
**Database**: `44.14`
**Commit**: pymedusa/Medusa@d588a2c92bb007d4346e5f46811f896775efb3be
**Link to Log**: https://gist.github.com/9ea813c284721c1c691562abc005054b
### ERROR
<pre>
2019-02-28 15:37:38 ERROR    SNATCHQUEUE-SNATCH-258541 :: [d588a2c] qbittorrent: Unknown exception raised when sending torrent to qbittorrent: a bytes-like object is required, not 'str'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/medusa/clients/torrent/generic.py#L82">medusa/clients/torrent/generic.py</a>", line 82, in _request
    cookies=cookies, timeout=120, verify=False)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/medusa/session/core.py#L103">medusa/session/core.py</a>", line 103, in request
    timeout=timeout, verify=ssl_cert, **kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/ext/requests/sessions.py#L533">ext/requests/sessions.py</a>", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/ext/requests/sessions.py#L653">ext/requests/sessions.py</a>", line 653, in send
    r = dispatch_hook('response', hooks, r, **kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/ext/requests/hooks.py#L31">ext/requests/hooks.py</a>", line 31, in dispatch_hook
    _hook_data = hook(hook_data, **kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/d588a2c92bb007d4346e5f46811f896775efb3be/medusa/session/hooks.py#L32">medusa/session/hooks.py</a>", line 32, in log_url
    body = request.body[1:99].replace('\n', ' ') + '...'
TypeError: a bytes-like object is required, not 'str'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


### INFO
**Python Version**: `3.7.2 (default, Feb 12 2019, 08:15:36) [Clang 10.0.0 (clang-1000.11.45.5)]`
**Operating System**: `Darwin-18.2.0-x86_64-i386-64bit`
**Locale**: `unknown`
**Branch**: [develop](../tree/develop)
**Database**: `44.14`
**Commit**: pymedusa/Medusa@a79d1b7a7bf4fe35ed1c9102a3ec729e605b3af5
**Link to Log**: https://gist.github.com/1cd847ef7ef8987e77a11e2a82e76566
### ERROR
<pre>
2019-03-01 11:02:09 ERROR    SNATCHQUEUE-SNATCH-355966 :: [a79d1b7] qbittorrent: Unknown exception raised when sending torrent to qbittorrent: a bytes-like object is required, not 'str'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/a79d1b7a7bf4fe35ed1c9102a3ec729e605b3af5/medusa/clients/torrent/generic.py#L82">medusa/clients/torrent/generic.py</a>", line 82, in _request
    cookies=cookies, timeout=120, verify=False)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/a79d1b7a7bf4fe35ed1c9102a3ec729e605b3af5/medusa/session/core.py#L103">medusa/session/core.py</a>", line 103, in request
    timeout=timeout, verify=ssl_cert, **kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/a79d1b7a7bf4fe35ed1c9102a3ec729e605b3af5/ext/requests/sessions.py#L533">ext/requests/sessions.py</a>", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/a79d1b7a7bf4fe35ed1c9102a3ec729e605b3af5/ext/requests/sessions.py#L653">ext/requests/sessions.py</a>", line 653, in send
    r = dispatch_hook('response', hooks, r, **kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/a79d1b7a7bf4fe35ed1c9102a3ec729e605b3af5/ext/requests/hooks.py#L31">ext/requests/hooks.py</a>", line 31, in dispatch_hook
    _hook_data = hook(hook_data, **kwargs)
  File "/Users/ovidijus/Apps/Medusa/<a href="../blob/a79d1b7a7bf4fe35ed1c9102a3ec729e605b3af5/medusa/session/hooks.py#L32">medusa/session/hooks.py</a>", line 32, in log_url
    body = request.body[1:99].replace('\n', ' ') + '...'
TypeError: a bytes-like object is required, not 'str'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


Resolving patch:
diff --git a/medusa/session/hooks.py b/medusa/session/hooks.py
--- a/medusa/session/hooks.py
+++ b/medusa/session/hooks.py
@@ -6,7 +6,7 @@
 
 from medusa.logger.adapters.style import BraceAdapter
 
-from six import text_type
+from six import ensure_text
 
 log = BraceAdapter(logging.getLogger(__name__))
 log.logger.addHandler(logging.NullHandler())
@@ -26,28 +26,12 @@ def log_url(response, **kwargs):
 
     if request.method.upper() == 'POST':
         if request.body:
+            text_body = ensure_text(request.body)
             if 'multipart/form-data' not in request.headers.get('content-type', ''):
-                body = request.body
+                body = text_body
+            elif len(text_body) > 99:
+                body = text_body[0:99].replace('\n', ' ') + '...'
             else:
-                body = request.body[1:99].replace('\n', ' ') + '...'
-        else:
-            body = ''
+                body = text_body.replace('\n', ' ')
 
-        # try to log post data using various codecs to decode
-        if isinstance(body, text_type):
             log.debug('With post data: {0}', body)
-            return
-
-        codecs = ('utf-8', 'latin1', 'cp1252')
-        for codec in codecs:
-            try:
-                data = body.decode(codec)
-            except UnicodeError as error:
-                log.debug('Failed to decode post data as {codec}: {msg}',
-                          {'codec': codec, 'msg': error})
-            else:
-                log.debug('With post data: {0}', data)
-                break
-        else:
-            log.warning('Failed to decode post data with {codecs}',
-                        {'codecs': codecs})


Test patch: