forked from CouchPotato/CouchPotatoServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
72 lines (50 loc) · 1.96 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from qbittorrent.base import Base
from qbittorrent.torrent import Torrent
from requests import Session
from requests.auth import HTTPDigestAuth
import time
class QBittorrentClient(Base):
def __init__(self, url, username=None, password=None):
super(QBittorrentClient, self).__init__(url, Session())
if username and password:
self._session.auth = HTTPDigestAuth(username, password)
def test_connection(self):
r = self._get(response_type='response')
return r.status_code == 200
def add_file(self, file):
self._post('command/upload', files={'torrent': file})
def add_url(self, urls):
if type(urls) is not list:
urls = [urls]
urls = '%0A'.join(urls)
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