Created
October 11, 2017 00:05
-
-
Save amuramatsu/db81196376cae1688b1425be38085f2a 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/env python3 | |
# -*- coding: utf-8 -*- | |
import requests | |
import sys | |
import argparse | |
from datetime import datetime | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'-f', '--full_description', action='store_true', | |
help="show full description of image") | |
parser.add_argument( | |
'-t', '--tags', type=int, default=-1, | |
help="show each tag information") | |
parser.add_argument( | |
'-s', '--search', action='store_true', | |
help="search image") | |
parser.add_argument( | |
'-n', '--count', type=int, default=10, | |
help="max search page count") | |
parser.add_argument( | |
"NAME", | |
help="package name") | |
DOCKER_HUB_API = "https://hub.docker.com/v2" | |
def get_docker_image_info(name, tags=False): | |
if name.find('/') < 0 : | |
name = "library/{}".format(name) | |
elif name.startswith("_/"): | |
name = "library/{}".format(name[2:]) | |
req = requests.get("{}/repositories/{}/{}".format( | |
DOCKER_HUB_API, name, "tags/" if tags else "")) | |
if req.status_code != 200: | |
return None | |
return req.json() | |
def get_docker_image_tags(name, pages=0): | |
data = get_docker_image_info(name, tags=True) | |
if data is None: | |
return [] | |
results = [] | |
page = 1 | |
while True: | |
results.extend(data['results']) | |
if (pages != 0 and page >= pages) or data['next'] is None: | |
break | |
req = requests.get(data['next']) | |
if req.status_code != 200: | |
raise Exception() | |
data = req.json() | |
page += 1 | |
return results | |
def search(term, pages=10): | |
req = requests.get("{}/search/repositories".format(DOCKER_HUB_API), | |
params={'query': term }) | |
if req.status_code != 200: | |
raise Exception() | |
results = [] | |
page = 1 | |
while True: | |
data = req.json() | |
results.extend(data["results"]) | |
if (pages != 0 and page >= pages) or data['next'] is None: | |
break | |
page += 1 | |
req = requests.get("{}/search/repositories".format(DOCKER_HUB_API), | |
params={'query': term, | |
'page': page }) | |
if req.status_code != 200: | |
raise Exception() | |
return results | |
def format_date(s): | |
if s is None: | |
return "unknown" | |
return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%fZ").strftime( | |
'%Y-%m-%d %H:%M:%S') | |
def main(): | |
opt = parser.parse_args() | |
if opt.search: | |
images = search(opt.NAME, pages=opt.count) | |
for img in images: | |
print( | |
"{} STARS:{}, PULLS:{}, OFFICIAL:{}, AUTOMATED:{}".format( | |
img['repo_name'], img['star_count'], img['pull_count'], | |
img['is_official'], img['is_automated'])) | |
sys.exit(0) | |
info = get_docker_image_info(opt.NAME) | |
if info is None: | |
print("Image {} is not found".format(opt.NAME)) | |
sys.exit(1) | |
print(info['description']) | |
if opt.full_description: | |
print(info['full_description']) | |
if opt.tags >= 0: | |
tags = get_docker_image_tags(opt.NAME, pages=opt.tags) | |
print("\nTAGS:") | |
for t in tags: | |
print(" {}:{}".format(opt.NAME, t['name'])) | |
print(" date: {}".format(format_date(t.get('last_updated')))) | |
for img in t['images']: | |
print(" arch: {}/{}".format( | |
img.get('os', 'unknown'), | |
img.get('architecture', 'unknown'))) | |
print(" size: {:,}".format(img['size'])) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment