searx_engine.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. Searx (all)
  3. @website https://github.com/asciimoo/searx
  4. @provide-api yes (https://asciimoo.github.io/searx/dev/search_api.html)
  5. @using-api yes
  6. @results JSON
  7. @stable yes (using api)
  8. @parse url, title, content
  9. """
  10. from json import loads
  11. from searx.engines import categories as searx_categories
  12. categories = searx_categories.keys()
  13. # search-url
  14. instance_urls = []
  15. instance_index = 0
  16. # do search-request
  17. def request(query, params):
  18. global instance_index
  19. params['url'] = instance_urls[instance_index % len(instance_urls)]
  20. params['method'] = 'POST'
  21. instance_index += 1
  22. params['data'] = {
  23. 'q': query,
  24. 'pageno': params['pageno'],
  25. 'language': params['language'],
  26. 'time_range': params['time_range'],
  27. 'category': params['category'],
  28. 'format': 'json'
  29. }
  30. return params
  31. # get response from search-request
  32. def response(resp):
  33. response_json = loads(resp.text)
  34. results = response_json['results']
  35. for i in ('answers', 'infoboxes'):
  36. results.extend(response_json[i])
  37. results.extend({'suggestion': s} for s in response_json['suggestions'])
  38. results.append({'number_of_results': response_json['number_of_results']})
  39. return results