Pārlūkot izejas kodu

initial implemention of autocompletion in opensearch.xml

Thomas Pointhuber 11 gadus atpakaļ
vecāks
revīzija
cc7f3cb617
2 mainītis faili ar 21 papildinājumiem un 5 dzēšanām
  1. 10
    1
      searx/templates/opensearch.xml
  2. 11
    4
      searx/webapp.py

+ 10
- 1
searx/templates/opensearch.xml Parādīt failu

@@ -6,9 +6,18 @@
6 6
   <LongName>searx metasearch</LongName>
7 7
   {% if method == 'get' %}
8 8
     <Url type="text/html" method="get" template="{{ host }}?q={searchTerms}"/>
9
+    <Url type="application/x-suggestions+json" method="get" template="{{ host }}autocompleter">
10
+        <Param name="format" value="x-suggestions" />
11
+        <Param name="q" value="{searchTerms}" />
12
+    </Url>
9 13
   {% else %}
10 14
     <Url type="text/html" method="post" template="{{ host }}">
11
-      <Param name="q" value="{searchTerms}" />
15
+        <Param name="q" value="{searchTerms}" />
16
+    </Url>
17
+    <!-- TODO, POST REQUEST doesn't work -->
18
+    <Url type="application/x-suggestions+json" method="get" template="{{ host }}autocompleter">
19
+        <Param name="format" value="x-suggestions" />
20
+        <Param name="q" value="{searchTerms}" />
12 21
     </Url>
13 22
   {% endif %}
14 23
 </OpenSearchDescription>

+ 11
- 4
searx/webapp.py Parādīt failu

@@ -233,10 +233,17 @@ def autocompleter():
233 233
     
234 234
     # TODO fix XSS-vulnerability, remove test code
235 235
     autocompleter.querry = request_data.get('q')
236
-    autocompleter.results = [autocompleter.querry]
237
-
238
-    return Response(json.dumps(autocompleter.results),
239
-                               mimetype='application/json')
236
+    autocompleter.results = []
237
+    
238
+    if autocompleter.querry:
239
+        autocompleter.results = [autocompleter.querry + "-searx",autocompleter.querry + " asfded",autocompleter.querry + " asdf"]
240
+    
241
+    if request_data.get('format') == 'x-suggestions':
242
+        return Response(json.dumps([autocompleter.querry,autocompleter.results]),
243
+                                   mimetype='application/json')
244
+    else:
245
+        return Response(json.dumps(autocompleter.results),
246
+                                   mimetype='application/json')
240 247
 
241 248
 
242 249
 @app.route('/preferences', methods=['GET', 'POST'])