stract.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Stract is an independent open source search engine. At this state, it's
  3. still in beta and hence this implementation will need to be updated once beta
  4. ends.
  5. """
  6. from json import dumps
  7. from searx.utils import searx_useragent
  8. about = {
  9. "website": "https://stract.com/",
  10. "use_official_api": True,
  11. "official_api_documentation": "https://stract.com/beta/api/docs/#/search/api",
  12. "require_api_key": False,
  13. "results": "JSON",
  14. }
  15. categories = ['general']
  16. paging = True
  17. search_url = "https://stract.com/beta/api/search"
  18. def request(query, params):
  19. params['url'] = search_url
  20. params['method'] = "POST"
  21. params['headers'] = {
  22. 'Accept': 'application/json',
  23. 'Content-Type': 'application/json',
  24. 'User-Agent': searx_useragent(),
  25. }
  26. params['data'] = dumps({'query': query, 'page': params['pageno'] - 1})
  27. return params
  28. def response(resp):
  29. results = []
  30. for result in resp.json()["webpages"]:
  31. results.append(
  32. {
  33. 'url': result['url'],
  34. 'title': result['title'],
  35. 'content': ''.join(fragment['text'] for fragment in result['snippet']['text']['fragments']),
  36. }
  37. )
  38. return results