class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host = 'localhost', port = 58846, username = None, password = None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def add_torrent_magnet(self, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if options['label']: self.client.label.set_torrent(torrent_id, options['label']).get() except Exception, err: log.error('Failed to add torrent magnet %s: %s %s', (torrent, err, traceback.format_exc())) finally:
def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password)
class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host = 'localhost', port = 58846, username = None, password = None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except: return False return True def add_torrent_magnet(self, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if not torrent_id: torrent_id = self._check_torrent(True, torrent) if torrent_id and options['label']: self.client.label.set_torrent(torrent_id, options['label']).get() except Exception as err: log.error('Failed to add torrent magnet %s: %s %s', (torrent, err, traceback.format_exc())) finally: if self.client: self.disconnect() return torrent_id def add_torrent_file(self, filename, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_file(filename, b64encode(torrent), options).get() if not torrent_id: torrent_id = self._check_torrent(False, torrent) if torrent_id and options['label']: self.client.label.set_torrent(torrent_id, options['label']).get() except Exception as err: log.error('Failed to add torrent file %s: %s %s', (filename, err, traceback.format_exc())) finally: if self.client: self.disconnect() return torrent_id def get_alltorrents(self, ids): ret = False try: self.connect() ret = self.client.core.get_torrents_status({'id': ids}, ('name', 'hash', 'save_path', 'move_completed_path', 'progress', 'state', 'eta', 'ratio', 'stop_ratio', 'is_seed', 'is_finished', 'paused', 'move_on_completed', 'files')).get() except Exception as err: log.error('Failed to get all torrents: %s %s', (err, traceback.format_exc())) finally: if self.client: self.disconnect() return ret def pause_torrent(self, torrent_ids): try: self.connect() self.client.core.pause_torrent(torrent_ids).get() except Exception as err: log.error('Failed to pause torrent: %s %s', (err, traceback.format_exc())) finally: if self.client: self.disconnect() def resume_torrent(self, torrent_ids): try: self.connect() self.client.core.resume_torrent(torrent_ids).get() except Exception as err: log.error('Failed to resume torrent: %s %s', (err, traceback.format_exc())) finally: if self.client: self.disconnect() def remove_torrent(self, torrent_id, remove_local_data): ret = False try: self.connect() ret = self.client.core.remove_torrent(torrent_id, remove_local_data).get() except Exception as err: log.error('Failed to remove torrent: %s %s', (err, traceback.format_exc())) finally: if self.client: self.disconnect() return ret def disconnect(self): self.client.disconnect() def _check_torrent(self, magnet, torrent): # Torrent not added, check if it already existed. if magnet: torrent_hash = re.findall('urn:btih:([\w]{32,40})', torrent)[0] else: info = bdecode(torrent)["info"] torrent_hash = sha1(benc(info)).hexdigest() # Convert base 32 to hex if len(torrent_hash) == 32: torrent_hash = b16encode(b32decode(torrent_hash)) torrent_hash = torrent_hash.lower() torrent_check = self.client.core.get_torrent_status(torrent_hash, {}).get() if torrent_check['hash']: return torrent_hash return False
class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host='localhost', port=58846, username=None, password=None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except Exception: return False return True def add_torrent_magnet(self, torrent, options, torrent_hash): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception: return False finally: if self.client: self.disconnect() return torrent_id def add_torrent_file(self, filename, torrent, options, torrent_hash): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_file(filename, b64encode(torrent), options).get() if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception: return False finally: if self.client: self.disconnect() return torrent_id def set_torrent_label(self, torrent_id, label): try: self.connect() self.client.label.set_torrent(torrent_id, label).get() except Exception: return False finally: if self.client: self.disconnect() return True def set_torrent_path(self, torrent_id, path): try: self.connect() self.client.core.set_torrent_move_completed_path(torrent_id, path).get() self.client.core.set_torrent_move_completed(torrent_id, 1).get() except Exception: return False finally: if self.client: self.disconnect() return True def set_torrent_priority(self, torrent_ids, priority): try: self.connect() if priority: self.client.core.queue_top([torrent_ids]).get() except Exception, err: return False finally:
class DelugeRPC(object): host = "localhost" port = 58846 username = None password = None client = None def __init__(self, host="localhost", port=58846, username=None, password=None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except: return False return True def add_torrent_magnet(self, torrent, options, torrent_hash): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception as err: return False finally: if self.client: self.disconnect() return torrent_id def add_torrent_file(self, filename, torrent, options, torrent_hash): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_file(filename, b64encode(torrent), options).get() if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception as err: return False finally: if self.client: self.disconnect() return torrent_id def set_torrent_label(self, torrent_id, label): try: self.connect() self.client.label.set_torrent(torrent_id, label).get() except Exception as err: logger.log("DelugeD: Failed to set label for torrent: " + err + " " + traceback.format_exc(), logger.ERROR) return False finally: if self.client: self.disconnect() return True def set_torrent_path(self, torrent_id, path): try: self.connect() self.client.core.set_torrent_move_completed_path(torrent_id, path).get() self.client.core.set_torrent_move_completed(torrent_id, 1).get() except Exception as err: logger.log("DelugeD: Failed to set path for torrent: " + err + " " + traceback.format_exc(), logger.ERROR) return False finally: if self.client: self.disconnect() return True def pause_torrent(self, torrent_ids): try: self.connect() self.client.core.pause_torrent(torrent_ids).get() except Exception as err: logger.log("DelugeD: Failed to pause torrent: " + err + " " + traceback.format_exc(), logger.ERROR) return False finally: if self.client: self.disconnect() return True def disconnect(self): self.client.disconnect() def _check_torrent(self, torrent_hash): torrent_id = self.client.core.get_torrent_status(torrent_hash, {}).get() if torrent_id["hash"]: logger.log("DelugeD: Torrent already exists in Deluge", logger.DEBUG) return torrent_hash return False
class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host = 'localhost', port = 58846, username = None, password = None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except: return False return True def add_torrent_magnet(self, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if not torrent_id: torrent_id = self._check_torrent(True, torrent) except Exception as err: print "ERRERR: %s" % err return False finally: if self.client: self.disconnect() return torrent_id def add_torrent_file(self, filename, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_file(filename, b64encode(torrent), options).get() if not torrent_id: torrent_id = self._check_torrent(False, torrent) except Exception as err: return False finally: if self.client: self.disconnect() return torrent_id def set_torrent_label(self, torrent_id, label): try: self.connect() self.client.label.set_torrent(torrent_id, label).get() except Exception as err: logger.log('DelugeD: Failed to set label for torrent: ' + err + ' ' + traceback.format_exc(), logger.ERROR) return False finally: if self.client: self.disconnect() return True def pause_torrent(self, torrent_ids): try: self.connect() self.client.core.pause_torrent(torrent_ids).get() except Exception as err: logger.log('DelugeD: Failed to pause torrent: ' + err + ' ' + traceback.format_exc(), logger.ERROR) return False finally: if self.client: self.disconnect() return True def disconnect(self): self.client.disconnect() def _check_torrent(self, magnet, torrent): # Torrent not added, check if it already existed. if magnet: torrent_hash = re.findall('urn:btih:([\w]{32,40})', torrent)[0] else: info = bdecode(torrent)["info"] torrent_hash = sha1(benc(info)).hexdigest() # Convert base 32 to hex if len(torrent_hash) == 32: torrent_hash = b16encode(b32decode(torrent_hash)) torrent_hash = torrent_hash.lower() torrent_check = self.client.core.get_torrent_status(torrent_hash, {}).get() if torrent_check['hash']: return torrent_hash return False
class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host='localhost', port=58846, username=None, password=None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except Exception: return False else: return True def add_torrent_magnet(self, torrent, options, torrent_hash): try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() # pylint:disable=no-member if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception: return False else: return torrent_id finally: if self.client: self.disconnect() def add_torrent_file(self, filename, torrent, options, torrent_hash): try: self.connect() torrent_id = self.client.core.add_torrent_file(filename, b64encode(torrent), options).get() # pylint:disable=no-member if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception: return False else: return torrent_id finally: if self.client: self.disconnect() def set_torrent_label(self, torrent_id, label): try: self.connect() self.client.label.set_torrent(torrent_id, label).get() # pylint:disable=no-member except Exception: return False else: return True finally: if self.client: self.disconnect() def set_torrent_path(self, torrent_id, path): try: self.connect() self.client.core.set_torrent_move_completed_path(torrent_id, path).get() # pylint:disable=no-member self.client.core.set_torrent_move_completed(torrent_id, 1).get() # pylint:disable=no-member except Exception: return False else: return True finally: if self.client: self.disconnect() def set_torrent_priority(self, torrent_ids, priority): try: self.connect() if priority: self.client.core.queue_top([torrent_ids]).get() # pylint:disable=no-member except Exception: return False else: return True finally: if self.client: self.disconnect() def set_torrent_ratio(self, torrent_ids, ratio): try: self.connect() self.client.core.set_torrent_stop_at_ratio(torrent_ids, True).get() # pylint:disable=no-member self.client.core.set_torrent_stop_ratio(torrent_ids, ratio).get() # pylint:disable=no-member except Exception: return False else: return True finally: if self.client: self.disconnect() def pause_torrent(self, torrent_ids): try: self.connect() self.client.core.pause_torrent(torrent_ids).get() # pylint:disable=no-member except Exception: return False else: return True finally: if self.client: self.disconnect() def disconnect(self): self.client.disconnect() def _check_torrent(self, torrent_hash): torrent_id = self.client.core.get_torrent_status(torrent_hash, {}).get() # pylint:disable=no-member if torrent_id['hash']: logger.log('DelugeD: Torrent already exists in Deluge', logger.DEBUG) return torrent_hash return False
def connect(self): """Connect to the host using synchronousdeluge API.""" self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password)
class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host='localhost', port=58846, username=None, password=None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except Exception: return False return True def add_torrent_magnet(self, torrent, options, torrent_hash): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() # pylint:disable=no-member if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception: return False finally: if self.client: self.disconnect() return torrent_id def add_torrent_file(self, filename, torrent, options, torrent_hash): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_file( filename, b64encode(torrent), options).get() # pylint:disable=no-member if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception: return False finally: if self.client: self.disconnect() return torrent_id def set_torrent_label(self, torrent_id, label): try: self.connect() self.client.label.set_torrent(torrent_id, label).get() # pylint:disable=no-member except Exception: return False finally: if self.client: self.disconnect() return True def set_torrent_path(self, torrent_id, path): try: self.connect() self.client.core.set_torrent_move_completed_path(torrent_id, path).get() # pylint:disable=no-member self.client.core.set_torrent_move_completed(torrent_id, 1).get() # pylint:disable=no-member except Exception: return False finally: if self.client: self.disconnect() return True def set_torrent_priority(self, torrent_ids, priority): try: self.connect() if priority: self.client.core.queue_top([torrent_ids]).get() # pylint:disable=no-member except Exception: return False finally: if self.client: self.disconnect() return True def set_torrent_ratio(self, torrent_ids, ratio): try: self.connect() self.client.core.set_torrent_stop_at_ratio(torrent_ids, True).get() # pylint:disable=no-member self.client.core.set_torrent_stop_ratio(torrent_ids, ratio).get() # pylint:disable=no-member except Exception: return False finally: if self.client: self.disconnect() return True def pause_torrent(self, torrent_ids): try: self.connect() self.client.core.pause_torrent(torrent_ids).get() # pylint:disable=no-member except Exception: return False finally: if self.client: self.disconnect() return True def disconnect(self): self.client.disconnect() def _check_torrent(self, torrent_hash): torrent_id = self.client.core.get_torrent_status(torrent_hash, {}).get() # pylint:disable=no-member if torrent_id['hash']: logger.log('DelugeD: Torrent already exists in Deluge', logger.DEBUG) return torrent_hash return False
class DelugeRPC(object): """Deluge RPC client class.""" host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host='localhost', port=58846, username=None, password=None): """Constructor. :param host: :type host: str :param port: :type port: int :param username: :type username: str :param password: :type password: str """ super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): """Connect to the host using synchronousdeluge API.""" self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): """Test connection. :return: :rtype: bool """ try: self.connect() except Exception: return False else: return True def add_torrent_magnet(self, torrent, options, torrent_hash): """Add Torrent magnet and return torrent id/hash. :param torrent: :type torrent: str :param options: :type options: dict :param torrent_hash: :type torrent_hash: str :return: :rtype: str or bool """ try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception: return False else: return torrent_id finally: if self.client: self.disconnect() def add_torrent_file(self, filename, torrent, options, torrent_hash): """Add Torrent file and return torrent id/hash. :param filename: :type filename: str :param torrent: :type torrent: str :param options: :type options: dict :param torrent_hash: :type torrent_hash: str :return: :rtype: str or bool """ try: self.connect() torrent_id = self.client.core.add_torrent_file( filename, b64encode(torrent), options).get() if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception: return False else: return torrent_id finally: if self.client: self.disconnect() def set_torrent_label(self, torrent_id, label): """Set Torrent label. :param torrent_id: :type torrent_id: str :param label: :type label: str :return: :rtype: bool """ try: self.connect() self.client.label.set_torrent(torrent_id, label).get() except Exception: return False else: return True finally: if self.client: self.disconnect() def set_torrent_path(self, torrent_id, path): """Set Torrent path. :param torrent_id: :type torrent_id: str :param path: :type path: str :return: :rtype: bool """ try: self.connect() self.client.core.set_torrent_move_completed_path(torrent_id, path).get() self.client.core.set_torrent_move_completed(torrent_id, 1).get() except Exception: return False else: return True finally: if self.client: self.disconnect() def set_torrent_priority(self, torrent_id, priority): """Set Torrent priority. :param torrent_id: :type torrent_id: str :param priority: :type priority: bool :return: :rtype: bool """ try: self.connect() if priority: self.client.core.queue_top([torrent_id]).get() except Exception: return False else: return True finally: if self.client: self.disconnect() def set_torrent_ratio(self, torrent_id, ratio): """Set Torrent ratio. :param torrent_id: :type torrent_id: str :param ratio: :type ratio: float :return: :rtype: bool """ try: self.connect() self.client.core.set_torrent_stop_at_ratio(torrent_id, True).get() self.client.core.set_torrent_stop_ratio(torrent_id, ratio).get() except Exception: return False else: return True finally: if self.client: self.disconnect() def pause_torrent(self, torrent_ids): """Pause torrent. :param torrent_ids: :type torrent_ids: list of str :return: :rtype: bool """ try: self.connect() self.client.core.pause_torrent(torrent_ids).get() except Exception: return False else: return True finally: if self.client: self.disconnect() def disconnect(self): """Disconnect RPC client.""" self.client.disconnect() def _check_torrent(self, torrent_hash): torrent_id = self.client.core.get_torrent_status(torrent_hash, {}).get() if torrent_id['hash']: logger.debug('DelugeD: Torrent already exists in Deluge') return torrent_hash return False
class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host='localhost', port=58846, username=None, password=None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except: return False return True def add_torrent_magnet(self, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if not torrent_id: torrent_id = self._check_torrent(True, torrent) if torrent_id and options['label']: self.client.label.set_torrent(torrent_id, options['label']).get() except Exception as err: log.error('Failed to add torrent magnet %s: %s %s', (torrent, err, traceback.format_exc())) finally: if self.client: self.disconnect() return torrent_id def add_torrent_file(self, filename, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_file( filename, b64encode(torrent), options).get() if not torrent_id: torrent_id = self._check_torrent(False, torrent) if torrent_id and options['label']: self.client.label.set_torrent(torrent_id, options['label']).get() except Exception as err: log.error('Failed to add torrent file %s: %s %s', (filename, err, traceback.format_exc())) finally: if self.client: self.disconnect() return torrent_id def get_all_torrents(self, ids): ret = False try: self.connect() ret = self.client.core.get_torrents_status({ 'id': ids }, ('name', 'hash', 'save_path', 'move_completed_path', 'progress', 'state', 'eta', 'ratio', 'stop_ratio', 'is_seed', 'is_finished', 'paused', 'move_on_completed', 'files')).get() except Exception as err: log.error('Failed to get all torrents: %s %s', (err, traceback.format_exc())) finally: if self.client: self.disconnect() return ret def pause_torrent(self, torrent_ids): try: self.connect() self.client.core.pause_torrent(torrent_ids).get() except Exception as err: log.error('Failed to pause torrent: %s %s', (err, traceback.format_exc())) finally: if self.client: self.disconnect() def resume_torrent(self, torrent_ids): try: self.connect() self.client.core.resume_torrent(torrent_ids).get() except Exception as err: log.error('Failed to resume torrent: %s %s', (err, traceback.format_exc())) finally: if self.client: self.disconnect() def remove_torrent(self, torrent_id, remove_local_data): ret = False try: self.connect() ret = self.client.core.remove_torrent(torrent_id, remove_local_data).get() except Exception as err: log.error('Failed to remove torrent: %s %s', (err, traceback.format_exc())) finally: if self.client: self.disconnect() return ret def disconnect(self): self.client.disconnect() def _check_torrent(self, magnet, torrent): # Torrent not added, check if it already existed. if magnet: torrent_hash = re.findall('urn:btih:([\w]{32,40})', torrent)[0] else: info = bdecode(torrent)["info"] torrent_hash = sha1(benc(info)).hexdigest() # Convert base 32 to hex if len(torrent_hash) == 32: torrent_hash = b16encode(b32decode(torrent_hash)) torrent_hash = torrent_hash.lower() torrent_check = self.client.core.get_torrent_status(torrent_hash, {}).get() if torrent_check['hash']: return torrent_hash return False
class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host = 'localhost', port = 58846, username = None, password = None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except: return False return True def add_torrent_magnet(self, torrent, options, torrent_hash): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception as err: return False finally: if self.client: self.disconnect() return torrent_id def add_torrent_file(self, filename, torrent, options, torrent_hash): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_file(filename, b64encode(torrent), options).get() if not torrent_id: torrent_id = self._check_torrent(torrent_hash) except Exception as err: return False finally: if self.client: self.disconnect() return torrent_id def set_torrent_label(self, torrent_id, label): try: self.connect() self.client.label.set_torrent(torrent_id, label).get() except Exception as err: return False finally: if self.client: self.disconnect() return True def set_torrent_path(self, torrent_id, path): try: self.connect() self.client.core.set_torrent_move_completed_path(torrent_id, path).get() self.client.core.set_torrent_move_completed(torrent_id, 1).get() except Exception as err: return False finally: if self.client: self.disconnect() return True def set_torrent_priority(self, torrent_ids, priority): try: self.connect() if priority: self.client.core.queue_top([torrent_ids]).get() except Exception, err: return False finally:
class DelugeRPC(object): host = 'localhost' port = 58846 username = None password = None client = None def __init__(self, host = 'localhost', port = 58846, username = None, password = None): super(DelugeRPC, self).__init__() self.host = host self.port = port self.username = username self.password = password def connect(self): self.client = DelugeClient() self.client.connect(self.host, int(self.port), self.username, self.password) def test(self): try: self.connect() except: return False return True def add_torrent_magnet(self, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_magnet(torrent, options).get() if not torrent_id: torrent_id = self._check_torrent(True, torrent) except Exception as err: print "ERRERR: %s" % err return False finally: if self.client: self.disconnect() return torrent_id def add_torrent_file(self, filename, torrent, options): torrent_id = False try: self.connect() torrent_id = self.client.core.add_torrent_file(filename, b64encode(torrent), options).get() if not torrent_id: torrent_id = self._check_torrent(False, torrent) except Exception as err: return False finally: if self.client: self.disconnect() return torrent_id def set_torrent_label(self, torrent_id, label): try: self.connect() self.client.label.set_torrent(torrent_id, label).get() except Exception as err: logger.log('DelugeD: Failed to set label for torrent: ' + err + ' ' + traceback.format_exc(), logger.ERROR) return False finally: if self.client: self.disconnect() return True def set_torrent_path(self, torrent_id, path): try: self.connect() self.client.core.set_torrent_move_completed_path(torrent_id, path).get() self.client.core.set_torrent_move_completed(torrent_id, 1).get() except Exception as err: logger.log('DelugeD: Failed to set path for torrent: ' + err + ' ' + traceback.format_exc(), logger.ERROR) return False finally: if self.client: self.disconnect() return True def pause_torrent(self, torrent_ids): try: self.connect() self.client.core.pause_torrent(torrent_ids).get() except Exception as err: logger.log('DelugeD: Failed to pause torrent: ' + err + ' ' + traceback.format_exc(), logger.ERROR) return False finally: if self.client: self.disconnect() return True def disconnect(self): self.client.disconnect() def _check_torrent(self, magnet, torrent): # Torrent not added, check if it already existed. if magnet: torrent_hash = re.findall('urn:btih:([\w]{32,40})', torrent)[0] else: info = bdecode(torrent)["info"] torrent_hash = sha1(benc(info)).hexdigest() # Convert base 32 to hex if len(torrent_hash) == 32: torrent_hash = b16encode(b32decode(torrent_hash)) torrent_hash = torrent_hash.lower() torrent_check = self.client.core.get_torrent_status(torrent_hash, {}).get() if torrent_check['hash']: return torrent_hash return False