generalized gitignore and key
This commit is contained in:
		
							parent
							
								
									61bffcf353
								
							
						
					
					
						commit
						fe670bc62a
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1 +1 @@ | ||||
| .env.key | ||||
| .env* | ||||
|  | ||||
| @ -1,9 +1,9 @@ | ||||
| #!/bin/bash | ||||
| 
 | ||||
| # server version, calls server for actual API info | ||||
| # client version, calls server for actual API info | ||||
| usage() { | ||||
|     cat <<EOF | ||||
| usage: $0 [-c COORD_PAIR][-s QUERY][-z ZIPCODE] | ||||
| usage: $0 [-c COORD_PAIR][-s QUERY][-z ZIPCODE][-u <c|f>] | ||||
| 
 | ||||
| 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 | ||||
|     # only update weather every 30 seconds | ||||
|     if [[ "$UNITS" =~ ^[Cc]$ ]] ; then | ||||
|         # units set to celsius | ||||
|         UNITS="metric" | ||||
|     else | ||||
|         # imperial | ||||
|         UNITS="imperial" | ||||
|     fi | ||||
| 
 | ||||
| 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) | ||||
|     if [[ -z "$LAT" || -z "$LON" ]] ; then | ||||
|         # no lat or lon | ||||
|         get_location | ||||
|     fi | ||||
| 
 | ||||
|     echo $WEATHER | ||||
|     TEMP=$(echo $WEATHER | gojq -r '.main.temp') | ||||
|     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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 KeeganForelight
						KeeganForelight