stract.py 1.2 KB

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