/
mt_torrent_agent.py
439 lines (367 loc) · 15.7 KB
/
mt_torrent_agent.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# mtorrent
# Copyright (c) 2012 Martin Trojer <martin.trojer@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Agent to serialize all communication with libtorrent
import mt_logger as L
import mt_utils as U
import threading as T
import Queue as Q
import libtorrent as LT
import pdb as P
class TorrentAgent(T.Thread):
def __init__(self, config, logger):
T.Thread.__init__(self)
self.c = config
self.q = Q.Queue()
self.l = logger
self.handle_by_name = {}
self.highlight = None
self.session = LT.session()
settings = self.session.settings()
settings.user_agent = self.c["version_short"]
settings.auto_upload_slots_rate_based = self.c["auto_upload_slots"]
settings.announce_to_all_trackers = self.c["ann_all_trackers"]
settings.optimize_hashing_for_speed = self.c["optimize_hash_speed"]
settings.disk_cache_algorithm = self.c["disk_cache_algo"]
settings.use_dht_as_fallback = self.c["dht_as_fallback"]
self.session.set_settings(settings)
# Load session state
try:
f = open(self.c["session_file"],'rb')
self.l.log("TorrentAgent:loading session state")
self.session.load_state(LT.bdecode(f.read()))
f.close()
except:
self.l.log("TorrentAgent:failed to read session state " + self.c["session_file"], L.WARN)
# Start extensions
self.session.add_extension(LT.create_ut_metadata_plugin)
self.session.add_extension(LT.create_ut_pex_plugin)
self.session.add_extension(LT.create_smart_ban_plugin)
# Start DHT and UPNP
if self.c["use_dht"]:
self.session.add_dht_router(self.c["dht_router1"][0], self.c["dht_router1"][1])
self.session.add_dht_router(self.c["dht_router2"][0], self.c["dht_router2"][1])
self.session.add_dht_router(self.c["dht_router3"][0], self.c["dht_router3"][1])
self.session.start_dht()
self.session.listen_on(self.c["listen_on"][0], self.c["listen_on"][1])
if self.c["use_upnp"]:
self.session.start_upnp()
def __teardown(self):
self.l.log("TorrentAgent:stopping")
self.session.pause()
resume_ctr = 0
# Save session state for individual torrents
for h, f in self.handle_by_name.values():
if h.is_paused(): continue
if not h.is_valid(): continue
if not h.has_metadata(): continue
self.l.log("TorrentAgent:saving resume data for " + h.name(), L.DBG)
h.save_resume_data()
resume_ctr += 1
self.l.log("TorrentAgent:waiting for resume data...")
while resume_ctr > 0:
a = self.session.wait_for_alert(30 * 1000) # 30 secs
if a == None:
self.l.log(
"TorrentAgent:aborting with outstanding torrents to save resume data",
L.WARN)
break
dummy = self.session.pop_alert()
if a.what() == "save resume data complete":
filename = self.c["session_path"]+"/"+str(a.handle.info_hash())+".resume"
try:
f = open(filename,'wb')
self.l.log("TorrentAgent:writing resume data to " + filename, L.DBG)
self.l.log_ui("Writing resume data to " + filename)
f.write(LT.bencode(a.resume_data))
f.close()
except:
self.l.log("TorrentAgent:error saving resume data " + filename, L.ERR)
elif a.what() == "save resume data failed":
self.l.log_ui("No resume data for " + a.handle.name())
self.l.log(
"TorrentAgent:error getting resume data for " + a.handle.name() +
" " + a.what(), L.WARN)
else:
self.l.log("TorrentAgent:unknown alert received " + a.what(), L.WARN)
continue
resume_ctr = resume_ctr - 1
# Save state for the entire session
self.l.log("TorrentAgent:saving session state", L.DBG)
try:
f = open(self.c["session_file"],'wb')
f.write(LT.bencode(self.session.save_state()))
f.close()
except:
self.l.log("TorrentAgent:error save session file " + self.c["session_file"], L.ERR)
def __check_dups(self, name):
return filter(lambda n: n == name, self.handle_by_name.keys()) != []
def __get_resume_data(self, hash):
try:
resume_name = self.c["session_path"]+"/"+hash+".resume"
self.l.log("TorrentAgent:reading resume data from " + resume_name, L.DBG)
f = open(resume_name, 'rb')
data = f.read()
f.close()
return LT.bdecode(data)
except:
self.l.log("TorrentAgent:error reading resume data from " + resume_name, L.DBG)
return ""
def __setup_handle(self, h):
h.set_max_connections(self.c["max_connections"])
h.set_max_uploads(self.c["max_uploads"])
h.set_ratio(self.c["ratio"])
h.set_upload_limit(self.c["upload_limit"])
h.set_download_limit(self.c["download_limit"])
h.resolve_countries(self.c["resolve_countries"])
def __add_torrent(self, filename):
self.l.log("TorrentAgent:adding torrent " + filename)
if self.__check_dups(filename):
self.l.log("TorrentAgent:skipping, already added " + filename)
return
torrent_data = None
try:
f = open(filename, 'rb')
torrent_data = LT.bdecode(f.read())
f.close()
except:
self.l.log("TorrentAgent:error reading from " + filename, L.ERR)
return
try:
info = LT.torrent_info(torrent_data)
except:
self.l.log("TorrentAgent:error adding torrent, corrupt file? " + filename, L.ERR)
return
resume_data = self.__get_resume_data(str(info.info_hash()))
h = self.session.add_torrent(info, self.c["save_path"],
resume_data=resume_data,
storage_mode=self.c["storage_mode"])
self.__setup_handle(h)
self.handle_by_name[filename] = (h, False)
def __scan_and_add(self, path):
self.l.log("TorrentAgent:scanning path " + path, L.DBG)
files = {"torrent":[], "magnet": []}
try:
files = U.scan_dir(path)
except:
self.l.log("TorrentAgent:failed to scan dir " + path, L.WARN)
for t in files["torrent"]:
if not self.__check_dups(t):
self.__add_torrent(t)
for m in files["magnet"]:
try:
f = open(m, "rb")
uri = f.read()
f.close()
if not self.__check_dups(uri):
info_hash = m.split(".magnet")[0]
self.__add_magnet(uri, info_hash)
except:
self.l.log("TorrentAgent:error reading from " + m, L.ERR)
def __add_magnet(self, uri, info_hash):
self.l.log("TorrentAgent:adding magnet " + uri)
if self.__check_dups(uri):
self.l.log("TorrentAgent:skipping, already added " + uri)
return
resume_data = self.__get_resume_data(info_hash)
params = {"save_path":self.c["save_path"], "resume_data":resume_data,
"storage_mode":self.c["storage_mode"],
"paused":self.c["start_as_paused"],
"duplicate_is_error":self.c["dup_is_error"],
"auto_managed":self.c["auto_managed"]}
try:
h = LT.add_magnet_uri(self.session, uri, params)
except:
self.l.log("TorrentAgent:error adding magnet, malformed URI? " + uri, L.ERR)
return
try:
fname = self.c["watch_path"] + "/" + str(h.info_hash()) + ".magnet"
self.l.log("TorrentAgent:writing magnet restart file " + fname, L.DBG)
f = open(fname, "wb")
f.write(uri)
f.close()
except:
self.l.log("TorrentAgent:error writing magnet restart file " + fname, L.ERR)
self.__setup_handle(h)
self.handle_by_name[uri] = (h, True)
def __remove_torrent(self, name):
self.l.log("TorrentAgent:removing torrent " + name)
if not self.handle_by_name.has_key(name):
self.l.log("TorrentAgent:error removing torrent, not in current session " +
name, L.ERR)
return None
h, is_magnet = self.handle_by_name.pop(name)
self.highlight = None
# remove any resume file
U.remove_file(self.c["session_path"] + "/" + str(h.info_hash()) + ".resume", self.l)
# remove the source file
if is_magnet:
name = self.c["watch_path"] + "/" + str(h.info_hash()) + ".magnet"
U.remove_file(name, self.l)
# remove the torrent from the session
try:
self.session.remove_torrent(h)
except:
self.l.log("TorrentAgent:internal error remove torrent " + name, L.ERR)
return None
return name
def __remove_highlighted(self):
n = filter(lambda k: self.handle_by_name[k] == self.highlight, self.handle_by_name)
if len(n) == 1:
return self.__remove_torrent(n[0])
def __remove_all(self):
res = []
for k in self.handle_by_name.keys():
res.append(self.__remove_torrent(k))
return res
def __toggle_pause(self, name):
self.l.log("TorrentAgent:toggle_pause " + name)
if not self.handle_by_name.has_key(name):
self.l.log("TorrentAgent:torret not in session " + name, L.ERR)
return None
else:
h, im = self.handle_by_name[name]
if h.is_paused():
h.resume()
else:
h.pause()
return name
def __toggle_highlighted(self):
n = filter(lambda k: self.handle_by_name[k] == self.highlight, self.handle_by_name)
if len(n) == 1:
return self.__toggle_pause(n[0])
def __toggle_all(self):
keys = self.handle_by_name.keys()
for k in keys:
self.__toggle_pause(k)
return keys
def __move_highlight(self, delta):
vs = self.handle_by_name.values()
if len(vs) == 0:
self.highlight = None
elif self.highlight == None:
self.highlight = vs[0]
else:
idx = vs.index(self.highlight)
new_idx = (idx + delta) % len(vs)
self.highlight = vs[new_idx]
self.l.log("TorrentAgent:highlight is now " + str(self.highlight), L.DBG)
def __get_state(self):
res = []
i = 0
for k in self.handle_by_name.keys():
h, is_magnet = self.handle_by_name[k]
status = h.status()
size = 0
try:
# this sometimes fails (for magnets without meta data)
size = h.get_torrent_info().total_size()
except:
pass
states = ['queued for checking', 'checking files', 'downloading metadata',
'downloading', 'finished', 'seeding', 'allocating',
'checking resume data']
state_str = ""
try:
state_str = states[status.state]
except:
self.l.log("TorrentAgent:unknown state for " + k, L.WARN)
state_str = "unknown"
res.append({"name" : h.name(),
"source" : k,
"hash" : str(h.info_hash()),
"size" : size,
"status" : state_str,
"progress" : status.progress * 100,
"down_rate" : status.download_rate,
"up_rate" : status.upload_rate,
"seeds" : status.num_seeds,
"seeds_total" : status.list_seeds,
"peers" : status.num_peers,
"peers_total" : status.list_peers,
"is_paused" : h.is_paused(),
"is_highlighted": (h, is_magnet) == self.highlight,
"is_magnet": is_magnet})
return res
def run(self):
self.l.log("TorrentAgent:starting")
running = True
while running:
item = self.q.get()
self.l.log("TorrentAgent:req:" + item[0], L.DBG)
msg = item[0]
if msg == "add_torrent":
self.__add_torrent(item[1])
elif msg == "scan_and_add":
self.__scan_and_add(item[1])
elif msg == "add_magnet":
self.__add_magnet(item[1], item[2])
elif msg == "remove_torrent":
self.__remove_torrent(item[1])
elif msg == "remove_highlighted":
item[1].put(self.__remove_highlighted())
elif msg == "remove_all":
item[1].put(self.__remove_all())
elif msg == "toggle_pause":
self.__toggle_pause(item[1])
elif msg == "toggle_highlighted":
item[1].put(self.__toggle_highlighted())
elif msg == "toggle_all":
item[1].put(self.__toggle_all())
elif msg == "move_highlight":
self.__move_highlight(item[1])
elif msg == "get_state":
item[1].put(self.__get_state())
elif msg == "stop":
self.__teardown()
running = False
item[1].put(True)
else:
self.l.log("TorrentAgent:unknown message received " + str(msg), L.ERR)
self.q.task_done()
# Interface functions, called from other threads
def __action_with_res(self, action):
q = Q.Queue()
self.q.put([action, q])
r = q.get()
q.task_done()
return r
def add_torrent(self, fname):
self.q.put(["add_torrent", fname])
def scan_and_add(self, path):
self.q.put(["scan_and_add", path])
def add_magnet(self, uri, info_hash):
self.q.put(["add_magnet", uri, info_hash])
def remove_torrent(self, name):
self.q.put(["remove_torrent", name])
def remove_highlighted(self):
return self.__action_with_res("remove_highlighted")
def remove_all(self):
return self.__action_with_res("remove_all")
def toggle_pause(self, name):
self.q.put(["toggle_pause", name])
def toggle_highlighted(self):
return self.__action_with_res("toggle_highlighted")
def toggle_all(self):
return self.__action_with_res("toggle_all")
def highlight_up(self):
self.q.put(["move_highlight", -1])
def highlight_down(self):
self.q.put(["move_highlight", 1])
def get_state(self):
return self.__action_with_res("get_state")
def stop(self):
#blocks until teardown is complete
return self.__action_with_res("stop")