npm.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """npms.io
  3. """
  4. from urllib.parse import urlencode
  5. from dateutil import parser
  6. about = {
  7. "website": "https://npms.io/",
  8. "wikidata_id": "Q7067518",
  9. "official_api_documentation": "https://api-docs.npms.io/",
  10. "use_official_api": False,
  11. "require_api_key": False,
  12. "results": "JSON",
  13. }
  14. categories = ['it', 'packages']
  15. # engine dependent config
  16. paging = True
  17. page_size = 25
  18. search_api = "https://api.npms.io/v2/search?"
  19. def request(query: str, params):
  20. args = urlencode(
  21. {
  22. 'from': (params["pageno"] - 1) * page_size,
  23. 'q': query,
  24. 'size': page_size,
  25. }
  26. )
  27. params['url'] = search_api + args
  28. return params
  29. def response(resp):
  30. results = []
  31. content = resp.json()
  32. for entry in content["results"]:
  33. package = entry["package"]
  34. publishedDate = package.get("date")
  35. if publishedDate:
  36. publishedDate = parser.parse(publishedDate)
  37. tags = list(entry.get("flags", {}).keys()) + package.get("keywords", [])
  38. results.append(
  39. {
  40. "template": "packages.html",
  41. "url": package["links"]["npm"],
  42. "title": package["name"],
  43. 'package_name': package["name"],
  44. "content": package.get("description", ""),
  45. "version": package.get("version"),
  46. "maintainer": package.get("author", {}).get("name"),
  47. 'publishedDate': publishedDate,
  48. "tags": tags,
  49. "homepage": package["links"].get("homepage"),
  50. "source_code_url": package["links"].get("repository"),
  51. }
  52. )
  53. return results