Files
dotfiles/bin/.bin/get_astrobin_potd.py
2020-04-14 09:24:56 -04:00

38 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import subprocess
from datetime import datetime
import requests
today = datetime.today().strftime("%Y-%m-%d")
home = os.path.expanduser("~")
target_path = f"{home}/var/media/backgrounds/astrobin/{today}.jpg"
# If the file for today already exists, just exit
if os.path.isfile(target_path):
print(f"Astrobin image for {today} already exists, skipping download")
exit()
root = "https://www.astrobin.com"
api_key = "3f542cbb23407bde6f20490f377366582dd1a54c"
api_secret = (
subprocess.check_output("pass personal/apikey/astrobin", shell=True)
.decode("utf-8")
.strip()
)
fmt = "json"
iotd_uri = f"{root}/api/v1/imageoftheday/?limit=1&api_key={api_key}&api_secret={api_secret}&format={fmt}"
r = requests.get(iotd_uri)
image_info_uri = r.json()["objects"][0]["image"]
r = requests.get(
f"{root}{image_info_uri}?api_key={api_key}&api_secret={api_secret}&format={fmt}"
)
image_uri = r.json()["url_real"]
img = requests.get(image_uri, stream=True)
handle = open(target_path, "wb")
for chunk in img.iter_content(chunk_size=512):
if chunk: # filter out keep-alive new chunks
handle.write(chunk)