Преглед изворни кода

[fix] autocomplete encoding

Adam Tauber пре 11 година
родитељ
комит
8e2d996e27
2 измењених фајлова са 12 додато и 13 уклоњено
  1. 10
    7
      searx/autocomplete.py
  2. 2
    6
      searx/webapp.py

+ 10
- 7
searx/autocomplete.py Прегледај датотеку

@@ -1,13 +1,15 @@
1 1
 from lxml import etree
2 2
 from requests import get
3 3
 from json import loads
4
+from urllib import urlencode
4 5
 
5 6
 
6 7
 def dbpedia(query):
7 8
     # dbpedia autocompleter
8
-    autocomplete_url = 'http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString={q}'  # noqa
9
+    autocomplete_url = 'http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?'  # noqa
9 10
 
10
-    response = get(autocomplete_url.format(q=query))
11
+    response = get(autocomplete_url
12
+                   + urlencode(dict(QueryString=query)))
11 13
 
12 14
     results = []
13 15
 
@@ -21,14 +23,15 @@ def dbpedia(query):
21 23
 
22 24
 def google(query):
23 25
     # google autocompleter
24
-    autocomplete_url = 'http://suggestqueries.google.com/complete/search?client=toolbar&q={q}'  # noqa
26
+    autocomplete_url = 'http://suggestqueries.google.com/complete/search?client=toolbar&'  # noqa
25 27
 
26
-    response = get(autocomplete_url.format(q=query))
28
+    response = get(autocomplete_url
29
+                   + urlencode(dict(q=query)))
27 30
 
28 31
     results = []
29 32
 
30 33
     if response.ok:
31
-        dom = etree.fromstring(response.content)
34
+        dom = etree.fromstring(response.text)
32 35
         results = dom.xpath('//suggestion/@data')
33 36
 
34 37
     return results
@@ -36,9 +39,9 @@ def google(query):
36 39
 
37 40
 def wikipedia(query):
38 41
     # wikipedia autocompleter
39
-    url = 'https://en.wikipedia.org/w/api.php?action=opensearch&search={q}&limit=10&namespace=0&format=json'  # noqa
42
+    url = 'https://en.wikipedia.org/w/api.php?action=opensearch&{0}&limit=10&namespace=0&format=json'  # noqa
40 43
 
41
-    resp = loads(get(url.format(q=query)).text)
44
+    resp = loads(get(url.format(urlencode(dict(q=query)))).text)
42 45
     return resp[1]
43 46
 
44 47
 

+ 2
- 6
searx/webapp.py Прегледај датотеку

@@ -252,7 +252,7 @@ def autocompleter():
252 252
         request_data = request.args
253 253
 
254 254
     # TODO fix XSS-vulnerability
255
-    query = request_data.get('q')
255
+    query = request_data.get('q', '').encode('utf-8')
256 256
 
257 257
     if not query:
258 258
         return
@@ -262,11 +262,7 @@ def autocompleter():
262 262
     if not completer:
263 263
         return
264 264
 
265
-    try:
266
-        results = completer(query)
267
-    except Exception, e:
268
-        print e
269
-        results = []
265
+    results = completer(query)
270 266
 
271 267
     if request_data.get('format') == 'x-suggestions':
272 268
         return Response(json.dumps([query, results]),