forked from iambus/xunlei-lixian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lixian_queries.py
292 lines (237 loc) · 7.57 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
from lixian_query import Query
from lixian_query import query
from lixian_query import bt_query
import lixian_hash_bt
import lixian_url
import re
##################################################
# queries
##################################################
class SingleTaskQuery(Query):
def __init__(self, base, t):
super(SingleTaskQuery, self).__init__(base)
self.id = t['id']
def get_tasks(self):
return [self.base.get_task_by_id(self.id)]
@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(Query):
def __init__(self, base, tasks):
super(MultipleTasksQuery, self).__init__(base)
self.tasks = tasks
def get_tasks(self):
return map(self.base.get_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(Query):
def __init__(self, base, t, subs):
super(SubTaskQuery, self).__init__(base)
self.task = t
self.subs = subs
def get_tasks(self):
result = []
task = self.base.get_task_by_id(self.task['id'])
for i in self.subs:
t = dict(task)
t['index'] = i
result.append(t)
return result
@query(priority=1)
@bt_query(priority=1)
def sub_id_processor(base, x):
m = re.match(r'^#?(\d+)/([-.\w\[\],\s*]+)$', x)
if not m:
return
task_id, sub_id = m.groups()
task = base.find_task_by_id(task_id)
if not task:
return
import lixian_encoding
assert task['type'] == 'bt', 'task %s is not a bt task' % lixian_encoding.to_native(task['name'])
subs = []
if re.match(r'\[.*\]', sub_id):
for sub_id in re.split(r'\s*,\s*', sub_id[1:-1]):
assert re.match(r'^\d+(-\d+)?|\.\w+$', sub_id), sub_id
if sub_id.startswith('.'):
subs.append(sub_id)
elif '-' in sub_id:
start, end = map(int, sub_id.split('-'))
r = range(start, end+1) if start <= end else reversed(range(end, start+1))
for i in r:
subs.append(str(i))
else:
assert re.match(r'^\d+$', sub_id), sub_id
subs.append(sub_id)
elif re.match(r'^\.\w+$', sub_id):
subs.append(sub_id)
elif sub_id == '*':
subs.append(sub_id)
else:
assert re.match(r'^\d+$', sub_id), sub_id
subs.append(sub_id)
return SubTaskQuery(base, task, subs)
##################################################
class DateQuery(Query):
def __init__(self, base, x):
super(DateQuery, self).__init__(base)
self.text = x
def get_tasks(self):
return filter(lambda t: t['name'].lower().find(self.text.lower()) != -1, self.base.get_tasks())
@query(priority=1)
@bt_query(priority=1)
def date_processor(base, x):
if re.match(r'^\d{4}\.\d{2}\.\d{2}$', x):
matched = filter(lambda t: t['date'] == x, base.get_tasks())
if matched:
return MultipleTasksQuery(base, matched)
##################################################
class BtHashQuery(Query):
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 get_tasks(self):
t = self.base.find_task_by_hash(self.hash)
assert t, 'Task not found: bt://' + self.hash
return [t]
@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(Query):
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 get_tasks(self):
t = self.base.find_task_by_hash(self.hash)
assert t, 'Task not found: bt://' + self.hash
return [t]
@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(Query):
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 get_tasks(self):
t = self.base.find_task_by_hash(self.hash)
assert t, 'Task not found: bt://' + self.hash
return [t]
@query(priority=4)
@bt_query(priority=4)
def magnet_processor(base, url):
if re.match(r'magnet:', url):
return MagnetQuery(base, url)
##################################################
class BatchUrlsQuery(Query):
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 get_tasks(self):
return map(self.base.get_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(Query):
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 get_tasks(self):
t = self.base.find_task_by_url(self.url)
assert t, 'Task not found: bt://' + self.url
return [t]
@query(priority=7)
def url_processor(base, url):
if re.match(r'\w+://', url):
return UrlQuery(base, url)
##################################################
class BtUrlQuery(Query):
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 get_tasks(self):
t = self.base.find_task_by_hash(self.hash)
assert t, 'Task not found: bt://' + self.hash
return [t]
@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
torrent = urllib2.urlopen(url, timeout=60).read()
return BtUrlQuery(base, url, torrent)
##################################################
class DefaultQuery(Query):
def __init__(self, base, x):
super(DefaultQuery, self).__init__(base)
self.text = x
def get_tasks(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)