From 62de05befc38fe72af472d74df97b09bd505986a Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomashipp@googlemail.com>
Date: Sun, 12 Feb 2012 13:28:42 +0100
Subject: [PATCH] Magnet link support in autoadd plugin
Check the watch folders for .magnet files which contain valid magnet links
and add them.
Signed-off-by: Thomas Hipp <thomashipp@googlemail.com>
---
.../plugins/AutoAdd/deluge/plugins/autoadd/core.py | 41 +++++++++++++++----
1 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py
index c91eb46..7dae4de 100644
a
|
b
|
def set_options(self, watchdir_id, options):
|
161 | 161 | self.config.save() |
162 | 162 | component.get("EventManager").emit(AutoaddOptionsChangedEvent()) |
163 | 163 | |
164 | | def load_torrent(self, filename): |
| 164 | def load_torrent(self, filename, magnet): |
165 | 165 | try: |
166 | 166 | log.debug("Attempting to open %s for add.", filename) |
167 | | _file = open(filename, "rb") |
| 167 | if magnet == False: |
| 168 | _file = open(filename, "rb") |
| 169 | elif magnet == True: |
| 170 | _file = open(filename, "r") |
168 | 171 | filedump = _file.read() |
169 | 172 | if not filedump: |
170 | 173 | raise RuntimeError, "Torrent is 0 bytes!" |
… |
… |
def load_torrent(self, filename):
|
174 | 177 | raise e |
175 | 178 | |
176 | 179 | # Get the info to see if any exceptions are raised |
177 | | lt.torrent_info(lt.bdecode(filedump)) |
| 180 | if magnet == False: |
| 181 | lt.torrent_info(lt.bdecode(filedump)) |
178 | 182 | |
179 | 183 | return filedump |
180 | 184 | |
… |
… |
def update_watchdir(self, watchdir_id):
|
215 | 219 | if os.path.isdir(filepath): |
216 | 220 | # Skip directories |
217 | 221 | continue |
218 | | elif os.path.splitext(filename)[1] == ".torrent": |
| 222 | else: |
| 223 | ext = os.path.splitext(filename)[1] |
| 224 | if ext == ".torrent": |
| 225 | magnet = False |
| 226 | elif ext == ".magnet": |
| 227 | magnet = True |
| 228 | else: |
| 229 | continue |
219 | 230 | try: |
220 | | filedump = self.load_torrent(filepath) |
| 231 | filedump = self.load_torrent(filepath, magnet) |
221 | 232 | except (RuntimeError, Exception), e: |
222 | 233 | # If the torrent is invalid, we keep track of it so that we |
223 | 234 | # can try again on the next pass. This is because some |
… |
… |
def update_watchdir(self, watchdir_id):
|
237 | 248 | continue |
238 | 249 | |
239 | 250 | # The torrent looks good, so lets add it to the session. |
240 | | torrent_id = component.get("TorrentManager").add( |
241 | | filedump=filedump, filename=filename, options=opts, |
242 | | owner=watchdir.get("owner", "localclient") |
243 | | ) |
| 251 | if magnet == False: |
| 252 | torrent_id = component.get("TorrentManager").add( |
| 253 | filedump=filedump, filename=filename, options=opts, |
| 254 | owner=watchdir.get("owner", "localclient") |
| 255 | ) |
| 256 | elif magnet == True: |
| 257 | torrent_id = component.get("TorrentManager").add( |
| 258 | magnet=filedump, options=opts, |
| 259 | owner=watchdir.get("owner", "localclient") |
| 260 | ) |
244 | 261 | # If the torrent added successfully, set the extra options. |
245 | 262 | if torrent_id: |
246 | 263 | if 'Label' in component.get("CorePluginManager").get_enabled_plugins(): |
… |
… |
def update_watchdir(self, watchdir_id):
|
254 | 271 | component.get("TorrentManager").queue_top(torrent_id) |
255 | 272 | else: |
256 | 273 | component.get("TorrentManager").queue_bottom(torrent_id) |
| 274 | else: |
| 275 | # torrent handle is invalid and so is the magnet link |
| 276 | if magnet == True: |
| 277 | log.debug("invalid magnet link") |
| 278 | os.rename(filepath, filepath + ".invalid") |
| 279 | continue |
257 | 280 | |
258 | 281 | # Rename, copy or delete the torrent once added to deluge. |
259 | 282 | if watchdir.get('append_extension_toggle'): |