aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/i3blocks
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-06-06 13:19:38 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-06-06 13:19:38 -0500
commit59a5c3ec4e2841151f8234ae6fbcbe1426a750e3 (patch)
tree36c75ce2ef9e4cec495fbdea96c526298b7cc794 /i3blocks
parent89c94c83531e2a0f0751079e975e28142f3c87dc (diff)
feat(i3blocks): add wttr script for proper icons
Diffstat (limited to 'i3blocks')
-rw-r--r--i3blocks/.config/i3blocks/config2
-rwxr-xr-xi3blocks/.local/lib/i3blocks/i3blocks-wttr154
2 files changed, 155 insertions, 1 deletions
diff --git a/i3blocks/.config/i3blocks/config b/i3blocks/.config/i3blocks/config
index 3765819..f8c7ee4 100644
--- a/i3blocks/.config/i3blocks/config
+++ b/i3blocks/.config/i3blocks/config
@@ -6,7 +6,7 @@ max_length=30
interval=persist
[weather]
-command=curl -s wttr.in/?m\&format="%c+%t\n" | sed 's/\s\++\(.*\)C/ \1/'
+command=$SCRIPT_DIR/i3blocks-wttr
interval=600
[miniflux]
diff --git a/i3blocks/.local/lib/i3blocks/i3blocks-wttr b/i3blocks/.local/lib/i3blocks/i3blocks-wttr
new file mode 100755
index 0000000..1151a43
--- /dev/null
+++ b/i3blocks/.local/lib/i3blocks/i3blocks-wttr
@@ -0,0 +1,154 @@
+#!/usr/bin/env python3
+
+import os
+import subprocess
+import time
+
+import requests
+
+ICON = "\U000f046b" # 󰑫
+
+WWO_CODE = {
+ "113": "Sunny",
+ "116": "PartlyCloudy",
+ "119": "Cloudy",
+ "122": "VeryCloudy",
+ "143": "Fog",
+ "176": "LightShowers",
+ "179": "LightSleetShowers",
+ "182": "LightSleet",
+ "185": "LightSleet",
+ "200": "ThunderyShowers",
+ "227": "LightSnow",
+ "230": "HeavySnow",
+ "248": "Fog",
+ "260": "Fog",
+ "263": "LightShowers",
+ "266": "LightRain",
+ "281": "LightSleet",
+ "284": "LightSleet",
+ "293": "LightRain",
+ "296": "LightRain",
+ "299": "HeavyShowers",
+ "302": "HeavyRain",
+ "305": "HeavyShowers",
+ "308": "HeavyRain",
+ "311": "LightSleet",
+ "314": "LightSleet",
+ "317": "LightSleet",
+ "320": "LightSnow",
+ "323": "LightSnowShowers",
+ "326": "LightSnowShowers",
+ "329": "HeavySnow",
+ "332": "HeavySnow",
+ "335": "HeavySnowShowers",
+ "338": "HeavySnow",
+ "350": "LightSleet",
+ "353": "LightShowers",
+ "356": "HeavyShowers",
+ "359": "HeavyRain",
+ "362": "LightSleetShowers",
+ "365": "LightSleetShowers",
+ "368": "LightSnowShowers",
+ "371": "HeavySnowShowers",
+ "374": "LightSleetShowers",
+ "377": "LightSleet",
+ "386": "ThunderyShowers",
+ "389": "ThunderyHeavyRain",
+ "392": "ThunderySnowShowers",
+ "395": "HeavySnowShowers",
+}
+
+WEATHER_SYMBOL_WI_DAY = {
+ "Unknown": "",
+ "Cloudy": "",
+ "Fog": "",
+ "HeavyRain": "",
+ "HeavyShowers": "",
+ "HeavySnow": "",
+ "HeavySnowShowers": "",
+ "LightRain": "",
+ "LightShowers": "",
+ "LightSleet": "",
+ "LightSleetShowers": "",
+ "LightSnow": "",
+ "LightSnowShowers": "",
+ "PartlyCloudy": "",
+ "Sunny": "",
+ "ThunderyHeavyRain": "",
+ "ThunderyShowers": "",
+ "ThunderySnowShowers": "",
+ "VeryCloudy": "",
+}
+
+WEATHER_SYMBOL_WI_NIGHT = {
+ "Unknown": "",
+ "Cloudy": "",
+ "Fog": "",
+ "HeavyRain": "",
+ "HeavyShowers": "",
+ "HeavySnow": "",
+ "HeavySnowShowers": "",
+ "LightRain": "",
+ "LightShowers": "",
+ "LightSleet": "",
+ "LightSleetShowers": "",
+ "LightSnow": "",
+ "LightSnowShowers": "",
+ "PartlyCloudy": "",
+ "Sunny": "",
+ "ThunderyHeavyRain": "",
+ "ThunderyShowers": "",
+ "ThunderySnowShowers": "",
+ "VeryCloudy": "",
+}
+
+
+def parse_data() -> str:
+ data = requests.get("https://wttr.in/?format=j1").json()
+
+ current = next(iter(data.get("current_condition", [])), None)
+
+ if current is None:
+ return ""
+
+ code = current.get("weatherCode", "0")
+ key = WWO_CODE.get(code, "Unknown")
+
+ local_obs_date, local_obs_time = current.get("localObsDateTime").split(maxsplit=1)
+ astronomy = next(
+ iter(
+ next(
+ w.get("astronomy")
+ for w in data.get("weather", [])
+ if w.get("date", None) == local_obs_date and w.get("astronomy", None)
+ )
+ ),
+ None,
+ )
+
+ if astronomy is None:
+ return None
+
+ local = time.strptime(local_obs_time, "%I:%M %p")
+ sunrise = time.strptime(astronomy.get("sunrise"), "%I:%M %p")
+ sunset = time.strptime(astronomy.get("sunset"), "%I:%M %p")
+
+ if local > sunrise and local < sunset:
+ icon = WEATHER_SYMBOL_WI_DAY.get(key)
+ else:
+ icon = WEATHER_SYMBOL_WI_NIGHT.get(key)
+
+ if temp := current.get("temp_C"):
+ return " {} {}°C ".format(icon, temp)
+
+
+def main():
+ try:
+ print(parse_data())
+ except Exception:
+ print()
+
+
+if __name__ == "__main__":
+ main()