| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | LOCATION=${WEATHER_LOCATION:-} |
| 6 | UNITS=${WEATHER_UNITS:-m} |
| 7 | cache="${XDG_CACHE_HOME:-$HOME/.cache}/waybar-weather.json" |
| 8 | |
| 9 | loc=${LOCATION// /+} |
| 10 | if data=$(curl -sf --max-time 8 "https://wttr.in/${loc}?format=j1&${UNITS}"); then |
| 11 | echo "$data" > "$cache" |
| 12 | elif [[ -f $cache ]]; then |
| 13 | data=$(cat "$cache") |
| 14 | else |
| 15 | echo '{"text":"","tooltip":"weather unavailable"}'; exit 0 |
| 16 | fi |
| 17 | |
| 18 | jq -c --arg u "$UNITS" ' |
| 19 | def icon(c): |
| 20 | {"113":"βοΈ","116":"β
","119":"βοΈ","122":"βοΈ","143":"π«","248":"π«","260":"π«", |
| 21 | "176":"π¦","263":"π¦","266":"π¦","293":"π¦","296":"π¦","353":"π¦", |
| 22 | "299":"π§","302":"π§","305":"π§","308":"π§","356":"π§","359":"π§", |
| 23 | "179":"π¨","227":"π¨","230":"βοΈ","323":"π¨","326":"π¨","329":"βοΈ","332":"βοΈ", |
| 24 | "335":"βοΈ","338":"βοΈ","368":"π¨","371":"βοΈ", |
| 25 | "200":"β","386":"β","389":"β","392":"β","395":"βοΈ"}[c] // "π‘"; |
| 26 | .current_condition[0] as $c |
| 27 | | .nearest_area[0] as $a |
| 28 | | (if $u == "u" then "Β°F" else "Β°C" end) as $deg |
| 29 | | (if $u == "u" then $c.temp_F else $c.temp_C end) as $t |
| 30 | | (if $u == "u" then $c.FeelsLikeF else $c.FeelsLikeC end) as $f |
| 31 | | { |
| 32 | text: "\(icon($c.weatherCode)) \($t)\($deg)", |
| 33 | tooltip: ("<b>\($a.areaName[0].value), \($a.country[0].value)</b>\n" |
| 34 | + "\($c.weatherDesc[0].value)\n" |
| 35 | + "Feels like \($f)\($deg) Β· Humidity \($c.humidity)%\n" |
| 36 | + "Wind \($c.windspeedKmph) km/h \($c.winddir16Point)\n\n" |
| 37 | + ([.weather[] | "\(.date): \(.mintempC)β\(.maxtempC)Β°C"] | join("\n"))) |
| 38 | }' <<<"$data" |