docker_hub.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. search_url = base_url + "api/content/v1/products/search?{query}&type=image&page_size=25"
  19. def request(query, params):
  20. params['url'] = search_url.format(query=urlencode(dict(q=query, page=params["pageno"])))
  21. params["headers"]["Search-Version"] = "v3"
  22. return params
  23. def response(resp):
  24. '''post-response callback
  25. resp: requests response object
  26. '''
  27. results = []
  28. body = resp.json()
  29. for item in body.get("summaries", []):
  30. filter_type = item.get("filter_type")
  31. is_official = filter_type in ["store", "official"]
  32. result = {
  33. 'template': 'packages.html',
  34. 'url': base_url + ("_/" if is_official else "r/") + item.get("slug", ""),
  35. 'title': item.get("name"),
  36. 'content': item.get("short_description"),
  37. 'img_src': item["logo_url"].get("large") or item["logo_url"].get("small"),
  38. 'package_name': item.get("name"),
  39. 'maintainer': item["publisher"].get("name"),
  40. 'publishedDate': parser.parse(item.get("updated_at") or item.get("created_at")),
  41. 'popularity': item.get("pull_count", "0") + " pulls",
  42. 'tags': [arch['name'] for arch in item["architectures"]],
  43. }
  44. results.append(result)
  45. return results