From fe670bc62a3fcd28288695f076530c874b07074e Mon Sep 17 00:00:00 2001 From: KeeganForelight Date: Fri, 13 Jan 2023 17:12:07 -0500 Subject: [PATCH] generalized gitignore and key --- .gitignore | 2 +- client_weather.sh | 115 ++++++++++++++++++++++++++++++++++++++-------- server_weather.sh | 11 ++++- 3 files changed, 106 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 9c20535..81a38be 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -.env.key +.env* diff --git a/client_weather.sh b/client_weather.sh index b11f510..e025565 100755 --- a/client_weather.sh +++ b/client_weather.sh @@ -1,9 +1,9 @@ #!/bin/bash -# server version, calls server for actual API info +# client version, calls server for actual API info usage() { cat <] Queries OpenWeatherMapAPI for weather or lat/long @@ -11,6 +11,7 @@ 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 + -u, --units sets the temperature units COORD_PAIR is a string of the format "LAT,LON" QUERY is a city, state or city to search @@ -18,33 +19,88 @@ EOF } WORKING_DIR="$HOME/.local/share/weather" +WEATHER_FILE="$WORKING_DIR/.env" +WEATHER_CALL="ssh spinach@git.keegandeppe.com weather" +TIMEOUT=60 # timeout for info + +print_weather() { + # prints the weather + + # check to see if it is expired + check_expiration + + print "$TEMPERATURE $HUMIDITY in $CITY, $STATE\n" + + # save before exiting + save_info +} + +check_expiration() { + # checks if the info is expired + get_weather +} get_location() { # no search, use IP - url="http://ip-api.com/csv/?fields=252" - # search provided, feed to script + if [[ -n "$SEARCH" ]] ; then + # search + echo "$SEARCH" + elif [[ -n "$ZIPCODE" ]] ; then + # zipcode + echo "$ZIPCODE" + else + # default to IP + url="http://ip-api.com/csv/?fields=252" + res=$(curl --silent -fL "$url") + LAT=$(awk -F , '{print $5}' <<<"$res") + LON=$(awk -F , '{print $6}' <<<"$res") + CITY=$(awk -F , '{print $3}' <<<"$res") + STATE=$(awk -F , '{print $1}' <<<"$res") + fi } 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) + # only update weather every 30 seconds + if [[ "$UNITS" =~ ^[Cc]$ ]] ; then + # units set to celsius + UNITS="metric" + else + # imperial + UNITS="imperial" + fi - echo $WEATHER - TEMP=$(echo $WEATHER | gojq -r '.main.temp') + if [[ -z "$LAT" || -z "$LON" ]] ; then + # no lat or lon + get_location + fi + + WEATHER=$($WEATHER_CALL "-c $LAT,$LON -u $UNITS") + echo "$WEATHER" | gojq '.' + TEMPERATURE=$(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)) + WEATHER_ICON=$(echo $WEATHER | gojq -r '.weather[0].icon') + EXPIRATION=$(($(date +%s)+$TIMEOUT)) +} - printf 'TEMP=%s\nHUMIDITY=%s\nICON=%s\nEXPIRATION=%s\n' "$TEMP" "$HUMIDITY" "$ICON" "$EXPIRATION" >"$HOME/.dotfiles/bin/weather/.env.weather" -fi +save_info() { + # saves set env vars to files + printf '%s\n' \ +"LAT=$LAT" \ +"LON=$LON" \ +"CITY=$CITY" \ +"STATE=$STATE" \ +"TEMPERATURE=$TEMPERATURE" \ +"TEMPERATURE_ICON=$TEMPERATURE_ICON" \ +"HUMIDITY=$HUMIDITY" \ +"WEATHER_ICON=$WEATHER_ICON" \ +"EXPIRATION=$EXPIRATION" +} + +load_info() { + if [[ -e "$ # shifting longform for arg in "$@"; do @@ -53,12 +109,33 @@ for arg in "$@"; do '--zipcode') set -- "$@" "-z" ;; '--coords') set -- "$@" "-c" ;; '--search') set -- "$@" "-s" ;; + '--units') set -- "$@" "-u" ;; *) set -- "$@" "$arg" ;; esac done -while getopts "o:a:" arg; do - case ${arg} in +while getopts "z:c:s:u:h" opt; do + case "$opt" in + 'u' ) + UNITS="$OPTARG" + ;; + 'z' ) + ZIPCODE="$OPTARG" + ;; + 's' ) + SEARCH="$OPTARG" + ;; + 'h' ) + usage + exit 0 + ;; esac done +if [[ -n "$SEARCH" || -n "$ZIPCODE" ]] ; then + # perform search + get_location +fi + +# print the weather +print_weather diff --git a/server_weather.sh b/server_weather.sh index e1e59d5..5060bdc 100755 --- a/server_weather.sh +++ b/server_weather.sh @@ -2,9 +2,16 @@ # server version, calls server for actual API info WORKING_DIR="$HOME/.local/share/weather" +KEY_FILE="$WORKING_DIR/.env" -# sets $API_KEY -source "$WORKING_DIR/.env.key" +# 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