browserless-buienradar/buienradar

49 lines
1.3 KiB
Text
Raw Permalink Normal View History

2021-02-02 15:48:27 +01:00
#!/usr/bin/env python3
from datetime import datetime, timezone, timedelta
from subprocess import run
from os import makedirs, path, chdir
import requests
URL_TEMPLATE = "https://image.buienradar.nl/2.0/image/single/RadarMapRainBE?extension=png&width=550&height=512&renderText=True&renderBranding=False&renderBackground=True&runTimestamp={0:%Y%m%d%H%M}&timestamp={0:%Y%m%d%H%M}"
IMAGE_PATH = "/tmp/buienradar/"
def show_progress(progress):
bar_width = 20
if progress > 0.99999:
bar_tokens = "=" * bar_width
else:
bar_tokens = "=" * int(bar_width * progress) + ">"
percentage = int(100 * progress)
print(f"\rLoading images: [{bar_tokens:<{bar_width}}] {percentage: >3}%", end="")
now = datetime.now(timezone.utc)
now_rounded = datetime(now.year, now.month, now.day, now.hour, (now.minute // 5) * 5, 0, 0)
times = [now_rounded + timedelta(minutes=minutes) for minutes in range(-55, 125, 5)]
session = requests.Session()
makedirs(IMAGE_PATH, exist_ok=True)
chdir(IMAGE_PATH)
files = []
for i, time in enumerate(times):
show_progress(i / len(times))
url = URL_TEMPLATE.format(time)
r = session.get(url)
r.raise_for_status()
filename = "{:%Y-%m-%d %H:%M} UTC".format(time)
with open(filename, "wb") as file:
file.write(r.content)
files.append(filename)
show_progress(1)
print()
run(["sxiv", "-n", "12", *files], check=True)