mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-03 06:58:42 +00:00
Magnet link support in autoadd plugin
Check the watch folders for .magnet files which contain valid magnet links and add them.
This commit is contained in:
parent
e4840d6b37
commit
bbeb11b1e7
1 changed files with 49 additions and 22 deletions
|
@ -155,10 +155,13 @@ class Core(CorePluginBase):
|
|||
self.config.save()
|
||||
component.get("EventManager").emit(AutoaddOptionsChangedEvent())
|
||||
|
||||
def load_torrent(self, filename):
|
||||
def load_torrent(self, filename, magnet):
|
||||
try:
|
||||
log.debug("Attempting to open %s for add.", filename)
|
||||
if magnet == False:
|
||||
_file = open(filename, "rb")
|
||||
elif magnet == True:
|
||||
_file = open(filename, "r")
|
||||
filedump = _file.read()
|
||||
if not filedump:
|
||||
raise RuntimeError, "Torrent is 0 bytes!"
|
||||
|
@ -168,7 +171,8 @@ class Core(CorePluginBase):
|
|||
raise e
|
||||
|
||||
# Get the info to see if any exceptions are raised
|
||||
info = lt.torrent_info(lt.bdecode(filedump))
|
||||
if magnet == False:
|
||||
lt.torrent_info(lt.bdecode(filedump))
|
||||
|
||||
return filedump
|
||||
|
||||
|
@ -197,14 +201,25 @@ class Core(CorePluginBase):
|
|||
if watchdir.get(option+'_toggle', True):
|
||||
opts[option] = value
|
||||
for filename in os.listdir(watchdir["abspath"]):
|
||||
if filename.split(".")[-1] == "torrent":
|
||||
try:
|
||||
filepath = os.path.join(watchdir["abspath"], filename)
|
||||
except UnicodeDecodeError, e:
|
||||
log.error("Unable to auto add torrent due to inproper filename encoding: %s", e)
|
||||
log.error("Unable to auto add torrent due to improper "
|
||||
"filename encoding: %s", e)
|
||||
continue
|
||||
if os.path.isdir(filepath):
|
||||
# Skip directories
|
||||
continue
|
||||
else:
|
||||
ext = os.path.splitext(filename)[1]
|
||||
if ext == ".torrent":
|
||||
magnet = False
|
||||
elif ext == ".magnet":
|
||||
magnet = True
|
||||
else:
|
||||
continue
|
||||
try:
|
||||
filedump = self.load_torrent(filepath)
|
||||
filedump = self.load_torrent(filepath, magnet)
|
||||
except (RuntimeError, Exception), e:
|
||||
# If the torrent is invalid, we keep track of it so that we
|
||||
# can try again on the next pass. This is because some
|
||||
|
@ -220,7 +235,12 @@ class Core(CorePluginBase):
|
|||
continue
|
||||
|
||||
# The torrent looks good, so lets add it to the session.
|
||||
torrent_id = component.get("TorrentManager").add(filedump=filedump, filename=filename, options=opts)
|
||||
if magnet == False:
|
||||
torrent_id = component.get("TorrentManager").add(
|
||||
filedump=filedump, filename=filename, options=opts)
|
||||
elif magnet == True:
|
||||
torrent_id = component.get("TorrentManager").add(
|
||||
magnet=filedump, options=opts)
|
||||
# If the torrent added successfully, set the extra options.
|
||||
if torrent_id:
|
||||
if 'Label' in component.get("CorePluginManager").get_enabled_plugins():
|
||||
|
@ -234,7 +254,14 @@ class Core(CorePluginBase):
|
|||
component.get("TorrentManager").queue_top(torrent_id)
|
||||
else:
|
||||
component.get("TorrentManager").queue_bottom(torrent_id)
|
||||
# Rename or delete the torrent once added to deluge.
|
||||
else:
|
||||
# torrent handle is invalid and so is the magnet link
|
||||
if magnet == True:
|
||||
log.debug("invalid magnet link")
|
||||
os.rename(filepath, filepath + ".invalid")
|
||||
continue
|
||||
|
||||
# Rename, copy or delete the torrent once added to deluge.
|
||||
if watchdir.get('append_extension_toggle'):
|
||||
if not watchdir.get('append_extension'):
|
||||
watchdir['append_extension'] = ".added"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue