2019-10-05 14:02:23 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
get_icon() {
|
|
|
|
case $1 in
|
2019-10-11 13:39:15 +00:00
|
|
|
01d) icon="滛";; # Sun clear
|
|
|
|
01n) icon="";; # Moon clear
|
|
|
|
02d) icon="";; # Sun cloudy
|
|
|
|
02n) icon="";; # Moon cloudy
|
|
|
|
03*) icon="";; # Cloud
|
|
|
|
04*) icon="";; # Clouds
|
|
|
|
09d) icon="";; # Day, cloud, rain, wind
|
|
|
|
09n) icon="";; # Night, cloud, rain, wind
|
|
|
|
10d) icon="";; # Day rain
|
|
|
|
10n) icon="";; # Night rain
|
|
|
|
11d) icon="";; # Day, rain, lightning
|
|
|
|
11n) icon="";; # Night, rain, lightning
|
|
|
|
13d) icon="";; # Day, snow
|
|
|
|
13n) icon="";; # Night, snow
|
|
|
|
50d) icon="";; # Day, fog
|
|
|
|
50n) icon="";; # Night, fog
|
|
|
|
*) icon=""; # Sun clear, default icon
|
2019-10-05 14:02:23 +00: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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
KEY="3fbb0282dfd1ba8295bcb93b329fd152"
|
|
|
|
CITY="5149222"
|
|
|
|
UNITS="imperial"
|
|
|
|
SYMBOL="°"
|
|
|
|
|
|
|
|
API="https://api.openweathermap.org/data/2.5"
|
|
|
|
|
|
|
|
if [ -n "$CITY" ]; then
|
|
|
|
if [ "$CITY" -eq "$CITY" ] 2>/dev/null; then
|
|
|
|
CITY_PARAM="id=$CITY"
|
|
|
|
else
|
|
|
|
CITY_PARAM="q=$CITY"
|
|
|
|
fi
|
|
|
|
|
|
|
|
current=$(curl -sf "$API/weather?appid=$KEY&$CITY_PARAM&units=$UNITS")
|
|
|
|
forecast=$(curl -sf "$API/forecast?appid=$KEY&$CITY_PARAM&units=$UNITS&cnt=1")
|
|
|
|
else
|
|
|
|
location=$(curl -sf 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 -sf "$API/weather?appid=$KEY&lat=$location_lat&lon=$location_lon&units=$UNITS")
|
|
|
|
forecast=$(curl -sf "$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)
|
|
|
|
current_icon=$(echo "$current" | jq -r ".weather[0].icon")
|
|
|
|
|
|
|
|
forecast_temp=$(echo "$forecast" | jq ".list[].main.temp" | cut -d "." -f 1)
|
|
|
|
forecast_icon=$(echo "$forecast" | jq -r ".list[].weather[0].icon")
|
|
|
|
|
|
|
|
|
|
|
|
if [ "$current_temp" -gt "$forecast_temp" ]; then
|
2019-10-11 13:39:15 +00:00
|
|
|
trend="" # Arrow, down-right slant
|
2019-10-05 14:02:23 +00:00
|
|
|
elif [ "$forecast_temp" -gt "$current_temp" ]; then
|
2019-10-05 14:32:20 +00:00
|
|
|
trend="" # Arrow, up-right slant
|
2019-10-05 14:02:23 +00:00
|
|
|
else
|
2019-10-19 18:57:14 +00:00
|
|
|
trend="" # Arrow, left-to-right
|
2019-10-05 14:02:23 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
sun_rise=$(echo "$current" | jq ".sys.sunrise")
|
|
|
|
sun_set=$(echo "$current" | jq ".sys.sunset")
|
|
|
|
now=$(date +%s)
|
|
|
|
|
2020-03-23 23:52:33 +00:00
|
|
|
# Format of sunset/rise display and icons
|
2019-10-05 14:02:23 +00:00
|
|
|
if [ "$sun_rise" -gt "$now" ]; then
|
2020-03-23 23:52:33 +00:00
|
|
|
daytime=" $(get_duration "$((sun_rise-now))")" # Sun rise
|
2019-10-05 14:02:23 +00:00
|
|
|
elif [ "$sun_set" -gt "$now" ]; then
|
2020-03-23 23:52:33 +00:00
|
|
|
daytime=" $(get_duration "$((sun_set-now))")" # Sun set
|
2019-10-05 14:02:23 +00:00
|
|
|
else
|
2020-03-23 23:52:33 +00:00
|
|
|
daytime=" $(get_duration "$((sun_rise-now))")" # Unknown
|
2019-10-05 14:02:23 +00:00
|
|
|
fi
|
|
|
|
|
2020-03-23 23:52:33 +00:00
|
|
|
# Format of resulting forecast
|
|
|
|
echo "$(get_icon "$current_icon") $current_temp$SYMBOL $trend $(get_icon "$forecast_icon") $forecast_temp$SYMBOL$daytime"
|
2020-02-19 15:54:50 +00:00
|
|
|
#echo "$(get_icon "$current_icon") $current_temp$SYMBOL $trend $(get_icon "$forecast_icon") $forecast_temp$SYMBOL $daytime"
|
2019-10-05 14:02:23 +00:00
|
|
|
fi
|