docker_hub.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Docker Hub (IT)
  4. """
  5. # pylint: disable=use-dict-literal
  6. from urllib.parse import urlencode
  7. from dateutil import parser
  8. about = {
  9. "website": 'https://hub.docker.com',
  10. "wikidata_id": 'Q100769064',
  11. "official_api_documentation": 'https://docs.docker.com/registry/spec/api/',
  12. "use_official_api": True,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. categories = ['it', 'packages'] # optional
  17. paging = True
  18. base_url = "https://hub.docker.com/"
  19. search_url = base_url + "api/content/v1/products/search?{query}&type=image&page_size=25"
  20. def request(query, params):
  21. params['url'] = search_url.format(query=urlencode(dict(q=query, page=params["pageno"])))
  22. params["headers"]["Search-Version"] = "v3"
  23. return params
  24. def response(resp):
  25. '''post-response callback
  26. resp: requests response object
  27. '''
  28. results = []
  29. body = resp.json()
  30. for item in body.get("summaries", []):
  31. filter_type = item.get("filter_type")
  32. is_official = filter_type in ["store", "official"]
  33. result = {
  34. 'template': 'packages.html',
  35. 'url': base_url + ("_/" if is_official else "r/") + item.get("slug", ""),
  36. 'title': item.get("name"),
  37. 'content': item.get("short_description"),
  38. 'img_src': item["logo_url"].get("large") or item["logo_url"].get("small"),
  39. 'package_name': item.get("name"),
  40. 'maintainer': item["publisher"].get("name"),
  41. 'publishedDate': parser.parse(item.get("updated_at") or item.get("created_at")),
  42. 'popularity': item.get("pull_count", "0") + " pulls",
  43. 'tags': [arch['name'] for arch in item["architectures"]],
  44. }
  45. results.append(result)
  46. return results