Ticket #2030: 0002-Multiple-Magnet-links-support-in-autoadd-plugin.patch

File 0002-Multiple-Magnet-links-support-in-autoadd-plugin.patch, 3.3 KB (added by Thomas Hipp, 13 years ago)
  • deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py

    From 14fef1f0ea2260f7f5bbf5d3dd4042e83de35c8f Mon Sep 17 00:00:00 2001
    From: Thomas Hipp <thomashipp@googlemail.com>
    Date: Mon, 13 Feb 2012 13:41:55 +0100
    Subject: [PATCH 2/2] Multiple Magnet links support in autoadd plugin
    
    Check watch folders for .magnet files wich contain valid magnet links
    and add them.  In case a .magnet file provides more than one link, a new
    .magnet file is created for each of these links.
    
    Signed-off-by: Thomas Hipp <thomashipp@googlemail.com>
    ---
     .../plugins/AutoAdd/deluge/plugins/autoadd/core.py |   48 ++++++++++++++++++++
     1 files changed, 48 insertions(+), 0 deletions(-)
    
    diff --git a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py
    index 7dae4de..6d829b4 100644
    a b def load_torrent(self, filename, magnet):  
    182182
    183183        return filedump
    184184
     185    def split_magnets(self, filename):
     186        log.debug("Attempting to open %s for splitting magnets.", filename)
     187        try:
     188            _file = open(filename, "r")
     189        except IOError, e:
     190            log.warning("Unable to open %s: %s", filename, e)
     191            raise e
     192        else:
     193            magnets = list(filter(len, _file.readlines()))
     194            _file.close()
     195            if len(magnets) < 2:
     196                return
     197            n = 0
     198            path = filename.rsplit(os.sep, 1)[0]
     199            for magnet in magnets:
     200                for part in magnet.split('&'):
     201                    if part.startswith("dn="):
     202                        mname = os.sep.join([path, part[3:] + ".magnet"])
     203                        break
     204                else:
     205                    mname = '.'.join([filename, str(n), "magnet"])
     206                    n += 1
     207                try:
     208                    _mfile = open(mname, "w")
     209                except IOError, e:
     210                    log.warning("Unable to open %s: %s", mname, e)
     211                else:
     212                    _mfile.write(magnet)
     213                    _mfile.close()
     214            return magnets
     215
    185216    def update_watchdir(self, watchdir_id):
    186217        """Check the watch folder for new torrents to add."""
    187218        log.trace("Updating watchdir id: %s", watchdir_id)
    def update_watchdir(self, watchdir_id):  
    209240            if OPTIONS_AVAILABLE.get(option):
    210241                if watchdir.get(option+'_toggle', True):
    211242                    opts[option] = value
     243
     244        # Check for .magnet files containing multiple magnet links and
     245        # create a new .magnet file for each of them.
     246        for filename in os.listdir(watchdir["abspath"]):
     247            try:
     248                filepath = os.path.join(watchdir["abspath"], filename)
     249            except UnicodeDecodeError, e:
     250                log.error("Unable to auto add torrent due to improper "
     251                          "filename encoding: %s", e)
     252                continue
     253            if os.path.isdir(filepath):
     254                # Skip directories
     255                continue
     256            elif os.path.splitext(filename)[1] == ".magnet" and \
     257                    self.split_magnets(filepath):
     258                os.remove(filepath)
     259
    212260        for filename in os.listdir(watchdir["abspath"]):
    213261            try:
    214262                filepath = os.path.join(watchdir["abspath"], filename)