crates.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Cargo search on crates.io"""
  3. from collections import OrderedDict
  4. from urllib.parse import urlencode
  5. from dateutil import parser
  6. about = {
  7. "website": "https://crates.io/",
  8. "wikidata_id": None,
  9. "official_api_documentation": "https://crates.io/data-access",
  10. "use_official_api": True,
  11. "require_api_key": False,
  12. "results": "JSON",
  13. }
  14. categories = ["it", "packages", "cargo"]
  15. # engine dependent config
  16. paging = True
  17. page_size = 10
  18. search_url = "https://crates.io/api/v1/crates"
  19. linked_terms = OrderedDict(
  20. [
  21. ("homepage", "Project homepage"),
  22. ("documentation", "Documentation"),
  23. ("repository", "Source code"),
  24. ]
  25. )
  26. def request(query: str, params):
  27. args = urlencode({"page": params["pageno"], "q": query, "per_page": page_size})
  28. params["url"] = f"{search_url}?{args}"
  29. return params
  30. def response(resp):
  31. results = []
  32. for package in resp.json()["crates"]:
  33. published_date = package.get("updated_at")
  34. published_date = parser.parse(published_date)
  35. links = {}
  36. for k, v in linked_terms.items():
  37. l = package.get(k)
  38. if l:
  39. links[v] = l
  40. results.append(
  41. {
  42. "template": "packages.html",
  43. "url": f'https://crates.io/crates/{package["name"]}',
  44. "title": package["name"],
  45. "package_name": package["name"],
  46. "tags": package["keywords"],
  47. "content": package["description"],
  48. "version": package["newest_version"] or package["max_version"] or package["max_stable_version"],
  49. "publishedDate": published_date,
  50. "links": links,
  51. }
  52. )
  53. return results