1
+ import requests
2
+ import argparse
3
+ import json
4
+ from datetime import datetime
5
+
6
+
7
+ def get_all_images_from_dockerhub (account_name :str ):
8
+ """
9
+ function to retrieve docker images list
10
+ :param account_name: docker hub acccount name. default dockerofkrishnadhas
11
+ :return:
12
+ """
13
+ api_endpoint = f'https://hub.docker.com/v2/repositories/{ account_name } /'
14
+ # print(api_endpoint)
15
+ # Define pagination parameters
16
+ per_page = 50 # Number of records per page
17
+ page = 1 # Initial page number
18
+ docker_image_names_list = []
19
+ while True :
20
+ params = {
21
+ 'per_page' : per_page , # Number of results per page
22
+ 'page' : page # Page number
23
+ }
24
+ # API call
25
+ response = requests .get (url = api_endpoint , params = params )
26
+ response_json = response .json () ## Github repo details
27
+
28
+ # Checking the API status code
29
+ if response .status_code == 200 :
30
+ print (f"API request successful on { api_endpoint } " )
31
+ # print(response_json)
32
+ else :
33
+ print (f"API request failed with status code { response .status_code } :" )
34
+ # print(response_json)
35
+ break
36
+
37
+ for images in response_json ['results' ]:
38
+ docker_image_names_list .append (images ['name' ])
39
+
40
+ page += 1 # Move to the next page
41
+ file_name = f'docker_images_tags_{ account_name } _results.json'
42
+ with open (file_name , 'w' ) as json_file :
43
+ json .dump (response_json ['results' ], json_file , indent = 4 )
44
+ # Break the loop if no more pages
45
+ if len (response_json ['results' ]) < per_page :
46
+ break
47
+ print (f'Total number of images under { account_name } is : { len (docker_image_names_list )} ' )
48
+
49
+ return docker_image_names_list
50
+
51
+ def get_image_tags_from_repository (account_name : str ):
52
+ """
53
+ get the tags from a docker image
54
+ :param account_name:
55
+ :return:
56
+ """
57
+ docker_image_names_list = get_all_images_from_dockerhub (account_name = account_name )
58
+ docker_image_tag_list = []
59
+ for image in docker_image_names_list :
60
+ tag_endpoint = f'https://hub.docker.com/v2/namespaces/{ account_name } /repositories/{ image } /tags'
61
+ # print(tag_endpoint)
62
+ response = requests .get (tag_endpoint )
63
+ # Checking the API status code
64
+ if response .status_code == 200 :
65
+ print (f"API request successful on { tag_endpoint } " )
66
+ # print(response_json)
67
+ else :
68
+ print (f"API request failed with status code { response .status_code } :" )
69
+ # print(response_json)
70
+ break
71
+ response_json = response .json ()
72
+ response_json_results = response_json ['results' ]
73
+ tag_count = response_json ['count' ]
74
+ print (f'Number of tags of { account_name } /{ image } is : { tag_count } ' )
75
+ for item in response_json_results :
76
+ tag = item ['name' ]
77
+ docker_image_tag_list .append (f'{ account_name } /{ image } :{ tag } ' )
78
+ file_name = f'docker_images_details_{ account_name } .json'
79
+ with open (file_name , 'w' ) as json_file :
80
+ json .dump (docker_image_tag_list , json_file , indent = 4 )
81
+ return docker_image_tag_list
82
+
83
+ def date_time ():
84
+ """ Simple function to print time """
85
+ now = datetime .now ()
86
+ current_time = now .strftime ("%B %d %Y - %H:%M:%S" )
87
+ return current_time
88
+
89
+
90
+ def main ():
91
+ """ To test the code"""
92
+ parser = argparse .ArgumentParser ("Retrieve Docker images and tags from dockerhub registry using python" )
93
+ parser .add_argument ("--account_name" , help = "dockerhub user name" , required = True , type = str )
94
+
95
+ args = parser .parse_args ()
96
+ account_name = args .account_name
97
+ starting_time = date_time ()
98
+ print (f"Proccess to retrieve Docker images and tags from dockerhub registry started at { starting_time } IST......" )
99
+ docker_image_tag_list = get_image_tags_from_repository (account_name )
100
+ print (docker_image_tag_list )
101
+ ending_time = date_time ()
102
+ print (f"Proccess to retrieve Docker images and tags from dockerhub registry completed at { ending_time } IST......" )
103
+
104
+ if __name__ == "__main__" :
105
+ main ()
0 commit comments