get_bing_potd.py 784 B

12345678910111213141516171819202122232425
  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/bing/{today}.jpg"
  9. # If the file for today already exists, just exit
  10. if os.path.isfile(target_path):
  11. print(f"Bing image for {today} already exists, skipping download")
  12. exit()
  13. iotd_uri = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=1&n=1"
  14. r = requests.get(iotd_uri)
  15. image_info_uri = r.json()["images"][0]["url"]
  16. img = requests.get(f"https://www.bing.com{image_info_uri}", stream=True)
  17. handle = open(target_path, "wb")
  18. for chunk in img.iter_content(chunk_size=512):
  19. if chunk: # filter out keep-alive new chunks
  20. handle.write(chunk)