Created
April 30, 2019 06:46
-
-
Save geberl/cf9f42b32bbabd0f0e7a0e68d24aafde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import datetime | |
import json | |
from urllib.parse import urlencode | |
from urllib.request import Request, urlopen | |
class DockerImageInfo(object): | |
def __init__(self, name: str, full_size: int, identifier: int, repository: int, creator: int, last_updater: int, | |
last_updated: str): | |
self.name = name | |
self.full_size = full_size | |
self.identifier = identifier | |
self.repository = repository | |
self.creator = creator | |
self.last_updater = last_updater | |
if last_updated: | |
self.last_updated = datetime.datetime.strptime(last_updated, '%Y-%m-%dT%H:%M:%S.%fZ') | |
else: | |
self.last_updated = None | |
def __repr__(self): | |
return '%s "%s" (%s bytes) @ %s by %s' % (self.identifier, self.name, self.full_size, self.last_updated, | |
self.last_updater) | |
# Step 1: Get access token | |
login_url = 'https://hub.docker.com/v2/users/login/' | |
login_post_data = {'username': 'geberl', 'password': 'supers3cret'} | |
request = Request(login_url, urlencode(login_post_data).encode()) | |
data = urlopen(request).read() | |
data_dict = json.loads(data.decode()) | |
token = data_dict['token'] | |
# print(token) | |
# Step 2: Get all tags of an image | |
# image_url = 'https://hub.docker.com/v2/repositories/jenkins/jenkins/tags/' # url for 3rd party image | |
image_url = 'https://hub.docker.com/v2/repositories/library/registry/tags/' # url for docker-maintained image | |
all_images = [] | |
while True: | |
request = Request(image_url) | |
request.add_header('Authorization', 'JWT %s' % token) | |
data = urlopen(request).read() | |
data_dict = json.loads(data.decode()) | |
for image in data_dict['results']: | |
# Hash is not available, just ID. | |
all_images.append(DockerImageInfo(name=image['name'], full_size=image['full_size'], identifier=image['id'], | |
repository=image['repository'], creator=image['creator'], | |
last_updater=image['last_updater'], last_updated=image['last_updated'])) | |
if data_dict['next']: | |
image_url = data_dict['next'] | |
else: | |
break | |
print(len(all_images)) | |
for image in all_images: | |
print(image) | |
print('---') | |
# Step 3: Get info about one specific image | |
# Doesn't contain any more info than the "all tags of an image" request before, is just way shorter | |
# image_tag_url = 'https://hub.docker.com/v2/repositories/library/openjdk/tags/8-jre/' | |
image_tag_url = 'https://hub.docker.com/v2/repositories/library/registry/tags/latest' | |
request = Request(image_tag_url) | |
request.add_header('Authorization', 'JWT %s' % token) | |
data = urlopen(request).read() | |
data_dict = json.loads(data.decode()) | |
print('Name: %s' % data_dict['name']) | |
print('Size: %s bytes' % data_dict['full_size']) | |
print('Last updated: %s' % data_dict['last_updated']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment