docker_hub.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Docker Hub (IT)
  3. """
  4. # pylint: disable=use-dict-literal
  5. from urllib.parse import urlencode
  6. from dateutil import parser
  7. about = {
  8. "website": 'https://hub.docker.com',
  9. "wikidata_id": 'Q100769064',
  10. "official_api_documentation": 'https://docs.docker.com/registry/spec/api/',
  11. "use_official_api": True,
  12. "require_api_key": False,
  13. "results": 'JSON',
  14. }
  15. categories = ['it', 'packages'] # optional
  16. paging = True
  17. base_url = "https://hub.docker.com"
  18. page_size = 10
  19. def request(query, params):
  20. args = {
  21. "query": query,
  22. "from": page_size * (params['pageno'] - 1),
  23. "size": page_size,
  24. }
  25. params['url'] = f"{base_url}/api/search/v3/catalog/search?{urlencode(args)}"
  26. return params
  27. def response(resp):
  28. '''post-response callback
  29. resp: requests response object
  30. '''
  31. results = []
  32. json_resp = resp.json()
  33. for item in json_resp.get("results", []):
  34. image_source = item.get("source")
  35. is_official = image_source in ["store", "official"]
  36. popularity_infos = [f"{item.get('star_count', 0)} stars"]
  37. architectures = []
  38. for rate_plan in item.get("rate_plans", []):
  39. pull_count = rate_plan.get("repositories", [{}])[0].get("pull_count")
  40. if pull_count:
  41. popularity_infos.insert(0, f"{pull_count} pulls")
  42. architectures.extend(arch['name'] for arch in rate_plan.get("architectures", []) if arch['name'])
  43. result = {
  44. 'template': 'packages.html',
  45. 'url': base_url + ("/_/" if is_official else "/r/") + item.get("slug", ""),
  46. 'title': item.get("name"),
  47. 'content': item.get("short_description"),
  48. 'thumbnail': item["logo_url"].get("large") or item["logo_url"].get("small"),
  49. 'package_name': item.get("name"),
  50. 'maintainer': item["publisher"].get("name"),
  51. 'publishedDate': parser.parse(item.get("updated_at") or item.get("created_at")),
  52. 'popularity': ', '.join(popularity_infos),
  53. 'tags': architectures,
  54. }
  55. results.append(result)
  56. return results