forked from TouchTone/pyjsit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsit_manager.py
438 lines (283 loc) · 13.3 KB
/
jsit_manager.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
#!/usr/bin/python
# Manager class to handle torrents and downloads
import time, re, glob
import jsit, aria
from log import *
from tools import *
import preferences
pref = preferences.pref
# Helpers
def enum(*sequential, **named):
if isinstance(sequential[0], tuple):
enums = dict(zip([s[0] for s in sequential], range(len(sequential))), **named)
reverse = dict((value, key) for key, value in enums.iteritems())
enums['attribs'] = dict(zip([s[0] for s in sequential], [s[1:] for s in sequential]))
enums['enum_attribs'] = [s[1:] for s in sequential]
else:
enums = dict(zip(sequential, range(len(sequential))), **named)
reverse = dict((value, key) for key, value in enums.iteritems())
enums['reverse_mapping'] = reverse
enums['count'] = len(enums)
return type('Enum', (), enums)
# Handler class for single torrents
TStates = enum("TSTARTED", "TSTOPPED", "TFINISHED", "DSTARTED", "DSTOPPED", "DFINISHED")
class Torrent(object):
def __init__(self, mgr, fname = None, url = None, jsittorrent = None, maximum_ratio = None, basedir = ".",
unquoteNames = True, interpretDirectories = True, autoStartDownload = True, addTorrentNameDir = True):
self._mgr = mgr
self._aria = None
self._torrent = None
self.hash = None
if jsittorrent:
self._torrent = jsittorrent
elif ( fname == None and url == None ) or ( fname != None and url != None ):
log(ERROR, "Mgr:Torrent: need to have either filename or url!\n")
raise Exception("Mgr:Torrent: need to have either filename or url!")
else:
if fname:
self._torrent = self._mgr._jsit.addTorrentFile(fname, maximum_ratio = maximum_ratio)
elif url:
self._torrent = self._mgr._jsit.addTorrentURL(url, maximum_ratio = maximum_ratio)
else:
raise Exception("Torrent: need something to base myself on!")
if not self._torrent:
raise ValueError("Torrent: failed to create JSIT torrent!")
self.hash = self._torrent._hash
# Save download-related options for later
self.autoStartDownload = autoStartDownload
self.addTorrentNameDir = addTorrentNameDir
self.basedir = basedir
self.unquoteNames = unquoteNames
self.interpretDirectories = interpretDirectories
# State vars
self.percentage = 0
def __repr__(self):
if self._torrent:
return "MTorrent(%r (%r))"% (self.name, self.hash)
else:
return "MTorrent(<unnamed> (%r))"% (self.hash)
# Forwarded attributes from _torrent or _aria
# From jsit.Torrent
def set_label(self, l):
self._torrent.label = l
def set_maximum_ratio(self, r):
self._torrent.maximum_ratio = r
name = property(lambda s: s._torrent.name)
size = property(lambda s: s._torrent.size)
label = property(lambda s: s._torrent.label, set_label)
private = property(lambda s: s._torrent.private)
maximum_ratio = property(lambda x: s._torrent.maximum_ratio, set_maximum_ratio)
# From aria.Download
# Other properties
@property
def hasFinished(self):
return self.percentage == 100
@property
def status(self):
if not self._aria:
return self._torrent.status
# Worker Methods
def start(self):
log(DEBUG)
self._torrent.start()
if self._aria:
self._aria.start()
def stop(self):
log(DEBUG)
self._torrent.stop()
if self._aria:
self._aria.stop()
def delete(self):
log(DEBUG)
self._torrent.delete()
if self._aria:
self._aria.delete()
def startDownload(self):
if not self._torrent.hasFinished:
log(WARNING, "can't start download, torrent not finished!\n")
return
log(INFO)
if not self._aria:
base = self.basedir
if self.addTorrentNameDir and len(self._torrent.files) > 1:
base = os.path.join(self.basedir, self._torrent.name.replace('/', '_'))
self._aria = aria.Download(self._mgr._aria, [f.url for f in self._torrent.files], fullsize = self._torrent.size,
basedir = base, unquoteNames = self.unquoteNames, startPaused = False,
interpretDirectories = self.interpretDirectories, torrentdata = self._torrent.torrent)
else:
self._aria.start()
def restartDownload(self):
if not self._torrent.hasFinished:
debug(WARNING, "can't restart download, torrent not finished!\n")
return
log(INFO, "Restarting download for %s.\n" % self.name)
if self._aria:
self._aria.delete()
self._aria = None
self.startDownload()
def recheckDownload(self):
if not self._aria:
base = self.basedir
if self.addTorrentNameDir:
base = os.path.join(self.basedir, self._torrent.name.replace('/', '_'))
self._aria = aria.Download(self._mgr._aria, [f.url for f in self._torrent.files], fullsize = self._torrent.size,
basedir = base, unquoteNames = self.unquoteNames, startPaused = True,
interpretDirectories = self.interpretDirectories, torrentdata = self._torrent.torrent)
else:
self._aria.recheckDownload(torrentdata = self._torrent.torrent)
def update(self):
"""To be called in regular intervals to check torrent status and initiate next steps if needed."""
log(DEBUG2)
# Not finished yet?
self.percentage = self._torrent.percentage / 2
if self._torrent.hasFinished and not self._aria and self.autoStartDownload:
self.startDownload()
if self._aria:
self.percentage += self._aria.percentage / 2
if self.percentage == 100:
##self._aria.cleanup() # This get sus into trouble for calculating the percentage later
if pref("downloads", "setCompletedLabel"):
self._torrent.label = pref("downloads", "setCompletedLabel")
def start(self):
log(DEBUG)
self._torrent.start()
if self._aria:
self._aria.start()
def stop(self):
log(DEBUG)
self._torrent.stop()
if self._aria:
self._aria.stop()
# Manager class for all torrents
class Manager(object):
def __init__(self, username, password):
self._jsit = jsit.JSIT(username, password)
self._aria = aria.Aria(cleanupLeftovers = True)
self._torrents = []
time.sleep(0.3) # Little break to avoid interrupted system calls
self.syncTorrents()
# Behavior Vars
self._watchClipboard = False
self._handledClips = set()
self._watchDirectory = False
self._torrentDirectory = "intorrents"
self._torrentRename = True
def __repr__(self):
return "Manager(0x%x)"% id(self)
# Iterator access to torrent list
def __iter__(self):
return self._torrents.__iter__()
def __getitem__(self, index):
return self._torrents[index]
def __len__(self):
return len(self._torrents)
def watchClipboard(self, value):
self._watchClipboard = bool(value)
def watchDirectory(self, value):
self._watchDirectory = bool(value)
def checkTorrentDirectory(self):
log(DEBUG)
torrents = glob.glob(os.path.join(self._torrentDirectory, "*.torrent"))
for t in torrents:
self.addTorrentFile(t, basedir = pref("downloads","basedir"), maximum_ratio = pref("jsit","maximumRatioPublic"))
if self._torrentRename:
os.rename(t, t + ".uploaded")
def checkClipboard(self, clips):
log(DEBUG)
for clip in clips:
if not clip or clip in self._handledClips:
return
self._handledClips.add(clip)
if clip.startswith("magnet:") or ( clip.startswith("http://") and clip.endswith(".torrent") ):
if clip.startswith("magnet:"):
s = clip.find("dn=") + 3
e = clip.find("&", s)
else:
s = clip.rfind("/") + 1
e = None
log(WARNING, "Found link for %s, uploading...\n" % clip[s:e])
self.addTorrentURL(clip, basedir = pref("downloads","basedir"), maximum_ratio = pref("jsit","maximumRatioPublic"))
def syncTorrents(self, autoStartDownload = False, force = False):
'''Synchronize local list with data from JSIT server: add new, remove deleted ones'''
log(DEBUG, "%s:syncTorrents\n" % self)
self._jsit.updateTorrents(force = force)
new, deleted = self._jsit.resetNewDeleted()
for d in deleted:
t = self.lookupTorrent(d)
if t:
t.hash = None
self._torrents.remove(t)
for n in new:
# Do we have this one already?
t = self.lookupTorrent(n)
if not t:
t = self._jsit.lookupTorrent(n)
self._torrents.append(Torrent(self, jsittorrent = t, autoStartDownload = autoStartDownload, basedir = "downloads"))
return new, deleted
def update(self, force = False, clip = None):
log(DEBUG)
if self._watchDirectory:
self.checkTorrentDirectory()
if self._watchClipboard and clip:
self.checkClipboard(clip)
new, deleted = self.syncTorrents(force)
for t in self:
t.update()
return new, deleted
@property
def allFinished(self):
af = True
for t in self:
t.update()
if not t.hasFinished:
af = False
return af
def addTorrentFile(self, fname, maximum_ratio = None, basedir=None, unquoteNames = True, interpretDirectories = True):
log(DEBUG, "%r:addTorrentFile(%s)\n" % (self, fname))
try:
t = Torrent(self, fname = fname, maximum_ratio = maximum_ratio, basedir = basedir, unquoteNames = unquoteNames, interpretDirectories = interpretDirectories)
if find(lambda tt: tt.hash == t.hash, self._torrents):
log(INFO, "Torrent already running, ignored.\n")
else:
self._torrents.append(t)
except ValueError, e:
log(ERROR, "%r::addTorrentFile: Caught '%s', aborting.\n" % (self, e))
t = None
return t
def addTorrentURL(self, url, maximum_ratio = None, basedir=None, unquoteNames = True, interpretDirectories = True):
log(INFO, "addTorrentURL(%s)\n" % (url))
try:
t = Torrent(self, url = url, maximum_ratio = maximum_ratio, basedir = basedir, unquoteNames = unquoteNames, interpretDirectories = interpretDirectories)
if find(lambda tt: tt.hash == t.hash, self._torrents):
log(INFO, "Torrent already running, ignored.\n")
else:
self._torrents.append(t)
except ValueError, e:
log(ERROR, "%r::addTorrentURL: Caught '%s', aborting.\n" % (self, e))
t = None
return t
def findTorrents(self, search):
sre = re.compile(search)
ret = []
for t in self._torrents:
if sre.match(t.name):
ret.append(t)
return ret
def lookupTorrent(self, hash):
for t in self._torrents:
if t.hash == hash:
return t
return None
def deleteTorrent(self, tor):
if isinstance(tor, str):
tor = self.lookupTorrent(tor)
log(INFO, u"Deleting torrent %s (%s)...\n" % (tor._name, tor._hash))
self._torrent.delete()
if self._aria:
self._aria.delete()
def startAll(self):
for t in self._torrents:
t.start()
def stopAll(self):
for t in self._torrents:
t.stop()