-
Notifications
You must be signed in to change notification settings - Fork 3
/
transmission_client.py
100 lines (76 loc) · 3.55 KB
/
transmission_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
try:
import simplejson as json
except ImportError:
import json
import urllib2
import sys
class TransmissionClientFailure(Exception): pass
class TransmissionClient(object):
rpcUrl = None
headers = {}
def __init__( self, rpcUrl='http://localhost:9091'):
""" try to do a stupid call to transmission via rpc """
try:
self.rpcUrl = rpcUrl
if not self.rpcUrl.endswith("/transmission/rpc"):
self.rpcUrl = '%s/transmission/rpc' % rpcUrl
req = urllib2.Request( self.rpcUrl , {}, self.headers)
response = urllib2.urlopen(req)
response = response.read()
if not response.find("no method name"):
raise TransmissionClientFailure, "Make sure your transmission-daemon is running %s" % e
except urllib2.HTTPError, e:
if e.code == 409:
self.headers['X-Transmission-Session-Id'] = e.info()['X-Transmission-Session-Id']
return self.__init__( self.rpcUrl )
else:
raise Exception('HTTPError: %s' % e.code )
except Exception, e:
raise TransmissionClientFailure, "Make sure your transmission-daemon is running %s" % e
def _rpc( self, method, params={} ):
""" generic rpc call to transmission """
try:
params['ids'] = int(params['ids'])
except:
pass
data = { 'method': method, 'arguments': params}
postdata = json.dumps(data)
try:
req = urllib2.Request( self.rpcUrl , postdata, self.headers)
response = urllib2.urlopen(req)
return json.loads(response.read())
except urllib2.HTTPError, e:
if e.code == 409:
self.headers['X-Transmission-Session-Id'] = e.info()['X-Transmission-Session-Id']
return self._rpc(method, params)
else:
raise Exception('HTTPError: %s' % e.code )
def sessionStats( self ):
return self._rpc( 'session-stats' )
def torrentGet( self, torrentIds=[], fields=[ 'id', 'name', 'totalSize', 'percentDone', 'rateDownload', 'rateUpload', 'files', 'status', 'peersConnected', 'peersSendingToUs', 'peersGettingFromUs', 'eta', 'uploadedEver', 'uploadRatio']):
if len(torrentIds) > 0:
return self._rpc( 'torrent-get', { 'ids': torrentIds, 'fields': fields } )
return self._rpc( 'torrent-get', { 'fields': fields } )
def torrentAdd( self, torrentFile, downloadDir='.' ):
return self._rpc( 'torrent-add', { 'filename': torrentFile, 'download-dir': downloadDir } )
def torrentRemove( self, torrents=None, files=False ):
if files:
if torrents:
return self._rpc( 'torrent-remove', { 'ids': torrents, 'delete-local-data': 'true' } )
else:
return self._rpc( 'torrent-remove', { 'delete-local-data': 'true' } )
if torrents:
return self._rpc( 'torrent-remove', { 'ids': torrents } )
else:
return self._rpc( 'torrent-remove', { } )
def torrentStart( self, torrents=None ):
if torrents:
return self._rpc( 'torrent-start', { 'ids': torrents } )
else:
return self._rpc( 'torrent-start', {} )
def torrentStop( self, torrents=None ):
if torrents:
return self._rpc( 'torrent-stop', { 'ids': torrents } )
else:
return self._rpc( 'torrent-stop', {} )