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 | #!/bin/bash | ||||||
| 
 | 
 | ||||||
| # server version, calls server for actual API info | # client version, calls server for actual API info | ||||||
| usage() { | usage() { | ||||||
|     cat <<EOF |     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 | Queries OpenWeatherMapAPI for weather or lat/long | ||||||
| 
 | 
 | ||||||
| @ -11,6 +11,7 @@ options: | |||||||
|   -c, --coords      searches for weather based on COORD_PAIR |   -c, --coords      searches for weather based on COORD_PAIR | ||||||
|   -z, --zipcode     returns the coordinate pair(s) for given zipcode |   -z, --zipcode     returns the coordinate pair(s) for given zipcode | ||||||
|   -s, --search      returns the coordinate pair(s) for QUERY |   -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" | COORD_PAIR is a string of the format "LAT,LON" | ||||||
| QUERY is a city, state or city to search | QUERY is a city, state or city to search | ||||||
| @ -18,33 +19,88 @@ EOF | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| WORKING_DIR="$HOME/.local/share/weather" | 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() { | get_location() { | ||||||
|     # no search, use IP |     # no search, use IP | ||||||
|     url="http://ip-api.com/csv/?fields=252" |     if [[ -n "$SEARCH" ]] ; then | ||||||
|     # search provided, feed to script |         # 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() { | get_weather() { | ||||||
|     # calls server for weather based on $LAT, $LONG |     # 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 |     # only update weather every 30 seconds | ||||||
|     WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial) |     if [[ "$UNITS" =~ ^[Cc]$ ]] ; then | ||||||
|  |         # units set to celsius | ||||||
|  |         UNITS="metric" | ||||||
|  |     else | ||||||
|  |         # imperial | ||||||
|  |         UNITS="imperial" | ||||||
|  |     fi | ||||||
| 
 | 
 | ||||||
|     echo $WEATHER |     if [[ -z "$LAT" || -z "$LON" ]] ; then | ||||||
|     TEMP=$(echo $WEATHER | gojq -r '.main.temp') |         # 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') |     HUMIDITY=$(echo $WEATHER | gojq -r '.main.humidity') | ||||||
|     ICON=$(echo $WEATHER | gojq -r '.weather[0].icon') |     WEATHER_ICON=$(echo $WEATHER | gojq -r '.weather[0].icon') | ||||||
|     EXPIRATION=$(($CUR_TIMESTAMP+30)) |     EXPIRATION=$(($(date +%s)+$TIMEOUT)) | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|     printf 'TEMP=%s\nHUMIDITY=%s\nICON=%s\nEXPIRATION=%s\n' "$TEMP" "$HUMIDITY" "$ICON" "$EXPIRATION" >"$HOME/.dotfiles/bin/weather/.env.weather" | save_info() { | ||||||
| fi |     # 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 | # shifting longform | ||||||
| for arg in "$@"; do | for arg in "$@"; do | ||||||
| @ -53,12 +109,33 @@ for arg in "$@"; do | |||||||
|         '--zipcode')    set -- "$@" "-z"    ;; |         '--zipcode')    set -- "$@" "-z"    ;; | ||||||
|         '--coords')     set -- "$@" "-c"    ;; |         '--coords')     set -- "$@" "-c"    ;; | ||||||
|         '--search')     set -- "$@" "-s"    ;; |         '--search')     set -- "$@" "-s"    ;; | ||||||
|  |         '--units')      set -- "$@" "-u"    ;; | ||||||
|         *)              set -- "$@" "$arg"  ;; |         *)              set -- "$@" "$arg"  ;; | ||||||
|     esac |     esac | ||||||
| done | done | ||||||
| 
 | 
 | ||||||
| while getopts "o:a:" arg; do | while getopts "z:c:s:u:h" opt; do | ||||||
|     case ${arg} in |     case "$opt" in | ||||||
|  |         'u' ) | ||||||
|  |             UNITS="$OPTARG" | ||||||
|  |             ;; | ||||||
|  |         'z' ) | ||||||
|  |             ZIPCODE="$OPTARG" | ||||||
|  |             ;; | ||||||
|  |         's' ) | ||||||
|  |             SEARCH="$OPTARG" | ||||||
|  |             ;; | ||||||
|  |         'h' ) | ||||||
|  |             usage | ||||||
|  |             exit 0 | ||||||
|  |             ;; | ||||||
|     esac |     esac | ||||||
| done | 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 | # server version, calls server for actual API info | ||||||
| WORKING_DIR="$HOME/.local/share/weather" | WORKING_DIR="$HOME/.local/share/weather" | ||||||
|  | KEY_FILE="$WORKING_DIR/.env" | ||||||
| 
 | 
 | ||||||
| # sets $API_KEY | # set $API_KEY | ||||||
| source "$WORKING_DIR/.env.key" | 
 | ||||||
|  | if [[ ! -e "$KEY_FILE" ]] ; then | ||||||
|  |     echo "No API key found!" | ||||||
|  |     exit 1 | ||||||
|  | fi | ||||||
|  | 
 | ||||||
|  | source "$KEY_FILE" | ||||||
| 
 | 
 | ||||||
| lookup_location() { | lookup_location() { | ||||||
|     # search for a location |     # search for a location | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 KeeganForelight
						KeeganForelight