metacpan.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """metacpan
  3. """
  4. from urllib.parse import urlunparse
  5. from json import dumps
  6. # about
  7. about = {
  8. "website": 'https://metacpan.org/',
  9. "wikidata_id": 'Q841507',
  10. "official_api_documentation": 'https://github.com/metacpan/metacpan-api/blob/master/docs/API-docs.md',
  11. "use_official_api": True,
  12. "require_api_key": False,
  13. "results": 'JSON',
  14. }
  15. # engine dependent config
  16. number_of_results = 20 # Don't put this over 5000
  17. categories = ["it", "packages"]
  18. disabled = True
  19. shortcut = "cpan"
  20. paging = True
  21. query_data_template = {
  22. 'query': {
  23. 'multi_match': {
  24. 'type': 'most_fields',
  25. 'fields': ['documentation', 'documentation.*'],
  26. 'analyzer': 'camelcase',
  27. }
  28. },
  29. 'filter': {
  30. 'bool': {
  31. 'must': [
  32. {'exists': {'field': 'documentation'}},
  33. {'term': {'status': 'latest'}},
  34. {'term': {'indexed': 1}},
  35. {'term': {'authorized': 1}},
  36. ]
  37. }
  38. },
  39. "sort": [
  40. {"_score": {"order": "desc"}},
  41. {"date": {"order": "desc"}},
  42. ],
  43. '_source': ['documentation', "abstract"],
  44. 'size': number_of_results,
  45. }
  46. search_url = urlunparse(["https", "fastapi.metacpan.org", "/v1/file/_search", "", "", ""])
  47. def request(query, params):
  48. params["url"] = search_url
  49. params["method"] = "POST"
  50. query_data = query_data_template
  51. query_data["query"]["multi_match"]["query"] = query
  52. query_data["from"] = (params["pageno"] - 1) * number_of_results
  53. params["data"] = dumps(query_data)
  54. return params
  55. def response(resp):
  56. results = []
  57. search_results = resp.json()["hits"]["hits"]
  58. for result in search_results:
  59. fields = result["_source"]
  60. module = fields["documentation"]
  61. results.append(
  62. {
  63. "url": "https://metacpan.org/pod/" + module,
  64. "title": module,
  65. "content": fields.get("abstract", ""),
  66. }
  67. )
  68. return results