weather/server_weather.sh
2023-01-13 16:26:09 -05:00

60 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# server version, calls server for actual API info
WORKING_DIR="$HOME/.local/share/weather"
# sets $API_KEY
source "$WORKING_DIR/.env.key"
lookup_location() {
# search for a location
url="http://api.openweathermap.org/geo/1.0/direct?q=$SEARCH&appid=$API_KEY"
res=$(curl --silent -fL "$url")
echo "$res"
}
lookup_zipcode() {
# lookup based on zipcode
url="http://api.openweathermap.org/geo/1.0/zip?zip=$ZIPCODE&appid=$API_KEY"
res=$(curl --silent -fL "$url")
echo "$res"
}
get_weather() {
# calls server for weather based on $COORDS
LAT=$(echo "$COORDS" | awk -F , '{print $1}')
LON=$(echo "$COORDS" | awk -F , '{print $2}')
url="https://api.openweathermap.org/data/2.5/weather\?lat=$LAT\&lon=$LON\&appid=$API_KEY\&units=imperial"
res=$(curl --silent -fL "$url")
echo "$res"
}
while getopts "c:s:z:" arg; do
case "$opt" in
'c' )
COORDS="$OPTARG"
;;
's' )
SEARCH="$OPTARG"
;;
'z' )
ZIPCODE="$OPTARG"
;;
'?' )
echo "ERROR: UNRECOGNZIED ARGUMENT"
exit 1
;;
esac
done
if [[ -n "$COORDS" ]] ; then
get_weather
elif [[ -n "$SEARCH" ]] ; then
lookup_location
elif [[ -n "$ZIPCODE" ]] ; then
lookup_zipcode
fi
exit 0