forked from iambus/xunlei-lixian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lixian_queries.py
326 lines (260 loc) · 8.42 KB
/
lixian_queries.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
from lixian_query import ExactQuery
from lixian_query import SearchQuery
from lixian_query import query
from lixian_query import bt_query
import lixian_hash_bt
import lixian_url
import lixian_encoding
import re
##################################################
# queries
##################################################
class SingleTaskQuery(ExactQuery):
def __init__(self, base, t):
super(SingleTaskQuery, self).__init__(base)
self.id = t['id']
def query_once(self):
return [self.base.get_task_by_id(self.id)]
def query_search(self):
t = self.base.find_task_by_id(self.id)
return [t] if t else []
@query(priority=1)
@bt_query(priority=1)
def single_id_processor(base, x):
if not re.match(r'^\d+/?$', x):
return
n = x.rstrip('/')
t = base.find_task_by_id(n)
if t:
return SingleTaskQuery(base, t)
##################################################
class MultipleTasksQuery(ExactQuery):
def __init__(self, base, tasks):
super(MultipleTasksQuery, self).__init__(base)
self.tasks = tasks
def query_once(self):
return map(self.base.get_task_by_id, (t['id'] for t in self.tasks))
def query_search(self):
return filter(bool, map(self.base.find_task_by_id, (t['id'] for t in self.tasks)))
@query(priority=1)
@bt_query(priority=1)
def range_id_processor(base, x):
m = re.match(r'^(\d+)-(\d+)$', x)
if not m:
return
begin = int(m.group(1))
end = int(m.group(2))
tasks = base.get_tasks()
if begin <= end:
found = filter(lambda x: begin <= x['#'] <= end, tasks)
else:
found = reversed(filter(lambda x: end <= x['#'] <= begin, tasks))
if found:
return MultipleTasksQuery(base, found)
##################################################
class SubTaskQuery(ExactQuery):
def __init__(self, base, t, subs):
super(SubTaskQuery, self).__init__(base)
self.task = t
self.subs = subs
def query_once(self):
task = dict(self.base.get_task_by_id(self.task['id']))
files = self.base.get_files(task)
task['files'] = self.subs
return [task]
def query_search(self):
task = self.base.find_task_by_id(self.task['id'])
if not task:
return []
task = dict(task)
files = self.base.get_files(task)
task['files'] = self.subs
return [task]
@query(priority=2)
@bt_query(priority=2)
def sub_id_processor(base, x):
x = lixian_encoding.from_native(x)
m = re.match(r'^(\d+)/(.+)$', x)
if not m:
return
task_id, sub_id = m.groups()
task = base.find_task_by_id(task_id)
if not task:
return
assert task['type'] == 'bt', 'task %s is not a bt task' % lixian_encoding.to_native(task['name'])
files = base.get_files(task)
import lixian_filter_expr
files = lixian_filter_expr.filter_expr(files, sub_id)
subs = [x for x in files]
return SubTaskQuery(base, task, subs)
##################################################
class BtHashQuery(ExactQuery):
def __init__(self, base, x):
super(BtHashQuery, self).__init__(base)
self.hash = re.match(r'^(?:bt://)?([0-9a-f]{40})$', x, flags=re.I).group(1).lower()
self.task = self.base.find_task_by_hash(self.hash)
def prepare(self):
if not self.task:
self.base.add_bt_task_by_hash(self.hash)
def query_once(self):
t = self.base.find_task_by_hash(self.hash)
assert t, 'Task not found: bt://' + self.hash
return [t]
def query_search(self):
t = self.base.find_task_by_hash(self.hash)
return [t] if t else []
@query(priority=1)
@bt_query(priority=1)
def bt_hash_processor(base, x):
if re.match(r'^(bt://)?[0-9a-f]{40}$', x, flags=re.I):
return BtHashQuery(base, x)
##################################################
class LocalBtQuery(ExactQuery):
def __init__(self, base, x):
super(LocalBtQuery, self).__init__(base)
self.path = x
self.hash = lixian_hash_bt.info_hash(self.path)
self.task = self.base.find_task_by_hash(self.hash)
with open(self.path, 'rb') as stream:
self.torrent = stream.read()
def prepare(self):
if not self.task:
self.base.add_bt_task_by_content(self.torrent, self.path)
def query_once(self):
t = self.base.find_task_by_hash(self.hash)
assert t, 'Task not found: bt://' + self.hash
return [t]
def query_search(self):
t = self.base.find_task_by_hash(self.hash)
return [t] if t else []
@query(priority=1)
@bt_query(priority=1)
def local_bt_processor(base, x):
import os.path
if x.lower().endswith('.torrent') and os.path.exists(x):
return LocalBtQuery(base, x)
##################################################
class MagnetQuery(ExactQuery):
def __init__(self, base, x):
super(MagnetQuery, self).__init__(base)
self.url = x
self.hash = lixian_hash_bt.magnet_to_infohash(x).encode('hex').lower()
self.task = self.base.find_task_by_hash(self.hash)
def prepare(self):
if not self.task:
self.base.add_magnet_task(self.url)
def query_once(self):
t = self.base.find_task_by_hash(self.hash)
assert t, 'Task not found: bt://' + self.hash
return [t]
def query_search(self):
t = self.base.find_task_by_hash(self.hash)
return [t] if t else []
@query(priority=4)
@bt_query(priority=4)
def magnet_processor(base, url):
if re.match(r'magnet:', url):
return MagnetQuery(base, url)
##################################################
class BatchUrlsQuery(ExactQuery):
def __init__(self, base, urls):
super(BatchUrlsQuery, self).__init__(base)
self.urls = urls
def prepare(self):
for url in self.urls:
if not self.base.find_task_by_url(url):
self.base.add_url_task(url)
def query_once(self):
return map(self.base.get_task_by_url, self.urls)
def query_search(self):
return filter(bool, map(self.base.find_task_by_url, self.urls))
@query(priority=6)
@bt_query(priority=6)
def url_extend_processor(base, url):
import lixian_plugins.parsers
extended = lixian_plugins.parsers.try_to_extend_link(url)
if extended:
extended = map(lixian_plugins.parsers.to_url, extended)
return BatchUrlsQuery(base, extended)
##################################################
class UrlQuery(ExactQuery):
def __init__(self, base, x):
super(UrlQuery, self).__init__(base)
self.url = lixian_url.url_unmask(x)
self.task = self.base.find_task_by_url(self.url)
def prepare(self):
if not self.task:
self.base.add_url_task(self.url)
def query_once(self):
t = self.base.find_task_by_url(self.url)
assert t, 'Task not found: ' + self.url
return [t]
def query_search(self):
t = self.base.find_task_by_url(self.url)
return [t] if t else []
@query(priority=7)
def url_processor(base, url):
if re.match(r'\w+://', url):
return UrlQuery(base, url)
##################################################
class BtUrlQuery(ExactQuery):
def __init__(self, base, url, torrent):
super(BtUrlQuery, self).__init__(base)
self.url = url
self.torrent = torrent
self.hash = lixian_hash_bt.info_hash_from_content(self.torrent)
self.task = self.base.find_task_by_hash(self.hash)
def prepare(self):
if not self.task:
self.base.add_bt_task_by_content(self.torrent, self.url)
def query_once(self):
t = self.base.find_task_by_hash(self.hash)
assert t, 'Task not found: bt://' + self.hash
return [t]
def query_search(self):
t = self.base.find_task_by_hash(self.hash)
return [t] if t else []
@bt_query(priority=7)
def bt_url_processor(base, url):
if not re.match(r'http://', url):
return
print 'Downloading torrent file from', url
import urllib2
response = urllib2.urlopen(url, timeout=60)
torrent = response.read()
if response.info().get('Content-Encoding') == 'gzip':
def ungzip(s):
from StringIO import StringIO
import gzip
buffer = StringIO(s)
f = gzip.GzipFile(fileobj=buffer)
return f.read()
torrent = ungzip(torrent)
return BtUrlQuery(base, url, torrent)
##################################################
class FilterQuery(SearchQuery):
def __init__(self, base, x):
super(FilterQuery, self).__init__(base)
self.keyword = x
def query_search(self):
import lixian_plugins.filters
tasks = lixian_plugins.filters.filter_tasks(self.base.get_tasks(), self.keyword)
assert tasks is not None
return tasks
@query(priority=8)
@bt_query(priority=8)
def filter_processor(base, x):
import lixian_plugins.filters
if lixian_plugins.filters.has_task_filter(x):
return FilterQuery(base, x)
##################################################
class DefaultQuery(SearchQuery):
def __init__(self, base, x):
super(DefaultQuery, self).__init__(base)
self.text = lixian_encoding.from_native(x)
def query_search(self):
return filter(lambda t: t['name'].lower().find(self.text.lower()) != -1, self.base.get_tasks())
@query(priority=9)
@bt_query(priority=9)
def default_processor(base, x):
return DefaultQuery(base, x)