hex.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """hex.pm"""
  3. from urllib.parse import urlencode
  4. from dateutil import parser
  5. about = {
  6. # pylint: disable=line-too-long
  7. "website": "https://hex.pm/",
  8. "wikidata_id": None,
  9. "official_api_documentation": "https://github.com/hexpm/hexpm/blob/main/lib/hexpm_web/controllers/api/package_controller.ex",
  10. "use_official_api": True,
  11. "require_api_key": False,
  12. "results": "JSON",
  13. }
  14. categories = ["it", "packages"]
  15. # engine dependent config
  16. paging = True
  17. search_url = "https://hex.pm/api/packages/"
  18. # Valid values: name inserted_at updated_at total_downloads recent_downloads
  19. sort_criteria = "recent_downloads"
  20. page_size = 10
  21. linked_terms = {
  22. # lower-case : replacement
  23. "author": "Author",
  24. "bitbucket": "Bitbucket",
  25. "bug tracker": "Issue tracker",
  26. "changelog": "Changelog",
  27. "doc": "Documentation",
  28. "docs": "Documentation",
  29. "documentation": "Documentation",
  30. "github repository": "GitHub",
  31. "github": "GitHub",
  32. "gitlab": "GitLab",
  33. "issues": "Issue tracker",
  34. "project source code": "Source code",
  35. "repository": "Source code",
  36. "scm": "Source code",
  37. "sourcehut": "SourceHut",
  38. "sources": "Source code",
  39. "sponsor": "Sponsors",
  40. "sponsors": "Sponsors",
  41. "website": "Homepage",
  42. }
  43. def request(query: str, params):
  44. args = urlencode({"page": params["pageno"], "per_page": page_size, "sort": sort_criteria, "search": query})
  45. params["url"] = f"{search_url}?{args}"
  46. return params
  47. def response(resp):
  48. results = []
  49. for package in resp.json():
  50. meta = package["meta"]
  51. published_date = package.get("updated_at")
  52. published_date = parser.parse(published_date)
  53. links = {linked_terms.get(k.lower(), k): v for k, v in meta.get("links").items()}
  54. results.append(
  55. {
  56. "template": "packages.html",
  57. "url": package["html_url"],
  58. "title": package["name"],
  59. "package_name": package["name"],
  60. "content": meta.get("description", ""),
  61. "version": meta.get("latest_version"),
  62. "maintainer": ", ".join(meta.get("maintainers", [])),
  63. "publishedDate": published_date,
  64. "license_name": ", ".join(meta.get("licenses", [])),
  65. "homepage": package["docs_html_url"],
  66. "links": links,
  67. }
  68. )
  69. return results