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

58 lines
1.2 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 "$url")
echo "$RES" | gojq '.'
}
lookup_zipcode() {
# lookup based on zipcode
url="http://api.openweathermap.org/geo/1.0/zip?zip=$ZIPCODE&appid=$API_KEY"
RES=$(curl --silent "$url")
echo "$RES" | gojq '.'
}
get_weather() {
# calls server for weather based on $LAT, $LONG
url="https://api.openweathermap.org/data/2.5/weather\?lat=$LAT\&lon=$LON\&appid=$API_KEY\&units=imperial"
RES=$(curl --silent "$url")
echo "$RES" | gojq '.'
}
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