weather/archived/weather
2023-01-13 15:59:14 -05:00

144 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
usage() {
cat <<EOF
Usage: wttr [-s] [-d] [-u] [-i] [-f] [-h]
Queries the weather and outputs it in a Nerd Font compaitable way
Options:
-p, --print print the current weather information
-s, --setloc=<LOC> change location used for wttr.in query
will use IP if left blank
-d, --disploc=<LOC> show location used in query
-f, --force force refresh weather data
-h, --help Show this help message
EOF
}
WORKING_DIR="$HOME/.local/share/weather"
WEATHER_FILE="$WORKING_DIR/.weather"
SYMBOL_FILE="$WORKING_DIR/symbols.json"
# timeout in seconds
TIMEOUT=60
get_wttr() {
if [[ -n "$ZONE_URL" && -z "$UPDATE" ]] ; then
# will default to IP
set_location
fi
if [[ -z "$ZONE_URL" || -n "$UPDATE" ]] ; then
# getting grid points if update or zone is empty
url="https://api.weather.gov/points/$LATITUDE,$LONGITUDE"
GRID=$(curl --silent -fL "$url")
query="{\
station: .properties.observationStations,\
city: .properties.relativeLocation.properties.city,\
state: .properties.relativeLocation.properties.state}"
res=$(echo "$GRID" | gojq "$query")
CITY=$(echo "$res" | gojq '.city')
STATE=$(echo "$res" | gojq '.state')
STATION_URL=$(echo "$res" | gojq '.station' | tr -d '"')
OBSERVATION_URL=$(curl --silent -fL "$STATION_URL" | gojq '.observationStations[0]' | tr -d '"')
fi
WEATHER=$(curl --silent -fL "$OBSERVATION_URL/observations/latest" | gojq '.features[]')
echo "$WEATHER" | gojq '.'
query="{\
temp: .temperature.value,\
humidity: .relativeHumidity.value,\
icon: .icon}"
PARSED=$(echo "$WEATHER" | gojq "$query")
TEMP=$(echo "$PARSED" | gojq '.temp')
# C -> F, unit check eventually
TEMP=$(echo "$TEMP * (9/5) + 32" | bc -l)
HUMIDITY=$(echo "$PARSED" | gojq '.humidity')
#echo "$PARSED" | gojq '.'
TOD=$(echo "$PARSED" | gojq '.icon' | awk -F / '{print $6}')
ICON=$(echo "$PARSED" | gojq '.icon' | awk -F / '{print $7}' | awk -F ? '{print $1}')
EXPIRATION=$(($(date +%s) + $TIMEOUT))
printf 'TEMP=%s\nHUMIDITY=%s\nICON=%s\nLOCATION=%s\nZONE_URL=%s\nCITY=%s\nSTATE=%s\nTOD=%s\nEXPIRATION=%s\n' "$TEMP" "$HUMIDITY" "$ICON" "$LOCATION" "$ZONE_URL" "$CITY" "$STATE" "$TOD" "$EXPIRATION" > "$WEATHER_FILE"
}
set_location() {
# updates location of weather query to $new_loc, can be empty
LOCATION="$1"
UPDATE="true"
url="https://v2.wttr.in/$LOCATION?format=j1"
query="{\
lat: .nearest_area[0].latitude,\
lon: .nearest_area[0].longitude}"
WTTR=$(curl --silent -fL "$url" | gojq "$query")
# Grabbing env vars
LATITUDE=$(echo "$WTTR" | gojq '.lat' | tr -d ' "')
LONGITUDE=$(echo "$WTTR" | gojq '.lon' | tr -d ' "')
get_wttr
}
display_location() {
# updates location to display to $disp_loc
printf 'Searching %s, (%s, %s)\n' "$LOCATION" "$CITY" "$STATE"
}
load_wttr() {
# loads and checks weather
# load env, error on new install
source "$WEATHER_FILE" 2>/dev/null
# check for expiration
if [[ $(date +%s) -ge $EXPIRATION ]] ; then
get_wttr
fi
}
print_wttr() {
# stripping and reformatting quotations
if [[ -n "$TOD" && -n "$ICON" ]] ; then
query=$(printf '.%s.%s' $TOD $ICON)
SYMBOL=$(cat "$SYMBOL_FILE" | gojq "$query" | tr -d '"')
fi
UNIT=$(echo -e "\ufa04")
HUMID_SYMB=$(echo -e "\ue373")
printf '%s %.1f%s %0.1f %s in %s, %s\n' "$SYMBOL" "$TEMP" "$UNIT" "$HUMIDITY" "$HUMID_SYMB" "$CITY" "$STATE"
}
load_wttr
while [[ "$1" != "" ]]; do
case $1 in
-h | --help)
usage
;;
-p | --print)
print_wttr
;;
-s | --setloc)
shift
set_location "$1"
;;
-d | --disploc)
shift
display_location
;;
-f | --force)
get_wttr
;;
*)
echo "Error: wttr $@"
usage
;;
esac
shift
done