jroshell/scripts/ags-weather-fetch.sh
2026-05-28 15:07:21 +02:00

78 lines
2.1 KiB
Bash
Executable file

#!/bin/bash
LAT="48.8566"
LON="2.3522"
TZ="Europe/Paris"
CACHE_FILE="${HOME}/.cache/ags/weather.json"
mkdir -p "$(dirname "$CACHE_FILE")"
RESPONSE=$(curl -s "https://api.open-meteo.com/v1/forecast\
?latitude=${LAT}&longitude=${LON}\
&current_weather=true\
&daily=temperature_2m_max,temperature_2m_min,weathercode,precipitation_probability_max\
&timezone=${TZ}&forecast_days=4")
[ -z "$RESPONSE" ] && echo "Error: empty response" && exit 1
j() { echo "$RESPONSE" | jq "$1"; }
jr() { echo "$RESPONSE" | jq -r "$1"; }
code_to_icon() {
case $1 in
0) echo "☀️" ;;
1 | 2 | 3) echo "🌤️" ;;
45 | 48) echo "🌫️" ;;
51 | 53 | 55 | 61 | 63 | 65 | 80 | 81 | 82) echo "🌧️" ;;
71 | 73 | 75 | 77 | 85 | 86) echo "❄️" ;;
95 | 96 | 99) echo "⛈️" ;;
*) echo "❓" ;;
esac
}
# Current
TEMP=$(j '.current_weather.temperature')
WIND=$(j '.current_weather.windspeed')
CODE=$(jr '.current_weather.weathercode')
ICON=$(code_to_icon "$CODE")
# Today
T_HIGH=$(j '.daily.temperature_2m_max[0]')
T_LOW=$(j '.daily.temperature_2m_min[0]')
T_RAIN=$(j '.daily.precipitation_probability_max[0]')
T_CODE=$(jr '.daily.weathercode[0]')
T_ICON=$(code_to_icon "$T_CODE")
# Forecast days 1-3
fc_entry() {
local i=$1
local date high low rain code icon day
date=$(jr ".daily.time[${i}]")
high=$(j ".daily.temperature_2m_max[${i}]")
low=$(j ".daily.temperature_2m_min[${i}]")
rain=$(j ".daily.precipitation_probability_max[${i}]")
code=$(jr ".daily.weathercode[${i}]")
icon=$(code_to_icon "$code")
day=$(date -d "$date" +%a)
echo "{\"day\":\"${day}\",\"high\":${high},\"low\":${low},\"rain\":${rain},\"icon\":\"${icon}\"}"
}
D1=$(fc_entry 1)
D2=$(fc_entry 2)
D3=$(fc_entry 3)
jq -n \
--argjson temp "$TEMP" \
--argjson wind "$WIND" \
--arg icon "$ICON" \
--argjson high "$T_HIGH" \
--argjson low "$T_LOW" \
--argjson rain "$T_RAIN" \
--arg today_icon "$T_ICON" \
--argjson d1 "$D1" \
--argjson d2 "$D2" \
--argjson d3 "$D3" \
'{
current: { temp: $temp, wind: $wind, icon: $icon },
today: { high: $high, low: $low, rain_pct: $rain, icon: $today_icon },
forecast: [$d1, $d2, $d3]
}' >"$CACHE_FILE"