on master help sign in
[fix] convert json engine result attributes to string - closes #1006
commit0969e50c5bc949d2c15eaed8e6a7def6b9da00fa authorAdam Tauber <asciimoo@gmail.com> dateFri 1 Dec 2017, 20:45 (9 years ago) parenta065fcd
2 files changed+23−2
searx/engines/json_engine.py+11−2 searx/utils.py+12
searx/engines/json_engine.py+11−2
@@ -2,6 +2,7 @@ from collections import Iterable
22from json import loads
33from sys import version_info
44from searx.url_utils import urlencode
5+from searx.utils import to_string
56
67if version_info[0] == 3:
78 unicode = str
@@ -111,14 +112,22 @@ def response(resp):
111112 content = query(result, content_query)[0]
112113 except:
113114 content = ""
114− results.append({'url': url, 'title': title, 'content': content})
115+ results.append({
116+ 'url': to_string(url),
117+ 'title': to_string(title),
118+ 'content': to_string(content),
119+ })
115120 else:
116121 for url, title, content in zip(
117122 query(json, url_query),
118123 query(json, title_query),
119124 query(json, content_query)
120125 ):
121− results.append({'url': url, 'title': title, 'content': content})
126+ results.append({
127+ 'url': to_string(url),
128+ 'title': to_string(title),
129+ 'content': to_string(content),
130+ })
122131
123132 if not suggestion_query:
124133 return results
searx/utils.py+12
@@ -7,6 +7,7 @@ import re
77from babel.dates import format_date
88from codecs import getincrementalencoder
99from imp import load_source
10+from numbers import Number
1011from os.path import splitext, join
1112from random import choice
1213import sys
@@ -336,3 +337,14 @@ def new_hmac(secret_key, url):
336337 return hmac.new(bytes(secret_key), url, hashlib.sha256).hexdigest()
337338 else:
338339 return hmac.new(bytes(secret_key, 'utf-8'), url, hashlib.sha256).hexdigest()
340+
341+
342+def to_string(obj):
343+ if isinstance(obj, basestring):
344+ return obj
345+ if isinstance(obj, Number):
346+ return unicode(obj)
347+ if hasattr(obj, '__str__'):
348+ return obj.__str__()
349+ if hasattr(obj, '__repr__'):
350+ return obj.__repr__()