Created
June 27, 2019 19:02
-
-
Save jgamblin/9146462bf3e607374f27840f2f0d0244 to your computer and use it in GitHub Desktop.
Find The Most Pulled Containers From Docker Hub
This file contains hidden or 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
import csv | |
import re | |
import requests | |
import time | |
import os | |
from datetime import timedelta | |
from more_itertools import unique_everseen | |
from operator import itemgetter | |
start = time.time() | |
containers = [] | |
index = [] | |
def _get_containers(): | |
global containers | |
page = 1 | |
while page <= 10: | |
params = ( | |
('page_size', '100'), | |
('page', (page)), | |
('q', '+'), | |
('source', 'community'), | |
('type', 'imgage,bundle'), | |
('sort', 'popularity'), | |
) | |
response = requests.get( | |
'https://store.docker.com/api/content/v1/products/search', | |
params=params) | |
data = response.json() | |
try: | |
for summaries in data['summaries']: | |
try: | |
name = summaries['name'] | |
popularity = summaries['popularity'] | |
popularity = format(popularity, ",") | |
containers.append([name, popularity, page]) | |
except KeyError: | |
continue | |
except KeyError: | |
continue | |
page = page + 1 | |
print(page) | |
time.sleep(1) | |
containers = list(unique_everseen(containers)) | |
containers = sorted(containers, key=itemgetter(1)) | |
def _add_to_index(): | |
_get_containers() | |
global containers | |
global index | |
containerstemp = [] | |
print("Building Index") | |
for elem in containers: | |
container, popularity, page, = elem | |
url = 'https://hub.docker.com/v2/repositories/%s' % container | |
response = requests.get((url)) | |
data = response.json() | |
pulls = (data['pull_count']) | |
pulls = format(pulls, ',') | |
last_updated = (data['last_updated']) | |
try: | |
last_updated = re.sub(r'\s*T.*', '', last_updated) | |
except TypeError: | |
last_updated = "" | |
containerstemp.append([container, pulls, last_updated]) | |
containers = containerstemp | |
def _write_csv(): | |
global containers | |
_add_to_index() | |
with open('popular.csv', 'w', newline='') as csvfile: | |
writer = csv.DictWriter(csvfile, | |
fieldnames=["Container", | |
"Pulls", | |
"Last Updated", | |
]) | |
writer.writeheader() | |
writer = csv.writer(csvfile) | |
writer.writerows(containers) | |
def main(): | |
_write_csv() | |
_cleanup() | |
elapsed = (time.time() - start) | |
print("\n\nDone In:") | |
print(str(timedelta(seconds=elapsed))) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment