forked from joshuajonah/boxee-torrentui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utorrent_client.py
executable file
·372 lines (288 loc) · 11.8 KB
/
utorrent_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# uTorrent.py version 0.1.1 ALPHA
# Copyright (C) 2006-2007 Rob Crowther <weilawei@gmail.com>
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import logging, sys, socket, simplejson
from base64 import b64encode
from httplib import *
from urllib import quote
# date/timestamp [LEVEL] error message
logging.basicConfig(datefmt='%d %b %Y %H:%M:%S',
format='%(asctime)s [%(levelname)s] %(message)s')
# UTORRENT CONSTANTS
# modify these, fuck things up
UT_DEBUG = True
# file priorities
UT_FILE_PRIO_SKIP = r'0'
UT_FILE_PRIO_LOW = r'1'
UT_FILE_PRIO_NORMAL = r'2'
UT_FILE_PRIO_HIGH = r'3'
# torrent states
UT_TORRENT_STATE_START = 0x00
UT_TORRENT_STATE_FORCESTART = 0x01
UT_TORRENT_STATE_PAUSE = 0x02
UT_TORRENT_STATE_STOP = 0x03
# individual torrent properties
UT_TORRENT_DETAIL_HASH = 0
UT_TORRENT_DETAIL_TRACKERS = 1
UT_TORRENT_DETAIL_ULRATE = 2
UT_TORRENT_DETAIL_DLRATE = 3
UT_TORRENT_DETAIL_SUPERSEED = 4
UT_TORRENT_DETAIL_DHT = 5
UT_TORRENT_DETAIL_PEX = 6
UT_TORRENT_DETAIL_SEED_OVERRIDE = 7
UT_TORRENT_DETAIL_SEED_RATIO = 8
UT_TORRENT_DETAIL_SEED_TIME = 9
UT_TORRENT_DETAIL_ULSLOTS = 10
# torrent info/stats
UT_TORRENT_PROP_HASH = 0
UT_TORRENT_PROP_NAME = 2
UT_TORRENT_PROP_LABEL = 11
UT_TORRENT_PROP_STATE = 1
UT_TORRENT_STAT_BYTES_SIZE = 3
UT_TORRENT_STAT_BYTES_LEFT = 18
UT_TORRENT_STAT_BYTES_RECV = 5
UT_TORRENT_STAT_BYTES_SENT = 6
UT_TORRENT_STAT_SPEED_UP = 8
UT_TORRENT_STAT_SPEED_DOWN = 9
UT_TORRENT_STAT_P1000_DONE = 4
UT_TORRENT_STAT_ETA = 10
UT_TORRENT_STAT_AVAILABLE = 16
UT_TORRENT_STAT_QUEUE_POS = 17
UT_TORRENT_STAT_RATIO = 7
UT_TORRENT_STAT_SEED_AVAIL = 15
UT_TORRENT_STAT_PEER_AVAIL = 13
UT_TORRENT_STAT_SEED_CONN = 14
UT_TORRENT_STAT_PEER_CONN = 12
# uTorrent
#
# Provides a handle with fine grained torrent state
# and file priority methods
class uTorrent(HTTPConnection):
username = None
password = None
identity = None
# will be happy as long as you feed it valid uTorrent WebUI details
def __init__(self, host='localhost', port='8080', username='default', password='default'):
try:
HTTPConnection.__init__(self, host, int(port))
self.connect()
except socket.error, exception:
logging.critical(exception)
logging.shutdown()
self.username = username
self.password = password
# creates an HTTP Basic Authentication token
def webui_identity(self):
if (self.identity is None):
self.identity = self.username + ':' + self.password
self.identity = b64encode(self.identity)
return self.identity
# creates and fires off an HTTP request
# all webui_ methods return a python object
def webui_action(self, selector, method=r'GET', headers=None, data=None):
self.putrequest(method, selector)
self.putheader('Authorization', 'Basic ' + self.webui_identity())
if (headers is not None):
for (name, value) in headers.items():
self.putheader(name, value)
self.endheaders()
if (method == r'POST'):
self.send(str(data))
webui_response = self.getresponse()
if (webui_response.status == 401):
logging.error('401 Unauthorized Access')
return None
return simplejson.loads(webui_response.read())
# gets torrent properties
def webui_get_props(self, torrent_hash):
return self.webui_action(r'/gui/?action=getprops&hash=' + torrent_hash)['props']
# sets torrent properties
def webui_set_prop(self, torrent_hash, setting, value):
setting = quote(setting)
value = quote(value)
return self.webui_action(r'/gui/?action=setsetting&s=' + setting + r'&v=' + value + r'&hash=' + torrent_hash)
# sets a uTorrent setting
def webui_set(self, setting, value):
setting = quote(setting)
value = quote(value)
return self.webui_action(r'/gui/?action=setsetting&s=' + setting + r'&v=' + value)
# gets uTorrent settings
def webui_get(self):
return self.webui_action(r'/gui/?action=getsettings')['settings']
# adds a torrent via url
# you need to check webui_ls() again *after* you get this result
# otherwise, the torrent might not show up and you won't know
# if it was successfully added.
def webui_add_url(self, torrent_url):
return self.webui_action(r'/gui/?action=add-url&s=' + quote(torrent_url) + r'&list=1')
# adds a torrent via POST
def webui_add_file(self, torrent_file):
CRLF = '\r\n'
method = r'POST'
boundary = r'---------------------------22385145923439'
headers = {r'Content-Type': r'multipart/form-data; boundary=' + boundary}
data = ''
try:
torrent = open(torrent_file, 'rb')
torrent = torrent.read()
except IOError:
logging.error('Torrent I/O Error')
return None
data += "--%s%s" % (boundary, CRLF)
data += "Content-Disposition: form-data; name=\"torrent_file\"; filename=\"%s\"%s" % (torrent_file, CRLF)
data += "Content-Type: application/x-bittorrent%s" % CRLF
data += "%s" % CRLF
data += torrent + CRLF
data += "--%s--%s" % (boundary, CRLF)
headers['Content-Length'] = str(len(data))
return self.webui_action(r'/gui/?action=add-file', method=method, headers=headers, data=data)
# removes a torrent
def webui_remove(self, torrent_hash):
return self.webui_action(r'/gui/?action=remove&hash=' + torrent_hash)
# removes a torrent and data
def webui_remove_data(self, torrent_hash):
return self.webui_action(r'/gui/?action=removedata&hash=' + torrent_hash)
# returns a giant listing of uTorrentness
def webui_ls(self):
return self.webui_action(r'/gui/?list=1')['torrents']
# returns a giant listing of uTorrentness files for a given torrent
def webui_ls_files(self, torrent_hash):
return self.webui_action(r'/gui/?action=getfiles&hash=' + torrent_hash)
# starts a torrent
def webui_start_torrent(self, torrent_hash):
return self.webui_action(r'/gui/?action=start&hash=' + torrent_hash + r'&list=1')
# force starts a torrent
# don't ever do this. please. this is for the sake of completeness.
def webui_forcestart_torrent(self, torrent_hash):
return self.webui_action(r'/gui/?action=forcestart&hash=' + torrent_hash + r'&list=1')
# pause a torrent
def webui_pause_torrent(self, torrent_hash):
return self.webui_action(r'/gui/?action=pause&hash=' + torrent_hash + r'&list=1')
# stop a torrent
def webui_stop_torrent(self, torrent_hash):
return self.webui_action(r'/gui/?action=stop&hash=' + torrent_hash + r'&list=1')
# set priority on a list of files
def webui_prio_file(self, torrent_hash, torrent_files, torrent_file_prio):
webui_cmd_prio = r'/gui/?action=setprio&hash='
webui_cmd_prio += torrent_hash
webui_cmd_prio += r'&p='
webui_cmd_prio += torrent_file_prio
for torrent_file_idx in torrent_files:
webui_cmd_prio += r'&f='
webui_cmd_prio += torrent_file_idx
return self.webui_action(webui_cmd_prio)
# returns a dictionary of torrent names and hashes
def uls_torrents(self):
raw_torrent_list = self.webui_ls()
torrent_list = {}
for torrent in raw_torrent_list:
torrent_list[torrent[UT_TORRENT_PROP_NAME]] = torrent[UT_TORRENT_PROP_HASH]
return torrent_list
# returns a dictionary of file names mapping array of indices and parent torrent hashes, file size (in bytes),
# downloaded (in bytes) and priority
# ex. {'fileb.txt': (1, IAMABIGASSHASHFORATORRENT), 'filea.dat': (0, IAMABIGASSHASHFORATORRENT)}
def uls_files(self, torrent_name=None, torrent_hash=None):
if ((torrent_name is None) and (torrent_hash is None)):
logging.error('Specify torrent_name or torrent_hash')
return None
# faster, will use this if possible
if (torrent_hash is not None):
raw_file_list = self.webui_ls_files(torrent_hash)['files'][1:]
# slow since we need to look up the hash
else:
torrent_hash = self.uls_torrents()[torrent_name]
raw_file_list = self.webui_ls_files(torrent_hash)['files'][1:]
file_list = {}
i = 0
for filename in raw_file_list[0]:
file_list[filename[0]] = (i, torrent_hash,filename[1],filename[2],filename[3])
i += 1
return file_list
# sets the current state of a list of torrents
def uset_torrents_state(self, torrent_state, torrent_list_name=None, torrent_list_hash=None):
if ((torrent_list_name is None) and (torrent_list_hash is None)):
logging.error('Specify torrent_list_name or torrent_list_hash')
return None
if (torrent_list_hash is None):
current_torrents = self.uls_torrents()
if (torrent_state == UT_TORRENT_STATE_STOP):
if (torrent_list_hash is not None):
for torrent in torrent_list_hash:
self.webui_stop_torrent(torrent)
else:
for torrent in torrent_list_name:
self.webui_stop_torrent(current_torrents[torrent])
return True
elif (torrent_state == UT_TORRENT_STATE_START):
if (torrent_list_hash is not None):
for torrent in torrent_list_hash:
self.webui_start_torrent(torrent)
else:
for torrent in torrent_list_name:
self.webui_start_torrent(current_torrents[torrent])
return True
elif (torrent_state == UT_TORRENT_STATE_PAUSE):
if (torrent_list_hash is not None):
for torrent in torrent_list_hash:
self.webui_pause_torrent(torrent)
else:
for torrent in torrent_list_name:
self.webui_pause_torrent(current_torrents[torrent])
return True
elif (torrent_state == UT_TORRENT_STATE_FORCESTART):
if (torrent_list_hash is not None):
for torrent in torrent_list_hash:
self.webui_forcestart_torrent(torrent)
else:
for torrent in torrent_list_name:
self.webui_forcestart_torrent(current_torrents[torrent])
return True
else:
return False
# sets the current priority of a list of files
def uprio_files(self, file_list, file_prio, torrent_name=None, torrent_hash=None):
if ((torrent_name is None) and (torrent_hash is None)):
logging.error('Specify torrent_name or torrent_hash')
return None
# whee, faster
if (torrent_hash is not None):
current_files = self.uls_files(torrent_hash=torrent_hash)
# slow since we need to look up the hash
else:
torrent_list = self.uls_torrents()
current_files = self.uls_files(torrent_name=torrent_name)
file_idx_list = []
for filename in file_list:
file_idx_list.append(str(current_files[filename][0]))
# whee, faster
if (torrent_hash is not None):
for filename in file_list:
self.webui_prio_file(torrent_hash, file_idx_list, file_prio)
# ew, slower
else:
for filename in file_list:
self.webui_prio_file(torrent_list[torrent_name], file_idx_list, file_prio)
# the sandbox
# TODO: make this an interactive prompt
if (__name__ == '__main__'):
from code import interact
interact()
uTorrent_handle = uTorrent(port='44800', username='admin', password='passy')
uTorrent_handle.uprio_files( [r'Brand_New-The_Devil_And_God_Are_Raging_Inside_Me-(With_UK_Bonus_Track)-2006-h8me.rar'],
UT_FILE_PRIO_HIGH,
torrent_hash = r'B1A6CCEEA6F60EF82901B205766535D8F1C68B4E')