From 6764783aa72d62b9ce4622cbf9326b0433ab906c Mon Sep 17 00:00:00 2001 From: hypsometric <25771958+hypsometric@users.noreply.github.com> Date: Thu, 17 Apr 2025 12:24:34 +0200 Subject: [PATCH] Fix deluge torrent upload Fix torrent file encoding for deluge - received content is bytes - we need to convert this content into base64 - we then need to convert base64 bytes into base64 string to include in the json data binary Fixes #3362 --- headphones/deluge.py | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/headphones/deluge.py b/headphones/deluge.py index 01c6fbd8..27a926a6 100644 --- a/headphones/deluge.py +++ b/headphones/deluge.py @@ -484,34 +484,14 @@ def _add_torrent_file(result): # content is torrent file contents that needs to be encoded to base64 post_data = json.dumps({"method": "core.add_torrent_file", "params": [result['name'] + '.torrent', - b64encode(result['content'].encode('utf8')), + b64encode(result['content']).decode(), options], "id": 2}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth, - verify=deluge_verify_cert, headers=headers) + verify=deluge_verify_cert, headers=headers) result['hash'] = json.loads(response.text)['result'] logger.debug('Deluge: Response was %s' % str(json.loads(response.text))) return json.loads(response.text)['result'] - except UnicodeDecodeError: - try: - # content is torrent file contents that needs to be encoded to base64 - # this time let's try leaving the encoding as is - logger.debug('Deluge: There was a decoding issue, let\'s try again') - post_data = json.dumps({"method": "core.add_torrent_file", - "params": [result['name'].decode('utf8') + '.torrent', - b64encode(result['content']), - options], - "id": 22}) - response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth, - verify=deluge_verify_cert, headers=headers) - result['hash'] = json.loads(response.text)['result'] - logger.debug('Deluge: Response was %s' % str(json.loads(response.text))) - return json.loads(response.text)['result'] - except Exception as e: - logger.error('Deluge: Adding torrent file failed after decode: %s' % str(e)) - formatted_lines = traceback.format_exc().splitlines() - logger.error('; '.join(formatted_lines)) - return False except Exception as e: logger.error('Deluge: Adding torrent file failed: %s' % str(e)) formatted_lines = traceback.format_exc().splitlines()