get_astrobin_potd.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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/astrobin/{today}.jpg"
  9. # If the file for today already exists, just exit
  10. if os.path.isfile(target_path):
  11. print(f"Astrobin image for {today} already exists, skipping download")
  12. exit()
  13. root = "https://www.astrobin.com"
  14. api_key = "3f542cbb23407bde6f20490f377366582dd1a54c"
  15. api_secret = (
  16. subprocess.check_output("pass personal/apikey/astrobin", shell=True)
  17. .decode("utf-8")
  18. .strip()
  19. )
  20. fmt = "json"
  21. iotd_uri = f"{root}/api/v1/imageoftheday/?limit=1&api_key={api_key}&api_secret={api_secret}&format={fmt}"
  22. r = requests.get(iotd_uri)
  23. image_info_uri = r.json()["objects"][0]["image"]
  24. r = requests.get(
  25. f"{root}{image_info_uri}?api_key={api_key}&api_secret={api_secret}&format={fmt}"
  26. )
  27. image_uri = r.json()["url_real"]
  28. img = requests.get(image_uri, stream=True)
  29. handle = open(target_path, "wb")
  30. for chunk in img.iter_content(chunk_size=512):
  31. if chunk: # filter out keep-alive new chunks
  32. handle.write(chunk)