fixed to just set API key locally

This commit is contained in:
KeeganForelight 2023-01-15 17:30:17 -05:00
parent bc690b60d3
commit 73f8ae2f82
2 changed files with 137 additions and 149 deletions

View File

@ -1,69 +0,0 @@
#!/bin/bash
# server version, calls server for actual API info
WORKING_DIR="$HOME/.local/share/weather"
KEY_FILE="$WORKING_DIR/.env"
# set $API_KEY
if [[ ! -e "$KEY_FILE" ]] ; then
echo "No API key found!"
exit 1
fi
source "$KEY_FILE"
lookup_location() {
# search for a location
url="http://api.openweathermap.org/geo/1.0/direct?q=$SEARCH&appid=$API_KEY&limit=5"
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&limit=5"
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=$UNITS"
res=$(curl --silent -fL "$url")
echo "$res"
}
while getopts "c:s:z:u:" opt; do
case "$opt" in
'c' )
COORDS="$OPTARG"
;;
's' )
SEARCH="$OPTARG"
;;
'z' )
ZIPCODE="$OPTARG"
;;
'u' )
UNITS="$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

View File

@ -3,11 +3,13 @@
# client version, calls server for actual API info # client version, calls server for actual API info
usage() { usage() {
cat <<EOF cat <<EOF
usage: $0 [-p][-s QUERY | -z ZIPCODE][-u <c|f>][-i][-h] usage: $0 [-a KEY][-s QUERY | -z ZIPCODE][-u <c|f>][-p][-i][-l][-h]
Queries OpenWeatherMapAPI for weather or lat/long Queries OpenWeatherMapAPI for weather or lat/long
options: options:
-a, --apikey set the api key to use to KEY
-l, --location print the location being used
-p, --pretty enables nerdfont symbols -p, --pretty enables nerdfont symbols
-z, --zipcode returns the weather for ZIPCODE -z, --zipcode returns the weather for ZIPCODE
-s, --search returns the weather for QUERY -s, --search returns the weather for QUERY
@ -21,20 +23,13 @@ ex) -z "02139,US"
QUERY sets the location to the search QUERY sets the location to the search
QUERY format is "CITY,REGION,COUNTRY" where QUERY format is "CITY,REGION,COUNTRY" where
CITY is any city
REGION is Region or State
COUNTRY is ISO Country Code
to narrow results, try more specific searches
ex) -s "Cambridge" will return places in the UK
ex) -s "Cambridge,US" will return Cambridge, MA
EOF EOF
exit $1 exit $1
} }
WORKING_DIR="$HOME/.local/share/weather" WORKING_DIR="$HOME/.local/share/weather"
WEATHER_FILE="$WORKING_DIR/.env" WEATHER_FILE=".env"
# URL
WEATHER_CALL="https://api.openweathermap.org/data/2.5/weather?lat=$LAT&lon=$LON&appid=$API_KEY&units=$UNITS"
TIMEOUT=60 # timeout for info TIMEOUT=60 # timeout for info
TIME=$(date +%s) TIME=$(date +%s)
@ -43,6 +38,9 @@ if [[ ! -d "$WORKING_DIR" ]] ; then
mkdir -p "$WORKING_DIR" mkdir -p "$WORKING_DIR"
fi fi
# going to weather directory
cd "$WORKING_DIR"
print_weather() { print_weather() {
# prints the weather # prints the weather
@ -64,84 +62,94 @@ print_weather() {
printf '%s %.1f%s%d\ue373 in %s\n' $icon $TEMPERATURE "$temp_icon" "$HUMIDITY" "$CITY" printf '%s %.1f%s%d\ue373 in %s\n' $icon $TEMPERATURE "$temp_icon" "$HUMIDITY" "$CITY"
else else
# default printing # default printing
printf "%.1f *$UNIT %d%% Humidity in %s\n" $TEMPERATURE $HUMIDITY "$CITY" printf '%.1f*%s %d%% RH in %s\n' $TEMPERATURE $UNIT $HUMIDITY "$CITY"
fi fi
# save before exiting # save before exiting
save_info save_info
} }
print_location() {
# prints the location being used for the weather
# uses reverse geocoding
LOCATION_URL="https://api.openweathermap.org/geo/1.0/reverse?lat=$LAT&lon=$LON&appid=$API_KEY"
LOCATION=$(curl --silent -fL "$LOCATION_URL" | gojq '.[0] | del(.local_names)')
name=$(echo "$LOCATION" | gojq '.name' | tr -d '"')
state=$(echo "$LOCATION" | gojq '.state' | tr -d '"')
country=$(echo "$LOCATION" | gojq '.country' | tr -d '"')
printf 'Location is set to %s in %s, %s\n' "$name" "$state" "$country"
}
check_expiration() { check_expiration() {
# checks if the info is expired # checks if the info is expired
if [[ $TIME -gt $EXPIRATION ]] ; then if [[ $TIME -gt $EXPIRATION ]] ; then
# expired # expired
echo "expired"
get_weather get_weather
fi fi
} }
get_location() { get_location() {
# searching for user location # searching for user location
LOCATION_CALL="https://api.openweathermap.org/data/2.5/weather?$SEARCH&appid=$API_KEY" if [[ -n "$SEARCH" ]] ; then
g if [[ -n "$SEARCH" ]] ; then
# search # search
RESULTS=$($(curl --silent -fL "$LOCATION_CALL") | gojq "del(.[].local_names)") LOCATION_URL="https://api.openweathermap.org/geo/1.0/$SEARCH&appid=$API_KEY&limit=5"
NUM_RESULTS=$(echo "$RESULTS" | gojq '. | length') RESULTS=$(curl --silent -fL "$LOCATION_URL")
SELECTION=0 # check if its an array
is_array=$(echo "$RESULTS" | gojq 'if type=="array" then 1 else 0 end')
if [[ $NUM_RESULTS -gt 1 ]] ; then if [[ $is_array -eq 1 ]] ; then
# create menu for user # returned result is an array
for ((i=0; i < $NUM_RESULTS; i++)); do NUM_RESULTS=$(echo "$RESULTS" | gojq '. | length')
# adding options if [[ $NUM_RESULTS -gt 1 ]] ; then
res=$(echo "$RESULTS" | gojq ".[$i]") # provide menu for multiple results
name=$(echo "$res" | gojq '.name' | tr -d '"') for ((i=0; i < $NUM_RESULTS; i++)); do
state=$(echo "$res" | gojq '.state' | tr -d '"') # adding options
country=$(echo "$res" | gojq '.country' | tr -d '"') res=$(echo "$RESULTS" | gojq ".[$i]")
opt=$(printf '%s %s, %s' "$name" "$state" "$country") name=$(echo "$res" | gojq '.name' | tr -d '"')
OPTIONS+=($(($i+1)) "$opt") state=$(echo "$res" | gojq '.state' | tr -d '"')
done country=$(echo "$res" | gojq '.country' | tr -d '"')
opt=$(printf '%s %s, %s' "$name" "$state" "$country")
OPTIONS+=($(($i+1)) "$opt")
done
# executing dialog menu # executing dialog menu
exec 3>&1 exec 3>&1
SELECTION=$(dialog \ SELECTION=$(dialog \
--title 'Multiple Locations Found!' \ --title 'Multiple Locations Found!' \
--clear \ --clear \
--cancel-label 'Exit' \ --cancel-label 'Exit' \
--ok-label 'Select' \ --ok-label 'Select' \
--menu 'Please select the desired location. If none of the options look correct, try making refining your search by including a country code and/or region' 0 0 4 \ --menu 'Please select the desired location. If none of the options look correct, try making refining your search by including a country code and/or region' 0 0 4 \
"${OPTIONS[@]}" \ "${OPTIONS[@]}" \
2>&1 1>&3) 2>&1 1>&3)
exit_status=$? exit_status=$?
exec 3>&- exec 3>&-
clear clear
if [[ $exit_status -eq 1 || $exit_satus -eq 255 ]] ; then if [[ $exit_status -eq 1 || $exit_satus -eq 255 ]] ; then
# canceled or escaped # canceled or escaped
echo "Try again: $0 -s 'CITY,REGION,COUNTRY'" echo "Location not changed"
exit 0 exit 0
fi
# fix index
SELECTION=$(($SELECTION - 1))
fi fi
# updating result
# fix index RESULTS=$(echo "$RESULTS" | gojq ".[$SELECTION]")
SELECTION=$(($SELECTION - 1))
fi fi
# Update info # Update info
LAT=$(echo "$RESULTS" | gojq ".[$SELECTION].lat")
LON=$(echo "$RESULTS" | gojq ".[$SELECTION].lon")
CITY=$(echo "$RESULTS" | gojq ".[$SELECTION].name" | tr -d '"')
elif [[ -n "$ZIPCODE" ]] ; then
# searching for zipcode
RESULTS=$($WEATHER_CALL -z "$ZIPCODE")
LAT=$(echo "$RESULTS" | gojq ".lat") LAT=$(echo "$RESULTS" | gojq ".lat")
LON=$(echo "$RESULTS" | gojq ".lon") LON=$(echo "$RESULTS" | gojq ".lon")
CITY=$(echo "$RESULTS" | gojq ".name" | tr -d '"') CITY=$(echo "$RESULTS" | gojq ".name" | tr -d '"')
else else
# no search, default to user IP # no search, default to user IP
url="http://ip-api.com/csv/?fields=252" url="http://ip-api.com/csv/?fields=252"
res=$(curl --silent -fL "$url") res=$(curl --silent -fL "$url")
LAT=$(awk -F , '{print $5}' <<<"$res") LAT=$(awk -F , '{print $5}' <<<"$res")
@ -151,7 +159,7 @@ g if [[ -n "$SEARCH" ]] ; then
# check # check
if [[ -z "$LAT" || -z "$LON" ]] ; then if [[ -z "$LAT" || -z "$LON" ]] ; then
echo "No location found!" echo "No location found!" >&2
exit 1 exit 1
fi fi
@ -160,41 +168,65 @@ g if [[ -n "$SEARCH" ]] ; then
} }
get_weather() { get_weather() {
# calls server for weather based on $LAT, $LONG # calls api for weather based on $LAT, $LONG
if [[ -z "$LAT" || -z "$LON" ]] ; then if [[ -z "$LAT" || -z "$LON" ]] ; then
# no lat or lon # no lat or lon
get_location get_location
fi fi
# only update weather every 30 seconds
if [[ "$UNIT" =~ ^[Cc]$ ]] ; then if [[ "$UNIT" =~ ^[Cc]$ ]] ; then
# units set to celsius # units set to metric
UNIT="C" UNIT="C"
UNITS="metric" UNITS="metric"
else elif [[ "$UNIT" =~ ^[Ff]$ || -z "$UNIT" ]] ; then
# imperial # imperial (default)
UNIT="F" UNIT="F"
UNITS="imperial" UNITS="imperial"
else
printf 'Unit %s unrecognized\n' "$UNIT" >&2
exit 1
fi fi
WEATHER=$($WEATHER_CALL "-c $LAT,$LON -u $UNITS") WEATHER_URL="https://api.openweathermap.org/data/2.5/weather?lat=$LAT&lon=$LON&appid=$API_KEY&units=$UNITS"
echo "$WEATHER" | gojq '.'
WEATHER=$(curl --silent -fL "$WEATHER_URL")
TEMPERATURE=$(echo $WEATHER | gojq -r '.main.temp') TEMPERATURE=$(echo $WEATHER | gojq -r '.main.temp')
HUMIDITY=$(echo $WEATHER | gojq -r '.main.humidity') HUMIDITY=$(echo $WEATHER | gojq -r '.main.humidity')
WEATHER_ICON=$(echo $WEATHER | gojq -r '.weather[0].icon') WEATHER_ICON=$(echo $WEATHER | gojq -r '.weather[0].icon')
EXPIRATION=$(($TIME+$TIMEOUT)) EXPIRATION=$(($TIME+$TIMEOUT))
echo "$TEMPERATURE $HUMIDITY $WEATHER_ICON" }
update_key() {
# updates the API key used
printf 'Testing API key %s... ' "$API_KEY"
WEATHER_URL="https://api.openweathermap.org/data/2.5/weather?lat=42.3736&lon=71.1097&appid=$API_KEY"
CODE=$(curl --silent "$WEATHER_URL" | gojq '.cod' | tr -d '"')
if [[ $CODE -eq 401 ]] ; then
# API_KEY error
printf 'Error: Invalid API Key "%s"!\n' "$API_KEY" >&2
exit 1
fi
printf 'Success: Key Updated!\n'
# saving new key
save_info
} }
save_info() { save_info() {
# saves set env vars to files # saves set env vars to file
printf '%s\n' \ printf '%s\n' \
"API_KEY=$API_KEY" "API_KEY=$API_KEY" \
"PARSER=$PARSER" \
"LAT=$LAT" \ "LAT=$LAT" \
"LON=$LON" \ "LON=$LON" \
"CITY=$CITY" \ "CITY=$CITY" \
"TEMPERATURE=$TEMPERATURE" \ "TEMPERATURE=$TEMPERATURE" \
"HUMIDITY=$HUMIDITY" \ "HUMIDITY=$HUMIDITY" \
"UNIT=$UNIT" \
"WEATHER_ICON=$WEATHER_ICON" \ "WEATHER_ICON=$WEATHER_ICON" \
"EXPIRATION=$EXPIRATION" > "$WEATHER_FILE" "EXPIRATION=$EXPIRATION" > "$WEATHER_FILE"
} }
@ -202,15 +234,22 @@ save_info() {
load_info() { load_info() {
# loads env vars # loads env vars
if [[ ! -e "$WEATHER_FILE" ]] ; then if [[ ! -e "$WEATHER_FILE" ]] ; then
# generates blank info # generates blank info on fresh installs
# test for gojq
PARSER=gojq
if ! command -v $PARSER ; then
# test for jq
PARSER=jq
if ! command -v $PARSER ; then
echo "$1 depends on jq or gojq"
exit 1
fi
fi
save_info save_info
fi fi
source $WEATHER_FILE source $WEATHER_FILE
if [[ -z "$API_KEY" ]] ; then
echo "No API Key found!"
exit 1
fi
} }
get_icon() { get_icon() {
@ -290,6 +329,8 @@ for arg in "$@"; do
shift shift
case "$arg" in case "$arg" in
'--zipcode') set -- "$@" "-z" ;; '--zipcode') set -- "$@" "-z" ;;
'--location') set -- "$@" "-l" ;;
'--apikey') set -- "$@" "-a" ;;
'--coords') set -- "$@" "-c" ;; '--coords') set -- "$@" "-c" ;;
'--search') set -- "$@" "-s" ;; '--search') set -- "$@" "-s" ;;
'--units') set -- "$@" "-u" ;; '--units') set -- "$@" "-u" ;;
@ -300,16 +341,27 @@ for arg in "$@"; do
esac esac
done done
while getopts "chpiz:s:u:" opt; do # loading the info
load_info
while getopts "chpliz:s:u:a:" opt; do
case "$opt" in case "$opt" in
'a' )
API_KEY="$OPTARG"
update_key
;;
'u' ) 'u' )
UNIT="$OPTARG" UNIT="$OPTARG"
;; ;;
'l' )
print_location
exit 0
;;
'z' ) 'z' )
[ -n "$ZIPCODE" ] && usage 1 || ZIPCODE="zip=$OPTARG" [ -n "$SEARCH" ] && usage 1 || SEARCH="/zip?zip=$OPTARG"
;; ;;
's' ) 's' )
[ -n "$SEARCH" ] && usage 1 || SEARCH="q=$OPTARG" [ -n "$SEARCH" ] && usage 1 || SEARCH="/direct?q=$OPTARG"
;; ;;
'p' ) 'p' )
PRETTY=true PRETTY=true
@ -327,12 +379,17 @@ while getopts "chpiz:s:u:" opt; do
esac esac
done done
load_info # test for APIKEY
if [[ -z "$API_KEY" ]] ; then
echo "No API Key found!"
exit 1
fi
if [[ -n "$ZIPCODE" || -n "$SEARCH" ]] ; then if [[ -n "$SEARCH" ]] ; then
# perform search # perform search
get_location get_location
fi fi
# print the weather if [[ -z "$QUIET" ]] ; then
print_weather print_weather
fi