microsoft_learn.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Engine for Microsoft Learn, Microsoft's technical knowledge base.
  3. To use this engine add the following entry to your engines list
  4. in ``settings.yml``:
  5. .. code:: yaml
  6. - name: microsoft learn
  7. engine: microsoft_learn
  8. shortcut: msl
  9. disabled: false
  10. """
  11. from urllib.parse import urlencode
  12. from searx.result_types import EngineResults
  13. engine_type = "online"
  14. language_support = True
  15. categories = ["it"]
  16. paging = True
  17. page_size = 10
  18. time_range_support = False
  19. search_api = "https://learn.microsoft.com/api/search?"
  20. about = {
  21. "website": "https://learn.microsoft.com",
  22. "wikidata_id": "Q123663245",
  23. "official_api_documentation": None,
  24. "use_official_api": False,
  25. "require_api_key": False,
  26. "results": "JSON",
  27. }
  28. def request(query, params):
  29. if params['language'] == 'all':
  30. params['language'] = 'en-us'
  31. query_params = [
  32. ("search", query),
  33. ("locale", params["language"]),
  34. ("scoringprofile", "semantic-answers"),
  35. ("facet", "category"),
  36. ("facet", "products"),
  37. ("facet", "tags"),
  38. ("$top", "10"),
  39. ("$skip", (params["pageno"] - 1) * page_size),
  40. ("expandScope", "true"),
  41. ("includeQuestion", "false"),
  42. ("applyOperator", "false"),
  43. ("partnerId", "LearnSite"),
  44. ]
  45. params["url"] = search_api + urlencode(query_params)
  46. return params
  47. def response(resp) -> EngineResults:
  48. res = EngineResults()
  49. json_data = resp.json()
  50. for result in json_data["results"]:
  51. res.add(res.types.MainResult(url=result["url"], title=result["title"], content=result.get("description", "")))
  52. return res