diff --git a/libs/qbittorrent/client.py b/libs/qbittorrent/client.py index 0365a1a..bc59cd0 100644 --- a/libs/qbittorrent/client.py +++ b/libs/qbittorrent/client.py @@ -2,6 +2,7 @@ from qbittorrent.base import Base from qbittorrent.torrent import Torrent from requests import Session from requests.auth import HTTPDigestAuth +import time class QBittorrentClient(Base): @@ -28,6 +29,44 @@ class QBittorrentClient(Base): self._post('command/download', data={'urls': urls}) def get_torrents(self): + """Fetch all torrents + + :return: list of Torrent + """ r = self._get('json/torrents') return [Torrent.parse(self, x) for x in r] + + def get_torrent(self, hash, include_general=True, max_retries=5): + """Fetch details for torrent by info_hash. + + :param info_hash: Torrent info hash + :param include_general: Include general torrent properties + :param max_retries: Maximum number of retries to wait for torrent to appear in client + + :rtype: Torrent or None + """ + + torrent = None + retries = 0 + + # Try find torrent in client + while retries < max_retries: + # TODO this wouldn't be very efficient with large numbers of torrents on the client + torrents = dict([(t.hash, t) for t in self.get_torrents()]) + + if hash in torrents: + torrent = torrents[hash] + break + + retries += 1 + time.sleep(1) + + if torrent is None: + return None + + # Fetch general properties for torrent + if include_general: + torrent.update_general() + + return torrent diff --git a/libs/qbittorrent/torrent.py b/libs/qbittorrent/torrent.py index f7efdb5..a9ff51d 100644 --- a/libs/qbittorrent/torrent.py +++ b/libs/qbittorrent/torrent.py @@ -32,6 +32,10 @@ class Torrent(Base): self.seeds = None self.leechs = None + # General properties + self.comment = None + self.save_path = None + # # Commands # @@ -62,3 +66,16 @@ class Torrent(Base): def get_trackers(self): pass + + # + # Update torrent details + # + + def update_general(self): + r = self._get('json/propertiesGeneral/%s' % self.hash) + + if r: + self._fill(r) + return True + + return False