weather/client_weather.sh
2023-01-13 15:59:14 -05:00

65 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# server version, calls server for actual API info
usage() {
cat <<EOF
usage: $0 [-c COORD_PAIR][-s QUERY][-z ZIPCODE]
Queries OpenWeatherMapAPI for weather or lat/long
options:
-c, --coords searches for weather based on COORD_PAIR
-z, --zipcode returns the coordinate pair(s) for given zipcode
-s, --search returns the coordinate pair(s) for QUERY
COORD_PAIR is a string of the format "LAT,LON"
QUERY is a city, state or city to search
EOF
}
WORKING_DIR="$HOME/.local/share/weather"
get_location() {
# no search, use IP
url="http://ip-api.com/csv/?fields=252"
# search provided, feed to script
}
get_weather() {
# calls server for weather based on $LAT, $LONG
}
# getting weather
CUR_TIMESTAMP=$(date +%s)
# only update weather every 30 seconds
if [[ $CUR_TIMESTAMP -gt $EXPIRATION || -z $EXPIRATION ]] ; then
WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial)
echo $WEATHER
TEMP=$(echo $WEATHER | gojq -r '.main.temp')
HUMIDITY=$(echo $WEATHER | gojq -r '.main.humidity')
ICON=$(echo $WEATHER | gojq -r '.weather[0].icon')
EXPIRATION=$(($CUR_TIMESTAMP+30))
printf 'TEMP=%s\nHUMIDITY=%s\nICON=%s\nEXPIRATION=%s\n' "$TEMP" "$HUMIDITY" "$ICON" "$EXPIRATION" >"$HOME/.dotfiles/bin/weather/.env.weather"
fi
# shifting longform
for arg in "$@"; do
shift
case "$arg" in
'--zipcode') set -- "$@" "-z" ;;
'--coords') set -- "$@" "-c" ;;
'--search') set -- "$@" "-s" ;;
*) set -- "$@" "$arg" ;;
esac
done
while getopts "o:a:" arg; do
case ${arg} in
esac
done