33from requests .auth import HTTPBasicAuth
44from ..torrent import Torrent
55from ..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
712class 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 })
0 commit comments