docker_hub.py 1.9 KB

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