get_astrobin_potd.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python3
  2. import os
  3. from datetime import datetime
  4. import requests
  5. import subprocess
  6. today = datetime.today().strftime("%Y-%m-%d")
  7. target_path = f"/home/powellc/var/inbox/astrobin/{today}.jpg"
  8. # If the file for today already exists, just exit
  9. if os.path.isfile(target_path):
  10. print(f"Astrobin image for {today} already exists, skipping download")
  11. exit()
  12. root = "https://www.astrobin.com"
  13. api_key = "3f542cbb23407bde6f20490f377366582dd1a54c"
  14. api_secret = (
  15. subprocess.check_output("pass personal/apikey/astrobin", shell=True)
  16. .decode("utf-8")
  17. .strip()
  18. )
  19. fmt = "json"
  20. iotd_uri = f"{root}/api/v1/imageoftheday/?limit=1&api_key={api_key}&api_secret={api_secret}&format={fmt}"
  21. r = requests.get(iotd_uri)
  22. image_info_uri = r.json()["objects"][0]["image"]
  23. r = requests.get(
  24. f"{root}{image_info_uri}?api_key={api_key}&api_secret={api_secret}&format={fmt}"
  25. )
  26. image_uri = r.json()["url_real"]
  27. img = requests.get(image_uri, stream=True)
  28. handle = open(target_path, "wb")
  29. for chunk in img.iter_content(chunk_size=512):
  30. if chunk: # filter out keep-alive new chunks
  31. handle.write(chunk)