diff options
Diffstat (limited to 'deluge/plugins/AutoAdd/deluge_autoadd/core.py')
-rw-r--r-- | deluge/plugins/AutoAdd/deluge_autoadd/core.py | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/deluge/plugins/AutoAdd/deluge_autoadd/core.py b/deluge/plugins/AutoAdd/deluge_autoadd/core.py index 79e5327..07ad53a 100644 --- a/deluge/plugins/AutoAdd/deluge_autoadd/core.py +++ b/deluge/plugins/AutoAdd/deluge_autoadd/core.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009 GazpachoKing <chase.sterling@gmail.com> # Copyright (C) 2011 Pedro Algarvio <pedro@algarvio.me> @@ -13,22 +12,22 @@ # See LICENSE for more details. # -from __future__ import unicode_literals - import logging import os import shutil from base64 import b64encode from twisted.internet import reactor +from twisted.internet.defer import maybeDeferred from twisted.internet.task import LoopingCall, deferLater +from twisted.python.failure import Failure import deluge.component as component import deluge.configmanager from deluge._libtorrent import lt from deluge.common import AUTH_LEVEL_ADMIN, is_magnet from deluge.core.rpcserver import export -from deluge.error import AddTorrentError +from deluge.error import AddTorrentError, InvalidTorrentError from deluge.event import DelugeEvent from deluge.plugins.pluginbase import CorePluginBase @@ -150,7 +149,7 @@ class Core(CorePluginBase): try: with open(filename, file_mode) as _file: filedump = _file.read() - except IOError as ex: + except OSError as ex: log.warning('Unable to open %s: %s', filename, ex) raise ex @@ -159,7 +158,10 @@ class Core(CorePluginBase): # Get the info to see if any exceptions are raised if not magnet: - lt.torrent_info(lt.bdecode(filedump)) + decoded_torrent = lt.bdecode(filedump) + if decoded_torrent is None: + raise InvalidTorrentError('Torrent file failed decoding.') + lt.torrent_info(decoded_torrent) return filedump @@ -167,9 +169,9 @@ class Core(CorePluginBase): log.debug('Attempting to open %s for splitting magnets.', filename) magnets = [] try: - with open(filename, 'r') as _file: + with open(filename) as _file: magnets = list(filter(len, _file.read().splitlines())) - except IOError as ex: + except OSError as ex: log.warning('Unable to open %s: %s', filename, ex) if len(magnets) < 2: @@ -194,7 +196,7 @@ class Core(CorePluginBase): try: with open(mname, 'w') as _mfile: _mfile.write(magnet) - except IOError as ex: + except OSError as ex: log.warning('Unable to open %s: %s', mname, ex) return magnets @@ -269,7 +271,7 @@ class Core(CorePluginBase): try: filedump = self.load_torrent(filepath, magnet) - except (IOError, EOFError) as ex: + except (OSError, EOFError, InvalidTorrentError) as ex: # If torrent is invalid, keep track of it so can try again on the next pass. # This catches torrent files that may not be fully saved to disk at load time. log.debug('Torrent is invalid: %s', ex) @@ -325,6 +327,9 @@ class Core(CorePluginBase): os.remove(filepath) def fail_torrent_add(err_msg, filepath, magnet): + if isinstance(err_msg, Failure): + err_msg = err_msg.getErrorMessage() + # torrent handle is invalid and so is the magnet link log.error( 'Cannot Autoadd %s: %s: %s', @@ -337,15 +342,17 @@ class Core(CorePluginBase): try: # The torrent looks good, so lets add it to the session. if magnet: - d = component.get('Core').add_torrent_magnet( - filedump.strip(), options + d = maybeDeferred( + component.get('Core').add_torrent_magnet, + filedump.strip(), + options, ) else: d = component.get('Core').add_torrent_file_async( filename, b64encode(filedump), options ) - d.addCallback(on_torrent_added, filename, filepath) - d.addErrback(fail_torrent_add, filepath, magnet) + d.addCallback(on_torrent_added, filename, filepath) + d.addErrback(fail_torrent_add, filepath, magnet) except AddTorrentError as ex: fail_torrent_add(str(ex), filepath, magnet) |