From 446e9f00bade654e8dcdf7e13e97a715bfbddfa7 Mon Sep 17 00:00:00 2001 From: Midgard Date: Tue, 2 Feb 2021 15:48:27 +0100 Subject: [PATCH] Initial commit --- buienradar | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 buienradar diff --git a/buienradar b/buienradar new file mode 100755 index 0000000..241eec6 --- /dev/null +++ b/buienradar @@ -0,0 +1,48 @@ +#!/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}×tamp={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)