il_post.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Engine for Il Post, a largely independent online Italian newspaper.
  3. To use this engine add the following entry to your engines
  4. list in ``settings.yml``:
  5. .. code:: yaml
  6. - name: il post
  7. engine: il_post
  8. shortcut: pst
  9. disabled: false
  10. """
  11. from urllib.parse import urlencode
  12. from searx.result_types import EngineResults
  13. engine_type = "online"
  14. language_support = False
  15. categories = ["news"]
  16. paging = True
  17. page_size = 10
  18. time_range_support = True
  19. time_range_args = {"month": "pub_date:ultimi_30_giorni", "year": "pub_date:ultimo_anno"}
  20. search_api = "https://api.ilpost.org/search/api/site_search/?"
  21. about = {
  22. "website": "https://www.ilpost.it",
  23. "wikidata_id": "Q3792882",
  24. "official_api_documentation": None,
  25. "use_official_api": True,
  26. "require_api_key": False,
  27. "results": "JSON",
  28. "language": "it",
  29. }
  30. def request(query, params):
  31. query_params = {
  32. "qs": query,
  33. "pg": params["pageno"],
  34. "sort": "date_d",
  35. "filters": "ctype:articoli",
  36. }
  37. if params["time_range"]:
  38. if params["time_range"] not in time_range_args:
  39. return None
  40. query_params["filters"] += f";{time_range_args.get(params['time_range'], 'pub_date:da_sempre')}"
  41. params["url"] = search_api + urlencode(query_params)
  42. return params
  43. def response(resp) -> EngineResults:
  44. res = EngineResults()
  45. json_data = resp.json()
  46. for result in json_data["docs"]:
  47. res.add(
  48. res.types.MainResult(
  49. url=result["link"],
  50. title=result["title"],
  51. content=result.get("summary", ""),
  52. thumbnail=result.get("image"),
  53. )
  54. )
  55. return res