This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 378
/
lixian_query.py
476 lines (395 loc) · 11.5 KB
/
lixian_query.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
__all__ = ['query', 'bt_query', 'user_query', 'Query', 'ExactQuery', 'SearchQuery',
'build_query', 'find_tasks_to_download', 'search_tasks', 'expand_bt_sub_tasks']
import lixian_hash_bt
import lixian_hash_ed2k
import lixian_encoding
def link_normalize(url):
from lixian_url import url_unmask, normalize_unicode_link
url = url_unmask(url)
if url.startswith('magnet:'):
return 'bt://'+lixian_hash_bt.magnet_to_infohash(url).encode('hex')
elif url.startswith('ed2k://'):
return lixian_hash_ed2k.parse_ed2k_id(url)
elif url.startswith('bt://'):
return url.lower()
elif url.startswith('http://') or url.startswith('ftp://'):
return normalize_unicode_link(url)
return url
def link_equals(x1, x2):
return link_normalize(x1) == link_normalize(x2)
class TaskBase(object):
def __init__(self, client, list_tasks, limit=None):
self.client = client
self.fetch_tasks_unlimited = list_tasks
self.limit = limit
self.queries = []
self.tasks = None
self.files = {}
self.commit_jobs = [[], []]
self.download_jobs = []
def fetch_tasks(self):
if self.limit:
with self.client.attr(limit=self.limit):
return self.fetch_tasks_unlimited()
else:
return self.fetch_tasks_unlimited()
def register_queries(self, queries):
self.queries += queries
def unregister_query(self, query):
self.queries.remove(query)
def get_tasks(self):
if self.tasks is None:
self.tasks = self.fetch_tasks()
return self.tasks
def refresh_tasks(self):
self.tasks = self.fetch_tasks()
return self.tasks
def get_files(self, task):
assert isinstance(task, dict), task
id = task['id']
if id in self.files:
return self.files[id]
self.files[id] = self.client.list_bt(task)
return self.files[id]
def find_task_by_id(self, id):
assert isinstance(id, basestring), repr(id)
for t in self.get_tasks():
if t['id'] == str(id) or t['#'] == int(id):
return t
def get_task_by_id(self, id):
t = self.find_task_by_id(id)
if not t:
raise Exception('No task found for id '+id)
return t
def find_task_by_hash(self, hash):
for t in self.get_tasks():
if t['type'] == 'bt' and t['bt_hash'].lower() == hash:
return t
def find_task_by_url(self, url):
for t in self.get_tasks():
if link_equals(t['original_url'], url):
return t
def get_task_by_url(self, url):
t = self.find_task_by_url(url)
if not t:
raise Exception('No task found for ' + lixian_encoding.to_native(url))
return t
def add_url_task(self, url):
self.commit_jobs[0].append(url)
def add_bt_task_by_hash(self, hash):
self.commit_jobs[1].append(['hash', hash])
def add_bt_task_by_content(self, content, name):
self.commit_jobs[1].append(['content', (content, name)])
def add_magnet_task(self, hash):
self.commit_jobs[1].append(['magnet', hash])
def commit(self):
urls, bts = self.commit_jobs
if urls:
self.client.add_batch_tasks(map(lixian_encoding.try_native_to_utf_8, urls))
for bt_type, value in bts:
if bt_type == 'hash':
print 'Adding bt task', value # TODO: print the thing user inputs (may be not hash)
self.client.add_torrent_task_by_info_hash(value)
elif bt_type == 'content':
content, name = value
print 'Adding bt task', name
self.client.add_torrent_task_by_content(content)
elif bt_type == 'magnet':
print 'Adding magnet task', value # TODO: print the thing user inputs (may be not hash)
self.client.add_task(value)
else:
raise NotImplementedError(bt_type)
self.commit_jobs = [[], []]
self.refresh_tasks()
def prepare(self):
# prepare actions (e.g. add tasks)
for query in self.queries:
query.prepare()
# commit and refresh task list
self.commit()
def query_complete(self):
for query in list(self.queries):
query.query_complete()
def merge_results(self):
tasks = merge_tasks(self.download_jobs)
for t in tasks:
if t['type'] == 'bt':
# XXX: a dirty trick to cache requests
t['base'] = self
self.download_jobs = tasks
def query_once(self):
self.prepare()
# merge results
for query in self.queries:
self.download_jobs += query.query_once()
self.query_complete()
self.merge_results()
def query_search(self):
for query in self.queries:
self.download_jobs += query.query_search()
self.merge_results()
def peek_download_jobs(self):
return self.download_jobs
def pull_completed(self):
completed = []
waiting = []
for t in self.download_jobs:
if t['status_text'] == 'completed':
completed.append(t)
elif t['type'] != 'bt':
waiting.append(t)
elif 'files' not in t:
waiting.append(t)
else:
i_completed = []
i_waiting = []
for f in t['files']:
if f['status_text'] == 'completed':
i_completed.append(f)
else:
i_waiting.append(f)
if i_completed:
tt = dict(t)
tt['files'] = i_completed
completed.append(tt)
if i_waiting:
tt = dict(t)
tt['files'] = i_waiting
waiting.append(tt)
self.download_jobs = waiting
return completed
def refresh_status(self):
self.refresh_tasks()
self.files = {}
tasks = []
for old_task in self.download_jobs:
new_task = dict(self.get_task_by_id(old_task['id']))
if 'files' in old_task:
files = self.get_files(new_task)
new_task['files'] = [files[f['index']] for f in old_task['files']]
tasks.append(new_task)
self.download_jobs = tasks
class Query(object):
def __init__(self, base):
self.bind(base)
def bind(self, base):
self.base = base
self.client = base.client
return self
def unregister(self):
self.base.unregister_query(self)
def prepare(self):
pass
def query_once(self):
raise NotImplementedError()
def query_complete(self):
raise NotImplementedError()
def query_search(self):
raise NotImplementedError()
class ExactQuery(Query):
def __init__(self, base):
super(ExactQuery, self).__init__(base)
def query_once(self):
raise NotImplementedError()
def query_complete(self):
self.unregister()
def query_search(self):
raise NotImplementedError()
class SearchQuery(Query):
def __init__(self, base):
super(SearchQuery, self).__init__(base)
def query_once(self):
return self.query_search()
def query_complete(self):
pass
def query_search(self):
raise NotImplementedError()
##################################################
# register
##################################################
processors = []
bt_processors = []
# 0
# 1 -- builtin -- most
# 2 -- subs -- 0/[0-9]
# 4 -- magnet
# 5 -- user
# 6 -- extend url
# 7 -- plain url, bt url
# 8 -- filter
# 9 -- default -- text search
def query(priority):
assert isinstance(priority, (int, float))
def register(processor):
processors.append((priority, processor))
return processor
return register
def bt_query(priority):
assert isinstance(priority, (int, float))
def register(processor):
bt_processors.append((priority, processor))
return processor
return register
def user_query(processor):
return query(priority=5)(processor)
def load_default_queries():
import lixian_queries
##################################################
# query
##################################################
def to_list_tasks(client, args):
if args.category:
return lambda: client.read_all_tasks_by_category(args.category)
elif args.deleted:
return client.read_all_deleted
elif args.expired:
return client.read_all_expired
elif args.completed:
return client.read_all_tasks
elif args.failed:
return client.read_all_tasks
elif args.all:
return client.read_all_tasks
else:
return client.read_all_tasks
def to_query(base, arg, processors):
for _, process in sorted(processors):
q = process(base, arg)
if q:
return q
raise NotImplementedError('No proper query process found for: ' + arg)
def merge_files(files1, files2):
ids = []
files = []
for f in files1 + files2:
if f['id'] not in ids:
files.append(f)
ids.append(f['id'])
return files
def merge_tasks(tasks):
result_tasks = []
task_mapping = {}
for task in tasks:
assert type(task) == dict, repr(type)
id = task['id']
assert 'index' not in task
if id in task_mapping:
if 'files' in task and 'files' in task_mapping[id]:
task_mapping[id]['files'] = merge_files(task_mapping[id]['files'], task['files'])
else:
if 'files' in task:
t = dict(task)
result_tasks.append(t)
task_mapping[id] = t
else:
result_tasks.append(task)
task_mapping[id] = task
return result_tasks
class AllQuery(SearchQuery):
def __init__(self, base):
super(AllQuery, self).__init__(base)
def query_search(self):
return self.base.get_tasks()
class CompletedQuery(SearchQuery):
def __init__(self, base):
super(CompletedQuery, self).__init__(base)
def query_search(self):
return filter(lambda x: x['status_text'] == 'completed', self.base.get_tasks())
class FailedQuery(SearchQuery):
def __init__(self, base):
super(FailedQuery, self).__init__(base)
def query_search(self):
return filter(lambda x: x['status_text'] == 'failed', self.base.get_tasks())
class NoneQuery(SearchQuery):
def __init__(self, base):
super(NoneQuery, self).__init__(base)
def query_search(self):
return []
def default_query(options):
if options.category:
return AllQuery
elif options.deleted:
return AllQuery
elif options.expired:
return AllQuery
elif options.completed:
return CompletedQuery
elif options.failed:
return FailedQuery
elif options.all:
return AllQuery
else:
return NoneQuery
def parse_queries(base, args):
return [to_query(base, arg, bt_processors if args.torrent else processors) for arg in args] or [default_query(args)(base)]
def parse_limit(args):
limit = args.limit
if limit:
limit = int(limit)
ids = []
for x in args:
import re
if re.match(r'^\d+$', x):
ids.append(int(x))
elif re.match(r'^(\d+)/', x):
ids.append(int(x.split('/')[0]))
elif re.match(r'^(\d+)-(\d+)$', x):
ids.extend(map(int, x.split('-')))
else:
return limit
if ids and limit:
return min(max(ids)+1, limit)
elif ids:
return max(ids)+1
else:
return limit
def build_query(client, args):
if args.input:
import fileinput
args._left.extend(line.strip() for line in fileinput.input(args.input) if line.strip())
load_default_queries() # IMPORTANT: init default queries
limit = parse_limit(args)
base = TaskBase(client, to_list_tasks(client, args), limit)
base.register_queries(parse_queries(base, args))
return base
##################################################
# compatible APIs
##################################################
def find_tasks_to_download(client, args):
base = build_query(client, args)
base.query_once()
return base.peek_download_jobs()
def search_tasks(client, args):
base = build_query(client, args)
base.query_search()
return base.peek_download_jobs()
def expand_bt_sub_tasks(task):
files = task['base'].get_files(task) # XXX: a dirty trick to cache requests
not_ready = []
single_file = False
if len(files) == 1 and files[0]['name'] == task['name']:
single_file = True
if 'files' in task:
ordered_files = []
for t in task['files']:
assert isinstance(t, dict)
if t['status_text'] != 'completed':
not_ready.append(t)
else:
ordered_files.append(t)
files = ordered_files
return files, not_ready, single_file
##################################################
# simple helpers
##################################################
def get_task_by_id(client, id):
base = TaskBase(client, client.read_all_tasks)
return base.get_task_by_id(id)
def get_task_by_any(client, arg):
import lixian_cli_parser
tasks = search_tasks(client, lixian_cli_parser.parse_command_line([arg]))
if not tasks:
raise LookupError(arg)
if len(tasks) > 1:
raise LookupError('Too many results for ' + arg)
return tasks[0]