json_engine.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from collections import Iterable
  2. from json import loads
  3. from sys import version_info
  4. from searx.url_utils import urlencode
  5. from searx.utils import to_string
  6. if version_info[0] == 3:
  7. unicode = str
  8. search_url = None
  9. url_query = None
  10. content_query = None
  11. title_query = None
  12. paging = False
  13. suggestion_query = ''
  14. results_query = ''
  15. # parameters for engines with paging support
  16. #
  17. # number of results on each page
  18. # (only needed if the site requires not a page number, but an offset)
  19. page_size = 1
  20. # number of the first page (usually 0 or 1)
  21. first_page_num = 1
  22. def iterate(iterable):
  23. if type(iterable) == dict:
  24. it = iterable.items()
  25. else:
  26. it = enumerate(iterable)
  27. for index, value in it:
  28. yield str(index), value
  29. def is_iterable(obj):
  30. if type(obj) == str:
  31. return False
  32. if type(obj) == unicode:
  33. return False
  34. return isinstance(obj, Iterable)
  35. def parse(query):
  36. q = []
  37. for part in query.split('/'):
  38. if part == '':
  39. continue
  40. else:
  41. q.append(part)
  42. return q
  43. def do_query(data, q):
  44. ret = []
  45. if not q:
  46. return ret
  47. qkey = q[0]
  48. for key, value in iterate(data):
  49. if len(q) == 1:
  50. if key == qkey:
  51. ret.append(value)
  52. elif is_iterable(value):
  53. ret.extend(do_query(value, q))
  54. else:
  55. if not is_iterable(value):
  56. continue
  57. if key == qkey:
  58. ret.extend(do_query(value, q[1:]))
  59. else:
  60. ret.extend(do_query(value, q))
  61. return ret
  62. def query(data, query_string):
  63. q = parse(query_string)
  64. return do_query(data, q)
  65. def request(query, params):
  66. query = urlencode({'q': query})[2:]
  67. fp = {'query': query}
  68. if paging and search_url.find('{pageno}') >= 0:
  69. fp['pageno'] = (params['pageno'] - 1) * page_size + first_page_num
  70. params['url'] = search_url.format(**fp)
  71. params['query'] = query
  72. return params
  73. def response(resp):
  74. results = []
  75. json = loads(resp.text)
  76. if results_query:
  77. rs = query(json, results_query)
  78. if not len(rs):
  79. return results
  80. for result in rs[0]:
  81. try:
  82. url = query(result, url_query)[0]
  83. title = query(result, title_query)[0]
  84. except:
  85. continue
  86. try:
  87. content = query(result, content_query)[0]
  88. except:
  89. content = ""
  90. results.append({
  91. 'url': to_string(url),
  92. 'title': to_string(title),
  93. 'content': to_string(content),
  94. })
  95. else:
  96. for url, title, content in zip(
  97. query(json, url_query),
  98. query(json, title_query),
  99. query(json, content_query)
  100. ):
  101. results.append({
  102. 'url': to_string(url),
  103. 'title': to_string(title),
  104. 'content': to_string(content),
  105. })
  106. if not suggestion_query:
  107. return results
  108. for suggestion in query(json, suggestion_query):
  109. results.append({'suggestion': suggestion})
  110. return results