[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
import os
from deluge.common import is_magnet
from deluge.log import LOG as log
from deluge.plugins.pluginbase import CorePluginBase
import deluge.component as component
@ -184,20 +185,23 @@ class Core(CorePluginBase):
log.warning("Unable to open %s: %s", filename, e)
raise e
else:
magnets = list(filter(len, _file.readlines()))
magnets = list(filter(len, _file.read().splitlines()))
_file.close()
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
else:
mname = '.'.join([filename, str(n), "magnet"])
n += 1
short_hash = magnet.split("btih:")[1][:8]
mname = '.'.join([filename, short_hash, "magnet"])
try:
_mfile = open(mname, "w")
except IOError, e: