Skip to content

Commit 845cc7a

Browse files
Added test cases for tasks and clients
1 parent 751c7b1 commit 845cc7a

37 files changed

Lines changed: 295 additions & 94 deletions

autoremovetorrents/client/base/clientbase.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

autoremovetorrents/client/base/torrentstatus.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

autoremovetorrents/client/qbittorrent.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import time
55
from ..torrent import Torrent
66
from ..torrentstatus import TorrentStatus
7-
from ..exception.loginfailed import LoginFailed
7+
from ..exception.loginfailure import LoginFailure
8+
from ..exception.deletionfailure import DeletionFailure
9+
from ..exception.connectionfailure import ConnectionFailure
810

911
class qBittorrent(object):
1012
def __init__(self, host):
@@ -23,15 +25,19 @@ def __init__(self, host):
2325

2426
# Login to qBittorrent
2527
def login(self, username, password):
26-
request = requests.post(self._host+'/login', data={'username':username, 'password':password})
27-
self._logger.info(request.text)
28+
try:
29+
request = requests.post(self._host+'/login', data={'username':username, 'password':password})
30+
self._logger.info(request.text)
31+
except Exception as exc:
32+
raise ConnectionFailure(str(exc))
33+
2834
if request.status_code == 200:
2935
if request.text == 'Ok.': # Success
3036
self._cookies = request.cookies
3137
else:
32-
raise LoginFailed(request.text)
38+
raise LoginFailure(request.text)
3339
else:
34-
raise LoginFailed('The server returned HTTP %d.' % request.status_code)
40+
raise LoginFailure('The server returned HTTP %d.' % request.status_code)
3541

3642
# Get qBittorrent Version
3743
def version(self):

autoremovetorrents/client/transmission.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
from requests.auth import HTTPBasicAuth
44
from ..torrent import Torrent
55
from ..torrentstatus import TorrentStatus
6+
from ..exception.connectionfailure import ConnectionFailure
7+
from ..exception.deletionfailure import DeletionFailure
8+
from ..exception.loginfailure import LoginFailure
9+
from ..exception.nosuchclient import NoSuchClient
10+
from ..exception.remotefailure import RemoteFailure
611

712
class Transmission(object):
813
def __init__(self, host):
@@ -28,22 +33,28 @@ def _make_transmission_request(self, method, arguments=None):
2833
while retry > 0:
2934
retry -= 1
3035
# Make request
31-
request = requests.post(self._host+'/transmission/rpc',
32-
json={'method':method, 'arguments':arguments, 'tag':self._request_id},
33-
headers={'X-Transmission-Session-Id': self._session_id},
34-
auth=HTTPBasicAuth(self._username, self._password))
35-
self._request_id += 1
36+
try:
37+
request = requests.post(self._host+'/transmission/rpc',
38+
json={'method':method, 'arguments':arguments, 'tag':self._request_id},
39+
headers={'X-Transmission-Session-Id': self._session_id},
40+
auth=HTTPBasicAuth(self._username, self._password))
41+
self._request_id += 1
42+
except Exception as exc:
43+
raise ConnectionFailure(str(exc))
44+
3645
if request.status_code == 409: # Save Session ID and retry
3746
self._session_id = request.headers['X-Transmission-Session-Id']
3847
elif request.status_code == 401: # Unauthorized user
39-
raise RuntimeError('Unauthorized user.')
40-
else:
48+
raise LoginFailure('Unauthorized user.')
49+
elif request.status_code == 200:
4150
result = request.json()
4251
if result['result'] == 'success': # Success
4352
return result['arguments']
4453
else:
45-
raise RuntimeError(result['result'])
46-
raise RuntimeError('HTTP error on requesting '+method)
54+
raise RemoteFailure(result['result'])
55+
raise RemoteFailure('The server responsed %d on method %s.' \
56+
% (request.status_code, method)
57+
)
4758

4859
# Get Transmission Version
4960
def version(self):
@@ -64,7 +75,7 @@ def torrent_properties(self, torrent_hash):
6475
'fields': ['hashString', 'name', 'trackers', 'status', 'totalSize', 'uploadRatio', 'uploadedEver', 'addedDate', 'secondsSeeding']}
6576
)
6677
if len(result['torrents']) == 0: # No such torrent
67-
raise RuntimeError('No such torrent.')
78+
raise NoSuchClient("No such torrent of hash '%s'." % torrent_hash)
6879
torrent = result['torrents'][0]
6980
# Judge status
7081
status_list = [
@@ -87,9 +98,9 @@ def torrent_properties(self, torrent_hash):
8798
# Remove Torrent
8899
def remove_torrent(self, torrent_hash):
89100
self._make_transmission_request('torrent-remove',
90-
{'ids':[torrent_hash], 'delete-local-data':False})
101+
{'ids':[torrent_hash], 'delete-local-data':False})
91102

92103
# Remove Data
93104
def remove_data(self, torrent_hash):
94105
self._make_transmission_request('torrent-remove',
95-
{'ids':[torrent_hash], 'delete-local-data':True})
106+
{'ids':[torrent_hash], 'delete-local-data':True})

autoremovetorrents/client/utorrent.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
from requests.auth import HTTPBasicAuth
66
import requests
77
from ..torrent import Torrent
8+
from autoremovetorrents.exception.connectionfailure import ConnectionFailure
9+
from autoremovetorrents.exception.deletionfailure import DeletionFailure
10+
from autoremovetorrents.exception.loginfailure import LoginFailure
11+
from autoremovetorrents.exception.nosuchtorrent import NoSuchTorrent
12+
from autoremovetorrents.exception.remotefailure import RemoteFailure
813
from ..torrentstatus import TorrentStatus
914

1015
class uTorrent(object):
@@ -27,13 +32,21 @@ def login(self, username, password):
2732
# HTTP Authorization
2833
self._auth = HTTPBasicAuth(username, password)
2934
# Requests Token
30-
request = requests.get(self._host+'/gui/token.html', auth=self._auth)
35+
try:
36+
request = requests.get(self._host+'/gui/token.html', auth=self._auth)
37+
except Exception as exc:
38+
raise ConnectionFailure(str(exc))
39+
3140
pattern = re.compile('<[^>]+>')
3241
text = request.text
33-
if request.status_code != 200: # Error
34-
raise RuntimeError(text)
35-
self._token = pattern.sub('', text)
36-
self._cookies = request.cookies
42+
if request.status_code == 200:
43+
self._token = pattern.sub('', text)
44+
self._cookies = request.cookies
45+
elif request.status_code == 401: # Error
46+
raise LoginFailure('401 Unauthorized.')
47+
else:
48+
raise RemoteFailure('The server responsed %d.' \
49+
% request.status_code)
3750

3851
# Get uTorrent Version
3952
def version(self):
@@ -49,7 +62,7 @@ def torrents_list(self):
4962
cookies=self._cookies, auth=self._auth)
5063
request.encoding = 'utf-8'
5164
if request.status_code != 200: # Error
52-
raise RuntimeError(request.text)
65+
raise RemoteFailure('The server reponsed %s.' % request.text)
5366
result = request.json()
5467
self._torrents_list_cache = result
5568
self._refresh_time = time.time()
@@ -95,7 +108,7 @@ def torrent_properties(self, torrent_hash):
95108
torrent[0], torrent[2], torrent[11], trackers, status, torrent[3], torrent[7]/1000,
96109
torrent[6], sys.maxsize, -1)
97110
# Not Found
98-
raise RuntimeError('No such torrent.')
111+
raise NoSuchTorrent('No such torrent.')
99112

100113
# Remove Torrent
101114
def remove_torrent(self, torrent_hash):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ConnectionFailure(RuntimeError):
2+
def __init__(self, arg):
3+
self.args = arg
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class DeletionFailure(RuntimeError):
2+
def __init__(self, arg):
3+
self.args = arg
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#-*- coding:utf-8 -*-
22

3-
class LoginFailed(RuntimeError):
3+
class LoginFailure(RuntimeError):
44
def __init__(self, arg):
55
self.args = arg
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class NoSuchTorrent(RuntimeError):
2+
def __init__(self, arg):
3+
self.args = arg
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class RemoteFailure(RuntimeError):
2+
def __init__(self, arg):
3+
self.args = arg

0 commit comments

Comments
 (0)