Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: TypeError: Object of type bytes is not JSON serializable

### INFO
**Python Version**: `3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]`
**Operating System**: `Linux-5.4.0-40-generic-x86_64-with-glibc2.29`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.15`
**Commit**: pymedusa/Medusa@d9c31da063e6c4b4383749e19b27b13693a1b71e
**Link to Log**: https://gist.github.com/4d5bbc00a6705968144841b2e05c9a28
### ERROR
<pre>
2020-07-20 05:50:12 ERROR    SNATCHQUEUE-SNATCH-281949 :: [d9c31da] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/generic.py#L270">medusa/clients/torrent/generic.py</a>", line 270, in send_torrent
    if not self._set_torrent_ratio(result):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/transmission.py#L149">medusa/clients/torrent/transmission.py</a>", line 149, in _set_torrent_ratio
    post_data = json.dumps({
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators



Discussion:
@medariox shouldn't we make sure that hash is always stored utf-8 encoded? Else can you take a look at this?
https://github.com/pymedusa/Medusa/blob/master/medusa/classes.py#L80
https://github.com/pymedusa/Medusa/blob/master/medusa/clients/torrent/generic.py#L208
this is a bug in bencode, I think #8149 introduced this bug (my bad for not catching it) and #8187 (which might need a little refactor) should fix it

### INFO
**Python Version**: `3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]`
**Operating System**: `Linux-5.4.0-40-generic-x86_64-with-glibc2.29`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.15`
**Commit**: pymedusa/Medusa@d9c31da063e6c4b4383749e19b27b13693a1b71e
**Link to Log**: https://gist.github.com/1c2dcc78d562cccb814f3508cbac9da1
### ERROR
<pre>
2020-07-24 06:02:33 ERROR    SNATCHQUEUE-SNATCH-368862 :: [d9c31da] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/generic.py#L270">medusa/clients/torrent/generic.py</a>", line 270, in send_torrent
    if not self._set_torrent_ratio(result):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/transmission.py#L149">medusa/clients/torrent/transmission.py</a>", line 149, in _set_torrent_ratio
    post_data = json.dumps({
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators

Fixed in develop branch

### INFO
**Python Version**: `3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]`
**Operating System**: `Linux-5.4.0-40-generic-x86_64-with-glibc2.29`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.15`
**Commit**: pymedusa/Medusa@d9c31da063e6c4b4383749e19b27b13693a1b71e
**Link to Log**: https://gist.github.com/278c696a34082ed2a361d756d67463d6
### ERROR
<pre>
2020-07-24 06:56:22 ERROR    SNATCHQUEUE-SNATCH-267435 :: [d9c31da] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/generic.py#L270">medusa/clients/torrent/generic.py</a>", line 270, in send_torrent
    if not self._set_torrent_ratio(result):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/transmission.py#L149">medusa/clients/torrent/transmission.py</a>", line 149, in _set_torrent_ratio
    post_data = json.dumps({
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


### INFO
**Python Version**: `3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]`
**Operating System**: `Linux-5.4.0-40-generic-x86_64-with-glibc2.29`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.15`
**Commit**: pymedusa/Medusa@d9c31da063e6c4b4383749e19b27b13693a1b71e
**Link to Log**: https://gist.github.com/63f0d3152ca55758f669fe7e40e70b74
### ERROR
<pre>
2020-07-30 17:48:11 ERROR    SNATCHQUEUE-SNATCH-267435 :: [d9c31da] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/generic.py#L270">medusa/clients/torrent/generic.py</a>", line 270, in send_torrent
    if not self._set_torrent_ratio(result):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/transmission.py#L149">medusa/clients/torrent/transmission.py</a>", line 149, in _set_torrent_ratio
    post_data = json.dumps({
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


### INFO
**Python Version**: `3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]`
**Operating System**: `Linux-5.4.0-40-generic-x86_64-with-glibc2.29`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.15`
**Commit**: pymedusa/Medusa@d9c31da063e6c4b4383749e19b27b13693a1b71e
**Link to Log**: https://gist.github.com/e64e397ab8b6f397a02b493822d2fe25
### ERROR
<pre>
2020-07-31 19:10:10 ERROR    SNATCHQUEUE-SNATCH-380654 :: [d9c31da] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/generic.py#L270">medusa/clients/torrent/generic.py</a>", line 270, in send_torrent
    if not self._set_torrent_ratio(result):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/transmission.py#L149">medusa/clients/torrent/transmission.py</a>", line 149, in _set_torrent_ratio
    post_data = json.dumps({
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


### INFO
**Python Version**: `3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]`
**Operating System**: `Linux-5.4.0-40-generic-x86_64-with-glibc2.29`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.15`
**Commit**: pymedusa/Medusa@d9c31da063e6c4b4383749e19b27b13693a1b71e
**Link to Log**: https://gist.github.com/3b81cbce53e39561bb1724f627ecf750
### ERROR
<pre>
2020-08-02 03:33:42 ERROR    SNATCHQUEUE-SNATCH-369048 :: [d9c31da] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/generic.py#L270">medusa/clients/torrent/generic.py</a>", line 270, in send_torrent
    if not self._set_torrent_ratio(result):
  File "/opt/medusa/<a href="../blob/d9c31da063e6c4b4383749e19b27b13693a1b71e/medusa/clients/torrent/transmission.py#L149">medusa/clients/torrent/transmission.py</a>", line 149, in _set_torrent_ratio
    post_data = json.dumps({
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


### INFO
**Python Version**: `3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]`
**Operating System**: `Linux-5.4.0-47-generic-x86_64-with-glibc2.29`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.16`
**Commit**: pymedusa/Medusa@b352bb6924afcdfafce176a540d53ce405ca1312
**Link to Log**: **No Log available**
### ERROR
<pre>
2020-09-18 18:51:29 ERROR    SNATCHQUEUE-SNATCH-380654 :: [b352bb6] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/b352bb6924afcdfafce176a540d53ce405ca1312/medusa/clients/torrent/generic.py#L270">medusa/clients/torrent/generic.py</a>", line 270, in send_torrent
    if not self._set_torrent_ratio(result):
  File "/opt/medusa/<a href="../blob/b352bb6924afcdfafce176a540d53ce405ca1312/medusa/clients/torrent/transmission.py#L149">medusa/clients/torrent/transmission.py</a>", line 149, in _set_torrent_ratio
    post_data = json.dumps({
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


### INFO
**Python Version**: `3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]`
**Operating System**: `Linux-5.4.0-47-generic-x86_64-with-glibc2.29`
**Locale**: `UTF-8`
**Branch**: [master](../tree/master)
**Database**: `44.16`
**Commit**: pymedusa/Medusa@b352bb6924afcdfafce176a540d53ce405ca1312
**Link to Log**: https://gist.github.com/3736a422c982042e27f9b12b60bd298f
### ERROR
<pre>
2020-10-01 08:02:16 ERROR    SNATCHQUEUE-SNATCH-81831 :: [b352bb6] Transmission: Failed Sending Torrent
Traceback (most recent call last):
  File "/opt/medusa/<a href="../blob/b352bb6924afcdfafce176a540d53ce405ca1312/medusa/clients/torrent/generic.py#L270">medusa/clients/torrent/generic.py</a>", line 270, in send_torrent
    if not self._set_torrent_ratio(result):
  File "/opt/medusa/<a href="../blob/b352bb6924afcdfafce176a540d53ce405ca1312/medusa/clients/torrent/transmission.py#L149">medusa/clients/torrent/transmission.py</a>", line 149, in _set_torrent_ratio
    post_data = json.dumps({
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators

@sharkykh should be fixed right? 

Resolving patch:
diff --git a/medusa/clients/torrent/generic.py b/medusa/clients/torrent/generic.py
--- a/medusa/clients/torrent/generic.py
+++ b/medusa/clients/torrent/generic.py
@@ -198,7 +198,8 @@ def _get_info_hash(result):
         if result.url.startswith('magnet:'):
             result.hash = re.findall(r'urn:btih:([\w]{32,40})', result.url)[0]
             if len(result.hash) == 32:
-                result.hash = b16encode(b32decode(result.hash)).lower()
+                hash_b16 = b16encode(b32decode(result.hash)).lower()
+                result.hash = hash_b16.decode('utf-8')
         else:
 
             try:


Test patch: