dotfiles/nixos/i3/openweathermap-fullfeatured.sh

100 lines
2.9 KiB
Bash
Raw Normal View History

2020-04-18 11:59:42 +01:00
#!/bin/sh
2019-08-24 08:56:37 +01:00
2022-06-04 12:51:36 +01:00
OPENWEATHERMAP_KEY=`cat /run/secrets/openweathermap-api-key`
2019-08-24 08:56:37 +01:00
KEY="$OPENWEATHERMAP_KEY"
CITY=""
UNITS="metric"
SYMBOL="°"
API="https://api.openweathermap.org/data/2.5"
2020-04-17 23:21:06 +01:00
CURL=`which curl`
CURL="$CURL -sf"
CUT=`which cut`
DATE=`which date`
JQ=`which jq`
UNAME=`which uname`
2019-08-24 08:56:37 +01:00
get_icon() {
case $1 in
2022-04-07 21:44:20 +01:00
01d) icon="";;
01n) icon="";;
02d) icon="";;
02n) icon="";;
03d) icon="";;
03n) icon="";;
04d) icon="";;
04n) icon="";;
09d) icon="";;
09n) icon="";;
10d) icon="";;
10n) icon="";;
11d) icon="";;
11n) icon="";;
13d) icon="";;
13n) icon="";;
50d) icon="";;
50n) icon="";;
2020-09-26 12:40:24 +01:00
*) icon="";
2019-08-24 08:56:37 +01:00
esac
echo $icon
}
get_duration() {
osname=$($UNAME -s)
case $osname in
*BSD) $DATE -r "$1" -u +%H:%M;;
*) $DATE --date="@$1" -u +%H:%M;;
esac
}
if [ -n "$CITY" ]; then
if [ "$CITY" -eq "$CITY" ] 2>/dev/null; then
CITY_PARAM="id=$CITY"
else
CITY_PARAM="q=$CITY"
fi
current=$($CURL "$API/weather?appid=$KEY&$CITY_PARAM&units=$UNITS")
forecast=$($CURL "$API/forecast?appid=$KEY&$CITY_PARAM&units=$UNITS&cnt=1")
else
location=$($CURL https://location.services.mozilla.com/v1/geolocate?key=geoclue)
if [ -n "$location" ]; then
location_lat="$(echo "$location" | $JQ '.location.lat')"
location_lon="$(echo "$location" | $JQ '.location.lng')"
current=$($CURL "$API/weather?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS")
forecast=$($CURL "$API/forecast?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS&cnt=1")
fi
fi
if [ -n "$current" ] && [ -n "$forecast" ]; then
current_temp=$(echo "$current" | $JQ ".main.temp" | $CUT -d "." -f 1)
2020-09-26 12:40:24 +01:00
current_feels=$(echo "$current" | $JQ ".main.feels_like" | $CUT -d "." -f 1)
2019-08-24 08:56:37 +01:00
current_icon=$(echo "$current" | $JQ -r ".weather[0].icon")
2022-04-07 22:33:40 +01:00
current_text=$(echo "$current" | $JQ -r ".weather[0].description")
2019-08-24 08:56:37 +01:00
forecast_temp=$(echo "$forecast" | $JQ ".list[].main.temp" | $CUT -d "." -f 1)
2020-09-26 12:40:24 +01:00
forecast_feels=$(echo "$forecast" | $JQ ".list[].main.feels_like" | $CUT -d "." -f 1)
2019-08-24 08:56:37 +01:00
forecast_icon=$(echo "$forecast" | $JQ -r ".list[].weather[0].icon")
if [ "$current_temp" -gt "$forecast_temp" ]; then
2022-04-07 22:33:40 +01:00
trend="getting colder"
2022-09-03 23:11:54 +01:00
echo "$current_text $(get_icon "$current_icon") $current_temp($current_feels)$SYMBOL => $(get_icon "$forecast_icon") $forecast_temp($forecast_feels)$SYMBOL"
2019-08-24 08:56:37 +01:00
elif [ "$forecast_temp" -gt "$current_temp" ]; then
2022-04-07 22:33:40 +01:00
trend="getting warmer"
2022-09-03 23:11:54 +01:00
echo "$current_text $(get_icon "$current_icon") $current_temp($current_feels)$SYMBOL => $(get_icon "$forecast_icon") $forecast_temp($forecast_feels)$SYMBOL"
2020-09-26 13:31:58 +01:00
else
2022-04-07 22:33:40 +01:00
trend="stable weather"
2022-09-03 23:11:54 +01:00
echo "$current_text $(get_icon "$current_icon") $current_temp($current_feels)$SYMBOL"
2019-08-24 08:56:37 +01:00
fi
fi