Skip to content

Instantly share code, notes, and snippets.

@eddie-atkinson
Created March 6, 2021 13:44
Show Gist options
  • Save eddie-atkinson/c122fa3c39564be8940f2c24d4224d30 to your computer and use it in GitHub Desktop.
Save eddie-atkinson/c122fa3c39564be8940f2c24d4224d30 to your computer and use it in GitHub Desktop.
import requests
from hashlib import md5
from pathlib import Path
image_hashes = set()
out_path = Path("./images")
out_path.mkdir(exist_ok=True, parents=True)
# Wouldn't it be nice to do this in an async way :/
while len(image_hashes) < 100:
r = requests.get("https://dog.ceo/api/breeds/image/random")
resp_url = r.json()["message"]
image_data = requests.get(resp_url).content
image_hash = md5(image_data).hexdigest()
img_name = resp_url.split("/")[-1]
if image_hash not in image_hashes:
with open(f"{(out_path/img_name).absolute()}", "wb") as outfile:
outfile.write(image_data)
image_hashes.add(image_hash)
print(len(image_hashes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment