diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index a7460953..43691906 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -327,7 +327,7 @@ Black Hole Transmission uTorrent (Beta) - Deluge (Beta) + Deluge QBitTorrent
@@ -448,6 +448,11 @@ Labels shouldn't contain spaces (requires Label plugin) +
+ + + Directory where Deluge should download to +
diff --git a/headphones/config.py b/headphones/config.py index 6b8dabac..cf5231e6 100644 --- a/headphones/config.py +++ b/headphones/config.py @@ -80,6 +80,7 @@ _CONFIG_DEFINITIONS = { 'DELUGE_PASSWORD': (str, 'Deluge', ''), 'DELUGE_LABEL': (str, 'Deluge', ''), 'DELUGE_DONE_DIRECTORY': (str, 'Deluge', ''), + 'DELUGE_DOWNLOAD_DIRECTORY': (str, 'Deluge', ''), 'DELUGE_PAUSED': (int, 'Deluge', 0), 'DESTINATION_DIR': (str, 'General', ''), 'DETECT_BITRATE': (int, 'General', 0), diff --git a/headphones/deluge.py b/headphones/deluge.py index 6d90424d..5130fb96 100644 --- a/headphones/deluge.py +++ b/headphones/deluge.py @@ -466,19 +466,56 @@ def _add_torrent_url(result): def _add_torrent_file(result): logger.debug('Deluge: Adding file') + + options = {} + + if headphones.CONFIG.DELUGE_DOWNLOAD_DIRECTORY: + options['download_location'] = headphones.CONFIG.DELUGE_DOWNLOAD_DIRECTORY + + if headphones.CONFIG.DELUGE_DONE_DIRECTORY or headphones.CONFIG.DOWNLOAD_TORRENT_DIR: + options['move_completed'] = 1 + if headphones.CONFIG.DELUGE_DONE_DIRECTORY: + options['move_completed_path'] = headphones.CONFIG.DELUGE_DONE_DIRECTORY + else: + options['move_completed_path'] = headphones.CONFIG.DOWNLOAD_TORRENT_DIR + + if headphones.CONFIG.DELUGE_PAUSED: + options['add_paused'] = headphones.CONFIG.DELUGE_PAUSED + if not any(delugeweb_auth): _get_auth() try: # 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']).decode(), {}], + b64encode(result['content'].encode('utf8')), + options], "id": 2}) 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 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() @@ -566,61 +603,3 @@ def setSeedRatio(result): return None -def setTorrentPath(result): - logger.debug('Deluge: Setting download path') - if not any(delugeweb_auth): - _get_auth() - - try: - if headphones.CONFIG.DELUGE_DONE_DIRECTORY or headphones.CONFIG.DOWNLOAD_TORRENT_DIR: - post_data = json.dumps({"method": "core.set_torrent_move_completed", - "params": [result['hash'], True], - "id": 7}) - response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth, - verify=deluge_verify_cert, headers=headers) - - if headphones.CONFIG.DELUGE_DONE_DIRECTORY: - move_to = headphones.CONFIG.DELUGE_DONE_DIRECTORY - else: - move_to = headphones.CONFIG.DOWNLOAD_TORRENT_DIR - - if not os.path.exists(move_to): - logger.debug('Deluge: %s directory doesn\'t exist, let\'s create it' % move_to) - os.makedirs(move_to) - post_data = json.dumps({"method": "core.set_torrent_move_completed_path", - "params": [result['hash'], move_to], - "id": 8}) - response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth, - verify=deluge_verify_cert, headers=headers) - - return not json.loads(response.text)['error'] - - return True - except Exception as e: - logger.error('Deluge: Setting torrent move-to directory failed: %s' % str(e)) - formatted_lines = traceback.format_exc().splitlines() - logger.error('; '.join(formatted_lines)) - return None - - -def setTorrentPause(result): - logger.debug('Deluge: Pausing torrent') - if not any(delugeweb_auth): - _get_auth() - - try: - if headphones.CONFIG.DELUGE_PAUSED: - post_data = json.dumps({"method": "core.pause_torrent", - "params": [[result['hash']]], - "id": 9}) - response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth, - verify=deluge_verify_cert, headers=headers) - - return not json.loads(response.text)['error'] - - return True - except Exception as e: - logger.error('Deluge: Setting torrent paused failed: %s' % str(e)) - formatted_lines = traceback.format_exc().splitlines() - logger.error('; '.join(formatted_lines)) - return None diff --git a/headphones/searcher.py b/headphones/searcher.py index f0aeec33..afc47b22 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -988,10 +988,6 @@ def send_to_downloader(data, result, album): logger.error("Error sending torrent to Deluge. Are you sure it's running? Maybe the torrent already exists?") return - # This pauses the torrent right after it is added - if headphones.CONFIG.DELUGE_PAUSED: - deluge.setTorrentPause({'hash': torrentid}) - # Set Label if headphones.CONFIG.DELUGE_LABEL: deluge.setTorrentLabel({'hash': torrentid}) @@ -1001,10 +997,6 @@ def send_to_downloader(data, result, album): if seed_ratio is not None: deluge.setSeedRatio({'hash': torrentid, 'ratio': seed_ratio}) - # Set move-to directory - if headphones.CONFIG.DELUGE_DONE_DIRECTORY or headphones.CONFIG.DOWNLOAD_TORRENT_DIR: - deluge.setTorrentPath({'hash': torrentid}) - # Get folder name from Deluge, it's usually the torrent name folder_name = deluge.getTorrentFolder({'hash': torrentid}) if folder_name: diff --git a/headphones/webserve.py b/headphones/webserve.py index 6e564763..9b45df0f 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -1183,6 +1183,7 @@ class WebInterface(object): "deluge_password": headphones.CONFIG.DELUGE_PASSWORD, "deluge_label": headphones.CONFIG.DELUGE_LABEL, "deluge_done_directory": headphones.CONFIG.DELUGE_DONE_DIRECTORY, + "deluge_download_directory": headphones.CONFIG.DELUGE_DOWNLOAD_DIRECTORY, "deluge_paused": checked(headphones.CONFIG.DELUGE_PAUSED), "utorrent_host": headphones.CONFIG.UTORRENT_HOST, "utorrent_username": headphones.CONFIG.UTORRENT_USERNAME,