Ticket #2030: 0001-Magnet-link-support-in-autoadd-plugin.patch

File 0001-Magnet-link-support-in-autoadd-plugin.patch, 4.3 KB (added by Thomas Hipp, 13 years ago)
  • deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py

    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):  
    161161        self.config.save()
    162162        component.get("EventManager").emit(AutoaddOptionsChangedEvent())
    163163
    164     def load_torrent(self, filename):
     164    def load_torrent(self, filename, magnet):
    165165        try:
    166166            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")
    168171            filedump = _file.read()
    169172            if not filedump:
    170173                raise RuntimeError, "Torrent is 0 bytes!"
    def load_torrent(self, filename):  
    174177            raise e
    175178
    176179        # 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))
    178182
    179183        return filedump
    180184
    def update_watchdir(self, watchdir_id):  
    215219            if os.path.isdir(filepath):
    216220                # Skip directories
    217221                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
    219230                try:
    220                     filedump = self.load_torrent(filepath)
     231                    filedump = self.load_torrent(filepath, magnet)
    221232                except (RuntimeError, Exception), e:
    222233                    # If the torrent is invalid, we keep track of it so that we
    223234                    # can try again on the next pass.  This is because some
    def update_watchdir(self, watchdir_id):  
    237248                    continue
    238249
    239250                # 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                    )
    244261                # If the torrent added successfully, set the extra options.
    245262                if torrent_id:
    246263                    if 'Label' in component.get("CorePluginManager").get_enabled_plugins():
    def update_watchdir(self, watchdir_id):  
    254271                            component.get("TorrentManager").queue_top(torrent_id)
    255272                        else:
    256273                            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
    257280
    258281                # Rename, copy or delete the torrent once added to deluge.
    259282                if watchdir.get('append_extension_toggle'):