[Autoadd] Fixes for splitting magnets from file

* use splitlines to remove line endings so filter with len works as intended.
 * use a short form of the magnet hash so the resulting filename will be unique
   and prevent potential overwriting of other files.
 * verify magnet is valid
This commit is contained in:
Calum Lind 2017-02-14 18:32:53 +00:00
parent 8565eccb3d
commit 8a3f15e5c0
1 changed files with 8 additions and 4 deletions

View File

@ -39,6 +39,7 @@
from deluge._libtorrent import lt from deluge._libtorrent import lt
import os import os
from deluge.common import is_magnet
from deluge.log import LOG as log from deluge.log import LOG as log
from deluge.plugins.pluginbase import CorePluginBase from deluge.plugins.pluginbase import CorePluginBase
import deluge.component as component import deluge.component as component
@ -184,20 +185,23 @@ class Core(CorePluginBase):
log.warning("Unable to open %s: %s", filename, e) log.warning("Unable to open %s: %s", filename, e)
raise e raise e
else: else:
magnets = list(filter(len, _file.readlines())) magnets = list(filter(len, _file.read().splitlines()))
_file.close() _file.close()
if len(magnets) < 2: if len(magnets) < 2:
return return
n = 0
path = filename.rsplit(os.sep, 1)[0] path = filename.rsplit(os.sep, 1)[0]
for magnet in magnets: 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('&'): for part in magnet.split('&'):
if part.startswith("dn="): if part.startswith("dn="):
mname = os.sep.join([path, part[3:] + ".magnet"]) mname = os.sep.join([path, part[3:] + ".magnet"])
break break
else: else:
mname = '.'.join([filename, str(n), "magnet"]) short_hash = magnet.split("btih:")[1][:8]
n += 1 mname = '.'.join([filename, short_hash, "magnet"])
try: try:
_mfile = open(mname, "w") _mfile = open(mname, "w")
except IOError, e: except IOError, e: