public
Description: The source code for the Django Plugables application index.
Homepage: http://djangoplugables.com/
Clone URL: git://github.com/revyver/django-plugables.git
Search Repo:
Removing django-sphinx because I'm now confused about what search 
functionality to use. :/
revyver (author)
Mon Apr 14 14:26:14 -0700 2008
commit  c97318b7aa3c9a790707be332c12ebe69f12fc56
tree    ec77aa7b4c3257766ff28e4c2f9e703a0c2e7ee9
parent  8be3074ab7d1213629d3500b0bf1142e395ad7a5
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -0,0 +1,19 @@
0
+from django.contrib import admin
0
+
0
+from models import Entry
0
+
0
+
0
+class EntryAdmin(admin.ModelAdmin):
0
+ date_hierarchy = 'published'
0
+ fieldsets = (
0
+ ('Metadata', {'fields': ('title', 'slug', 'published', 'author')}),
0
+ ('Content', {'fields': ('summary', 'body', 'footnotes')}),
0
+ ('Options', {'fields': ('status', 'enable_comments')}),
0
+ )
0
+ list_display = ('title', 'status', 'slug', 'published', 'enable_comments')
0
+ list_filter = ('published', 'status')
0
+ prepopulated_fields = { 'slug': ('title',) }
0
+ search_fields = ('title', 'summary', 'body', 'footnotes')
0
+
0
+
0
+admin.site.register(Entry, EntryAdmin)
0
\ No newline at end of file
...
 
 
0
...
1
2
3
0
@@ -0,0 +1,2 @@
0
+# Administration
0
+from blog import admin
0
\ No newline at end of file
...
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
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,455 +0,0 @@
0
-"""
0
-Sphinx Search Engine ORM for Django models
0
-http://www.sphinxsearch.com/
0
-Developed and maintained David Cramer <dcramer@gmail.com>
0
-
0
-To add a search manager to your model:
0
-<code>
0
- search = SphinxSearch([index=<string>, weight=[<int>,], mode=<string>])
0
-</code>
0
-
0
-To query the engine and retrieve objects:
0
-<code>
0
- MyModel.search.query('my string')
0
-</code>
0
-
0
-To use multiple index support, you need to define a "content_type" field in your SQL
0
-clause. Each index also needs to have the exact same field's. The rules are almost identical
0
-to that of an SQL UNION query.
0
-<code>
0
- SELECT id, name, 1 as content_type FROM model_myapp
0
- SELECT id, name, 2 as content_type FROM model_myotherapp
0
- search_results = SphinxSearch()
0
- search_results.on_index('model_myapp model_myotherapp')
0
- search_results.query('hello')
0
-</code>
0
-
0
-default settings.py values
0
-<code>
0
- SPHINX_SERVER = 'localhost'
0
- SPHINX_PORT = 3312
0
-</code>
0
-"""
0
-
0
-import select
0
-import socket
0
-import time
0
-import struct
0
-
0
-try:
0
- from sphinxapi import SPH_SORT_RELEVANCE, SPH_GROUPBY_DAY, SPH_MATCH_EXTENDED, VER_COMMAND_SEARCH, SphinxClient
0
-except ImportError:
0
- from api117 import SPH_SORT_RELEVANCE, SPH_GROUPBY_DAY, SPH_MATCH_EXTENDED, VER_COMMAND_SEARCH, SphinxClient
0
-
0
-from django.db.models.query import QuerySet
0
-from django.conf import settings
0
-
0
-__all__ = ('SearchError', 'ConnectionError', 'SphinxSearch')
0
-
0
-from django.contrib.contenttypes.models import ContentType
0
-
0
-# server settings
0
-SPHINX_SERVER = getattr(settings, 'SPHINX_SERVER', 'localhost')
0
-SPHINX_PORT = getattr(settings, 'SPHINX_PORT', 3312)
0
-
0
-# These require search API 1.19 (Sphinx 0.9.8)
0
-SPHINX_RETRIES = getattr(settings, 'SPHINX_RETRIES', 0)
0
-SPHINX_RETRIES_DELAY = getattr(settings, 'SPHINX_RETRIES_DELAY', 5)
0
-
0
-class SearchError(Exception):
0
- def __init__(self, message):
0
- self.message = message
0
-
0
- def __str__(self):
0
- return str(self.message)
0
-
0
-class ConnectionError(Exception):
0
- def __init__(self, message):
0
- self.message = message
0
-
0
- def __str__(self):
0
- return str(self.message)
0
-
0
-class SphinxProxy(object):
0
- """
0
- Acts exactly like a normal instance of an object except that
0
- it will handle any special sphinx attributes in a _sphinx class.
0
- """
0
- __slots__ = ('__dict__', '__instance__', '_sphinx')
0
-
0
- def __init__(self, instance, attributes):
0
- object.__setattr__(self, '__instance__', instance)
0
- object.__setattr__(self, '_sphinx', attributes)
0
-
0
- def _get_current_object(self):
0
- """
0
- Return the current object. This is useful if you want the real object
0
- behind the proxy at a time for performance reasons or because you want
0
- to pass the object into a different context.
0
- """
0
- return self.__instance__
0
- __current_object = property(_get_current_object)
0
-
0
- def __dict__(self):
0
- try:
0
- return self.__current_object.__dict__
0
- except RuntimeError:
0
- return AttributeError('__dict__')
0
- __dict__ = property(__dict__)
0
-
0
- def __repr__(self):
0
- try:
0
- obj = self.__current_object
0
- except RuntimeError:
0
- return '<%s unbound>' % self.__class__.__name__
0
- return repr(obj)
0
-
0
- def __nonzero__(self):
0
- try:
0
- return bool(self.__current_object)
0
- except RuntimeError:
0
- return False
0
-
0
- def __unicode__(self):
0
- try:
0
- return unicode(self.__current_oject)
0
- except RuntimeError:
0
- return repr(self)
0
-
0
- def __dir__(self):
0
- try:
0
- return dir(self.__current_object)
0
- except RuntimeError:
0
- return []
0
-
0
- def __getattr__(self, name, value=None):
0
- if name == '__members__':
0
- return dir(self.__current_object)
0
- elif name == '_sphinx':
0
- return object.__getattr__(self, '_sphinx', value)
0
- return getattr(self.__current_object, name)
0
-
0
- def __setattr__(self, name, value):
0
- if name == '_sphinx':
0
- return object.__setattr__(self, '_sphinx', value)
0
- return setattr(self.__current_object, name, value)
0
-
0
- def __setitem__(self, key, value):
0
- self.__current_object[key] = value
0
-
0
- def __delitem__(self, key):
0
- del self.__current_object[key]
0
-
0
- def __setslice__(self, i, j, seq):
0
- self.__current_object[i:j] = seq
0
-
0
- def __delslice__(self, i, j):
0
- del self.__current_object[i:j]
0
-
0
- __delattr__ = lambda x, n: delattr(x.__current_object, n)
0
- __str__ = lambda x: str(x.__current_object)
0
- __unicode__ = lambda x: unicode(x.__current_object)
0
- __lt__ = lambda x, o: x.__current_object < o
0
- __le__ = lambda x, o: x.__current_object <= o
0
- __eq__ = lambda x, o: x.__current_object == o
0
- __ne__ = lambda x, o: x.__current_object != o
0
- __gt__ = lambda x, o: x.__current_object > o
0
- __ge__ = lambda x, o: x.__current_object >= o
0
- __cmp__ = lambda x, o: cmp(x.__current_object, o)
0
- __hash__ = lambda x: hash(x.__current_object)
0
- __call__ = lambda x, *a, **kw: x.__current_object(*a, **kw)
0
- __len__ = lambda x: len(x.__current_object)
0
- __getitem__ = lambda x, i: x.__current_object[i]
0
- __iter__ = lambda x: iter(x.__current_object)
0
- __contains__ = lambda x, i: i in x.__current_object
0
- __getslice__ = lambda x, i, j: x.__current_object[i:j]
0
- __add__ = lambda x, o: x.__current_object + o
0
- __sub__ = lambda x, o: x.__current_object - o
0
- __mul__ = lambda x, o: x.__current_object * o
0
- __floordiv__ = lambda x, o: x.__current_object // o
0
- __mod__ = lambda x, o: x.__current_object % o
0
- __divmod__ = lambda x, o: x.__current_object.__divmod__(o)
0
- __pow__ = lambda x, o: x.__current_object ** o
0
- __lshift__ = lambda x, o: x.__current_object << o
0
- __rshift__ = lambda x, o: x.__current_object >> o
0
- __and__ = lambda x, o: x.__current_object & o
0
- __xor__ = lambda x, o: x.__current_object ^ o
0
- __or__ = lambda x, o: x.__current_object | o
0
- __div__ = lambda x, o: x.__current_object.__div__(o)
0
- __truediv__ = lambda x, o: x.__current_object.__truediv__(o)
0
- __neg__ = lambda x: -(x.__current_object)
0
- __pos__ = lambda x: +(x.__current_object)
0
- __abs__ = lambda x: abs(x.__current_object)
0
- __invert__ = lambda x: ~(x.__current_object)
0
- __complex__ = lambda x: complex(x.__current_object)
0
- __int__ = lambda x: int(x.__current_object)
0
- __long__ = lambda x: long(x.__current_object)
0
- __float__ = lambda x: float(x.__current_object)
0
- __oct__ = lambda x: oct(x.__current_object)
0
- __hex__ = lambda x: hex(x.__current_object)
0
- __index__ = lambda x: x.__current_object.__index__()
0
- __coerce__ = lambda x, o: x.__coerce__(x, o)
0
- __enter__ = lambda x: x.__enter__()
0
- __exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)
0
-
0
-class SphinxSearch(object):
0
- def __init__(self, index=None, **kwargs):
0
- self.init()
0
- if index:
0
- self._index = index
0
- else:
0
- self._index = None
0
- if 'mode' in kwargs:
0
- self.mode(kwargs['mode'])
0
- if 'weights' in kwargs:
0
- self.weights(kwargs['weights'])
0
-
0
- def _clone(self, **kwargs):
0
- # Clones the queryset passing any changed args
0
- c = self.__class__()
0
- c.__dict__.update(self.__dict__)
0
- c.__dict__.update(kwargs)
0
- return c
0
-
0
- def init(self):
0
- self._select_related = False
0
- self._select_related_args = {}
0
- self._select_related_fields = []
0
- self._filters = {}
0
- self._excludes = {}
0
- self._extra = {}
0
- self._query = ''
0
- self.__metadata = None
0
- self._offset = 0
0
- self._limit = 20
0
-
0
- self._filter_range = None
0
- self._groupby = None
0
- self._sort = None
0
- self._weights = None
0
-
0
- self._maxmatches = 1000
0
- self._result_cache = None
0
- self._mode = SPH_MATCH_EXTENDED
0
- self._model = None
0
- self._anchor = {}
0
-
0
- def __get__(self, instance, instance_model, **kwargs):
0
- if instance != None:
0
- raise AttributeError, "Manager isn't accessible via %s instances" % type.__name__
0
- self.init()
0
- self._model = instance_model
0
- if not self._index and self._model:
0
- self._index = self._model._meta.db_table
0
- return self
0
-
0
- def __repr__(self):
0
- return repr(self._get_data())
0
-
0
- def __len__(self):
0
- return len(self._get_data())
0
-
0
- def __iter__(self):
0
- return iter(self._get_data())
0
-
0
- def __getitem__(self, k):
0
- if not isinstance(k, (slice, int)):
0
- raise TypeError
0
- assert (not isinstance(k, slice) and (k >= 0)) \
0
- or (isinstance(k, slice) and (k.start is None or k.start >= 0) and (k.stop is None or k.stop >= 0)), \
0
- "Negative indexing is not supported."
0
- if type(k) == slice:
0
- if self._offset < k.start or k.stop-k.start > self._limit:
0
- self._result_cache = None
0
- else:
0
- if k not in range(self._offset, self._limit+self._offset):
0
- self._result_cache = None
0
- if self._result_cache is None:
0
- if type(k) == slice:
0
- self._offset = k.start
0
- self._limit = k.stop-k.start
0
- return self._get_results()
0
- else:
0
- self._offset = k
0
- self._limit = 1
0
- return self._get_results()[0]
0
- else:
0
- return self._result_cache[k]
0
-
0
- def query(self, string):
0
- return self._clone(_query=string)
0
-
0
- def mode(self, mode):
0
- return self._clone(_mode=mode)
0
-
0
- def group_by(self, attribute, func, groupsort='@group desc'):
0
- return self._clone(_groupby=attribute, _groupfunc=func, _groupsort=groupsort)
0
-
0
- def weights(self, weights):
0
- return self._clone(_weights=weights)
0
-
0
- # only works on attributes
0
- def filter(self, **kwargs):
0
- filters = self._filters.copy()
0
- for k,v in kwargs.iteritems():
0
- if not isinstance(v, list):
0
- v = [v,]
0
- v = [isinstance(value, bool) and value and 1 or 0 or int(value) for value in v]
0
- filters.setdefault(k, []).append(v)
0
-
0
- return self._clone(_filters=filters)
0
-
0
- def geoanchor(self, **kwargs):
0
- assert(VER_COMMAND_SEARCH >= 0x113, "You must upgrade sphinxapi to version 1.19 to use Geo Anchoring.")
0
- return self._clone(_anchor=kwargs)
0
-
0
- def on_index(self, index):
0
- return self._clone(_index=index)
0
-
0
- # this actually does nothing, its just a passthru to
0
- # keep things looking/working generally the same
0
- def all(self):
0
- return self
0
-
0
- # only works on attributes
0
- def exclude(self, **kwargs):
0
- filters = self._excludes.copy()
0
- for k,v in kwargs.iteritems():
0
- if not isinstance(v, list):
0
- v = [v,]
0
- v = [isinstance(value, bool) and value and 1 or 0 or int(value) for value in v]
0
- filters.setdefault(k, []).append(v)
0
-
0
- return self._clone(_excludes=filters)
0
-
0
- # you cannot order by @weight (it always orders in descending)
0
- # keywords are @id, @weight, @rank, and @relevance
0
- def order_by(self, *args):
0
- sort_by = []
0
- for arg in args:
0
- sort = 'ASC'
0
- if arg[0] == '-':
0
- arg = arg[1:]
0
- sort = 'DESC'
0
- if arg == 'id':
0
- arg = '@id'
0
- sort_by.append('%s %s' % (arg, sort))
0
- if sort_by:
0
- return self._clone(_sort=(SPH_SORT_EXTENDED, ', '.join(sort_by)))
0
- return self
0
-
0
- # pass these thru on the queryset and let django handle it
0
- def select_related(self, *args, **kwargs):
0
- _args = self._select_related_fields[:]
0
- _args.extend(args)
0
- _kwargs = self._select_related_args.copy()
0
- _kwargs.update(kwargs)
0
-
0
- return self._clone(
0
- _select_related=True,
0
- _select_related_fields=_args,
0
- _select_related_args=_kwargs,
0
- )
0
-
0
- def extra(self, **kwargs):
0
- extra = self._extra.copy()
0
- extra.update(kwargs)
0
- return self._clone(_extra=extra)
0
-
0
- def count(self):
0
- return self._get_sphinx_results()['total_found']
0
-
0
- # Internal methods
0
-
0
- def _sphinx(self):
0
- if not self.__metadata:
0
- # We have to force execution if this is accessed beforehand
0
- self._get_data()
0
- return self.__metadata
0
- _sphinx = property(_sphinx)
0
-
0
- def _get_data(self):
0
- assert(self._index)
0
- assert(self._query)
0
- # need to find a way to make this work yet
0
- if self._result_cache is None:
0
- self._result_cache = list(self._get_results())
0
- return self._result_cache
0
-
0
- def _get_sphinx_results(self):
0
- client = SphinxClient()
0
- client.SetServer(SPHINX_SERVER, SPHINX_PORT)
0
-
0
- if self._sort:
0
- client.SetSortMode(*self._sort)
0
-
0
- if self._weights:
0
- client.SetWeights(self._weights)
0
-
0
- # Include filters
0
- if self._filters:
0
- for name, values in self._filters:
0
- client.SetFilter(name, values)
0
-
0
- # Exclude filters
0
- if self._excludes:
0
- for name, values in self._excludes:
0
- client.SetFilter(name, values, exclude=1)
0
-
0
- if self._filter_range:
0
- client.SetIDRange(*self._filter_range)
0
-
0
- if self._groupby:
0
- client.SetGroupBy(self._groupby, self._groupfunc, self._groupsort)
0
-
0
- if self._anchor:
0
- client.SetGeoAnchor(self._anchor)
0
-
0
- client.SetLimits(self._offset, self._limit, self._maxmatches)
0
- if VER_COMMAND_SEARCH >= 0x113:
0
- client.SetRetries(SPHINX_RETRIES, SPHINX_RETRIES_DELAY)
0
-
0
- results = client.Query(self._query, self._index)
0
-
0
- # The Sphinx API doesn't raise exceptions
0
- if not results and client.GetLastError():
0
- raise SearchError, client.GetLastError()
0
- return results
0
-
0
- def _get_results(self):
0
- results = self._get_sphinx_results()
0
- if results['matches'] and self._model:
0
- qs = self._model.objects.filter(pk__in=[r['id'] for r in results['matches']])
0
- if self._select_related:
0
- qs = qs.select_related(*self._select_related_fields, **self._select_related_args)
0
- if self._extra:
0
- qs = qs.extra(**self._extra)
0
- queryset = dict([(o.id, o) for o in qs])
0
- self.__metadata = {
0
- 'total': results['total'],
0
- 'total_found': results['total_found'],
0
- 'words': results['words'],
0
- }
0
- results = [SphinxProxy(queryset[k['id']], {'weight': k['weight']}) for k in results['matches'] if k['id'] in queryset]
0
- elif results['matches']:
0
- "We did a query without a model, lets see if there's a content_type"
0
- if 'content_type' in results['attrs']:
0
- "Now we have to do one query per content_type"
0
- x = results['attrs'].index('content_type')
0
- objcache = {}
0
- for r in results['matches']:
0
- ct = r['attrs'][x]
0
- if ct not in objcache:
0
- objcache[ct] = {}
0
- objcache[ct][r['doc']] = None
0
- for ct in objcache:
0
- qs = ContentType.objects.get(pk=ct).model_class().objects.filter(pk__in=objcache[ct])
0
- for o in qs:
0
- objcache[ct][o.id] = o
0
- results = [objcache[r['attrs'][x]][r['doc']] for r in results['matches']]
0
- else:
0
- results = results['matches']
0
- else:
0
- results = []
0
- self._result_cache = results
0
- return results
...
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,579 +0,0 @@
0
-#
0
-# $Id: sphinxapi.py,v 1.7 2007/04/01 21:38:13 shodan Exp $
0
-#
0
-# Python version of Sphinx searchd client (Python API)
0
-#
0
-# Copyright (c) 2006-2007, Andrew Aksyonoff
0
-# Copyright (c) 2006, Mike Osadnik
0
-# All rights reserved
0
-#
0
-# This program is free software; you can redistribute it and/or modify
0
-# it under the terms of the GNU General Public License. You should have
0
-# received a copy of the GPL license along with this program; if you
0
-# did not, you can find it at http://www.gnu.org/
0
-#
0
-
0
-import select
0
-import socket
0
-from struct import *
0
-
0
-
0
-# known searchd commands
0
-SEARCHD_COMMAND_SEARCH  = 0
0
-SEARCHD_COMMAND_EXCERPT  = 1
0
-
0
-# current client-side command implementation versions
0
-VER_COMMAND_SEARCH    = 0x107
0
-VER_COMMAND_EXCERPT    = 0x100
0
-
0
-# known searchd status codes
0
-SEARCHD_OK        = 0
0
-SEARCHD_ERROR      = 1
0
-SEARCHD_RETRY      = 2
0
-SEARCHD_WARNING      = 3
0
-
0
-# known match modes
0
-SPH_MATCH_ALL      = 0
0
-SPH_MATCH_ANY      = 1
0
-SPH_MATCH_PHRASE    = 2
0
-SPH_MATCH_BOOLEAN    = 3
0
-SPH_MATCH_EXTENDED    = 4
0
-
0
-# known sort modes
0
-SPH_SORT_RELEVANCE    = 0
0
-SPH_SORT_ATTR_DESC    = 1
0
-SPH_SORT_ATTR_ASC    = 2
0
-SPH_SORT_TIME_SEGMENTS  = 3
0
-SPH_SORT_EXTENDED    = 4
0
-
0
-# known attribute types
0
-SPH_ATTR_INTEGER    = 1
0
-SPH_ATTR_TIMESTAMP    = 2
0
-
0
-# known grouping functions
0
-SPH_GROUPBY_DAY       = 0
0
-SPH_GROUPBY_WEEK    = 1
0
-SPH_GROUPBY_MONTH    = 2
0
-SPH_GROUPBY_YEAR    = 3
0
-SPH_GROUPBY_ATTR    = 4
0
-
0
-
0
-class SphinxClient:
0
-  _host    = 'localhost'      # searchd host (default is "localhost")
0
-  _port    = 3312          # searchd port (default is 3312)
0
-  _offset    = 0            # how much records to seek from result-set start (default is 0)