get_unsplash_potd.py 679 B

123456789101112131415161718192021222324
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. from datetime import datetime
  5. import requests
  6. today = datetime.today().strftime("%Y-%m-%d")
  7. home = os.path.expanduser("~")
  8. target_path = f"{home}/var/media/backgrounds/unsplash/{today}.jpg"
  9. # If the file for today already exists, just exit
  10. if os.path.isfile(target_path):
  11. print(f"Unsplash image for {today} already exists, skipping download")
  12. exit()
  13. root = "https://source.unsplash.com"
  14. iotd_uri = f"{root}/random"
  15. img = requests.get(iotd_uri, stream=True)
  16. handle = open(target_path, "wb")
  17. for chunk in img.iter_content(chunk_size=512):
  18. if chunk: # filter out keep-alive new chunks
  19. handle.write(chunk)