From c8d084c56369d4e23afef68c67e074af2eaa2d56 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Tue, 21 Mar 2017 20:03:48 +0000 Subject: [PATCH] [AutoAdd] Fixes for adding magnets * Combines three commits from 1.3-stable that fixed splitting magnets from file. b52de1549eb, 8a3f15e5c0, 2d4dec669e --- deluge/core/torrentmanager.py | 2 +- .../AutoAdd/deluge/plugins/autoadd/core.py | 25 +++++++++++-------- deluge/plugins/AutoAdd/setup.py | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 238f91d24..57ef22d5c 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -334,7 +334,7 @@ class TorrentManager(component.Component): elif magnet: magnet_info = get_magnet_info(magnet) if magnet_info: - add_torrent_params['url'] = magnet.encode('utf8') + add_torrent_params['url'] = magnet.strip().encode('utf8') add_torrent_params['name'] = magnet_info['name'] torrent_id = magnet_info['info_hash'] else: diff --git a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py index 56b4f2cde..b495d43d1 100644 --- a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py +++ b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py @@ -26,7 +26,7 @@ from twisted.internet.task import LoopingCall, deferLater import deluge.component as component import deluge.configmanager from deluge._libtorrent import lt -from deluge.common import AUTH_LEVEL_ADMIN +from deluge.common import AUTH_LEVEL_ADMIN, is_magnet from deluge.core.rpcserver import export from deluge.event import DelugeEvent from deluge.plugins.pluginbase import CorePluginBase @@ -172,26 +172,29 @@ class Core(CorePluginBase): magnets = [] try: with open(filename, 'r') as _file: - for line in _file: - line = line.strip() - if line: - magnets.append(line) + magnets = list(filter(len, _file.read().splitlines())) except IOError as ex: log.warning('Unable to open %s: %s', filename, ex) if len(magnets) < 2: return [] - n = 0 path = filename.rsplit(os.sep, 1)[0] for magnet in magnets: + if not is_magnet(magnet): + log.warning('Found line which is not a magnet: %s', magnet) + continue + for part in magnet.split('&'): if part.startswith('dn='): - mname = os.sep.join([path, part[3:] + '.magnet']) - break + name = part[3:].strip() + if name: + mname = os.sep.join([path, name + '.magnet']) + break else: - mname = '.'.join([filename, str(n), 'magnet']) - n += 1 + short_hash = magnet.split('btih:')[1][:8] + mname = '.'.join([os.path.splitext(filename)[0], short_hash, 'magnet']) + try: with open(mname, 'w') as _mfile: _mfile.write(magnet) @@ -277,7 +280,7 @@ class Core(CorePluginBase): # The torrent looks good, so lets add it to the session. if magnet: torrent_id = component.get('Core').add_torrent_magnet( - filedump, options) + filedump.strip(), options) else: torrent_id = component.get('Core').add_torrent_file( filename, base64.encodestring(filedump), options) diff --git a/deluge/plugins/AutoAdd/setup.py b/deluge/plugins/AutoAdd/setup.py index 7d18adef6..079c1bf76 100644 --- a/deluge/plugins/AutoAdd/setup.py +++ b/deluge/plugins/AutoAdd/setup.py @@ -18,7 +18,7 @@ from setuptools import find_packages, setup __plugin_name__ = 'AutoAdd' __author__ = 'Chase Sterling, Pedro Algarvio' __author_email__ = 'chase.sterling@gmail.com, pedro@algarvio.me' -__version__ = '1.05' +__version__ = '1.06' __url__ = 'http://dev.deluge-torrent.org/wiki/Plugins/AutoAdd' __license__ = 'GPLv3' __description__ = 'Monitors folders for .torrent files.'