fixed weather script to only update every 30 seconds and persist location data to last seen
This commit is contained in:
		
							parent
							
								
									1840aea989
								
							
						
					
					
						commit
						2e087b18c0
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -2,4 +2,5 @@ tmux/resurrect
 | 
				
			|||||||
tmux/plugins
 | 
					tmux/plugins
 | 
				
			||||||
!tmux/plugins/tpm
 | 
					!tmux/plugins/tpm
 | 
				
			||||||
bin/water/waterintake
 | 
					bin/water/waterintake
 | 
				
			||||||
 | 
					bin/weather/.env*
 | 
				
			||||||
*.swp
 | 
					*.swp
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										3
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							@ -29,3 +29,6 @@
 | 
				
			|||||||
[submodule "tpm"]
 | 
					[submodule "tpm"]
 | 
				
			||||||
	path = tpm
 | 
						path = tpm
 | 
				
			||||||
	url = https://github.com/tmux-plugins/tpm
 | 
						url = https://github.com/tmux-plugins/tpm
 | 
				
			||||||
 | 
					[submodule ".priv"]
 | 
				
			||||||
 | 
						path = .priv
 | 
				
			||||||
 | 
						url = git@github.com:KeeganForelight/private_info.git
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										1
									
								
								.priv
									
									
									
									
									
										Submodule
									
								
							
							
								
								
								
								
								
								
									
									
								
							
						
						
									
										1
									
								
								.priv
									
									
									
									
									
										Submodule
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					Subproject commit 66fe519826ba9b6870422179c368ea5d3a97e890
 | 
				
			||||||
							
								
								
									
										4
									
								
								bin/weather/.location
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								bin/weather/.location
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					STATE=MA
 | 
				
			||||||
 | 
					CITY=Cambridge
 | 
				
			||||||
 | 
					LAT=42.3737
 | 
				
			||||||
 | 
					LON=-71.1284
 | 
				
			||||||
@ -1,66 +0,0 @@
 | 
				
			|||||||
package main
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
var USStates = map[string]string{
 | 
					 | 
				
			||||||
	"New Jersey":                     "NJ",
 | 
					 | 
				
			||||||
	"Ohio":                           "OH",
 | 
					 | 
				
			||||||
	"Pennsylvania":                   "PA",
 | 
					 | 
				
			||||||
	"Vermont":                        "VT",
 | 
					 | 
				
			||||||
	"Georgia":                        "GA",
 | 
					 | 
				
			||||||
	"Hawaii":                         "HI",
 | 
					 | 
				
			||||||
	"New Hampshire":                  "NH",
 | 
					 | 
				
			||||||
	"Utah":                           "UT",
 | 
					 | 
				
			||||||
	"Oklahoma":                       "OK",
 | 
					 | 
				
			||||||
	"South Carolina":                 "SC",
 | 
					 | 
				
			||||||
	"South Dakota":                   "SD",
 | 
					 | 
				
			||||||
	"Illinois":                       "IL",
 | 
					 | 
				
			||||||
	"New York":                       "NY",
 | 
					 | 
				
			||||||
	"Armed Forces Europe":            "AE",
 | 
					 | 
				
			||||||
	"Armed Forces Pacific":           "AP",
 | 
					 | 
				
			||||||
	"Connecticut":                    "CT",
 | 
					 | 
				
			||||||
	"Kansas":                         "KS",
 | 
					 | 
				
			||||||
	"Wisconsin":                      "WI",
 | 
					 | 
				
			||||||
	"Tennessee":                      "TN",
 | 
					 | 
				
			||||||
	"Palau":                          "PW",
 | 
					 | 
				
			||||||
	"California":                     "CA",
 | 
					 | 
				
			||||||
	"Louisiana":                      "LA",
 | 
					 | 
				
			||||||
	"Michigan":                       "MI",
 | 
					 | 
				
			||||||
	"Rhode Island":                   "RI",
 | 
					 | 
				
			||||||
	"Armed Forces Americas":          "AA",
 | 
					 | 
				
			||||||
	"Alabama":                        "AL",
 | 
					 | 
				
			||||||
	"Arizona":                        "AZ",
 | 
					 | 
				
			||||||
	"North Dakota":                   "ND",
 | 
					 | 
				
			||||||
	"Colorado":                       "CO",
 | 
					 | 
				
			||||||
	"Guam":                           "GU",
 | 
					 | 
				
			||||||
	"Virgin Islands":                 "VI",
 | 
					 | 
				
			||||||
	"Alaska":                         "AK",
 | 
					 | 
				
			||||||
	"Indiana":                        "IN",
 | 
					 | 
				
			||||||
	"Iowa":                           "IA",
 | 
					 | 
				
			||||||
	"District of Columbia":           "DC",
 | 
					 | 
				
			||||||
	"Marshall Islands":               "MH",
 | 
					 | 
				
			||||||
	"Puerto Rico":                    "PR",
 | 
					 | 
				
			||||||
	"Montana":                        "MT",
 | 
					 | 
				
			||||||
	"New Mexico":                     "NM",
 | 
					 | 
				
			||||||
	"North Carolina":                 "NC",
 | 
					 | 
				
			||||||
	"Arkansas":                       "AR",
 | 
					 | 
				
			||||||
	"Maryland":                       "MD",
 | 
					 | 
				
			||||||
	"Massachusetts":                  "MA",
 | 
					 | 
				
			||||||
	"Delaware":                       "DE",
 | 
					 | 
				
			||||||
	"Maine":                          "ME",
 | 
					 | 
				
			||||||
	"Northern Mariana Islands":       "MP",
 | 
					 | 
				
			||||||
	"West Virginia":                  "WV",
 | 
					 | 
				
			||||||
	"Idaho":                          "ID",
 | 
					 | 
				
			||||||
	"Mississippi":                    "MS",
 | 
					 | 
				
			||||||
	"Washington":                     "WA",
 | 
					 | 
				
			||||||
	"Nevada":                         "NV",
 | 
					 | 
				
			||||||
	"Oregon":                         "OR",
 | 
					 | 
				
			||||||
	"Virginia":                       "VA",
 | 
					 | 
				
			||||||
	"Wyoming":                        "WY",
 | 
					 | 
				
			||||||
	"Federated States of Micronesia": "FM",
 | 
					 | 
				
			||||||
	"Kentucky":                       "KY",
 | 
					 | 
				
			||||||
	"Minnesota":                      "MN",
 | 
					 | 
				
			||||||
	"Nebraska":                       "NE",
 | 
					 | 
				
			||||||
	"American Samoa":                 "AS",
 | 
					 | 
				
			||||||
	"Florida":                        "FL",
 | 
					 | 
				
			||||||
	"Missouri":                       "MO",
 | 
					 | 
				
			||||||
	"Texas":                          "TX",
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -1,3 +1,5 @@
 | 
				
			|||||||
module weather
 | 
					module weather
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.18
 | 
					go 1.18
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require github.com/joho/godotenv v1.4.0 // indirect
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								bin/weather/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								bin/weather/go.sum
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
				
			|||||||
 | 
					github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
 | 
				
			||||||
 | 
					github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 | 
				
			||||||
@ -1,9 +1,15 @@
 | 
				
			|||||||
#!/usr/bin/env bash
 | 
					#!/usr/bin/env bash
 | 
				
			||||||
# sets enviroment variables on login
 | 
					# creates a hidden file of your location details based on your address, will expand to support addition via zip codes
 | 
				
			||||||
CURRENT_LOCATION=$(curl --silent http://ip-api.com/csv)
 | 
					
 | 
				
			||||||
CITY=$(echo "$CURRENT_LOCATION" | cut -d , -f 6)
 | 
					if [[ -z $1 ]] ; then
 | 
				
			||||||
LAT=$(echo "$CURRENT_LOCATION" | cut -d , -f 8)
 | 
					    # called without args
 | 
				
			||||||
LON=$(echo "$CURRENT_LOCATION" | cut -d , -f 9)
 | 
					    CURRENT_LOCATION=$(curl --silent http://ip-api.com/csv)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    STATE_CODE=$(echo "$CURRENT_LOCATION" | cut -d, -f 4)
 | 
				
			||||||
 | 
					    CITY=$(echo "$CURRENT_LOCATION" | cut -d , -f 6)
 | 
				
			||||||
 | 
					    LAT=$(echo "$CURRENT_LOCATION" | cut -d , -f 8)
 | 
				
			||||||
 | 
					    LON=$(echo "$CURRENT_LOCATION" | cut -d , -f 9)
 | 
				
			||||||
 | 
					    printf "STATE=%s\nCITY=%s\nLAT=%s\nLON=%s\n" $STATE_CODE $CITY $LAT $LON > "$HOME/.dotfiles/bin/weather/.env.location"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export LAT LON CITY
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,12 +1,13 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
					 | 
				
			||||||
	"encoding/json"
 | 
					 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/joho/godotenv"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Weather struct {
 | 
					type Weather struct {
 | 
				
			||||||
@ -15,6 +16,7 @@ type Weather struct {
 | 
				
			|||||||
	Icon       rune
 | 
						Icon       rune
 | 
				
			||||||
	City       string
 | 
						City       string
 | 
				
			||||||
	State      string
 | 
						State      string
 | 
				
			||||||
 | 
						LastPinged int64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type weathercode rune
 | 
					type weathercode rune
 | 
				
			||||||
@ -34,52 +36,47 @@ const (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (w *Weather) String() string {
 | 
					func (w *Weather) String() string {
 | 
				
			||||||
	return fmt.Sprintf(" %c %.1f%c %d%c in %s, %s", w.Icon, w.Temp, DEGREES_F, w.Humidity, HUMIDITY, w.City, USStates[w.State]) // Output as ICON ##.#*F ##% in City, ST where the rune is a degree sign
 | 
						return fmt.Sprintf(" %c %.1f%c %d%c in %s, %s", w.Icon, w.Temp, DEGREES_F, w.Humidity, HUMIDITY, w.City, w.State) // Output as ICON ##.#*F ##% in City, ST where the rune is a degree sign
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 | 
						// getting weather
 | 
				
			||||||
 | 
						if err := exec.Command(os.ExpandEnv("$HOME/.dotfiles/bin/weather/weather.sh")).Run(); err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// get up to date info
 | 
				
			||||||
 | 
						loc := os.ExpandEnv(fmt.Sprintf("$HOME/.dotfiles/bin/weather/.env.location"))
 | 
				
			||||||
 | 
						weath := os.ExpandEnv(fmt.Sprintf("$HOME/.dotfiles/bin/weather/.env.weather"))
 | 
				
			||||||
 | 
						if err := godotenv.Load(weath, loc); err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var humidity int
 | 
				
			||||||
 | 
						var temp float64
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	city := os.Getenv("CITY")
 | 
						city := os.Getenv("CITY")
 | 
				
			||||||
	if city == "" {
 | 
						state := os.Getenv("STATE") // fine as strings
 | 
				
			||||||
		fmt.Print("No location found!")
 | 
						icon := getIcon(os.Getenv("ICON"))
 | 
				
			||||||
		os.Exit(1)
 | 
						h := os.Getenv("HUMIDITY")
 | 
				
			||||||
	}
 | 
						t := os.Getenv("TEMP")
 | 
				
			||||||
	city = strings.Trim(city, " \n")
 | 
						if h != "" {
 | 
				
			||||||
	weathercmd := exec.Command("sh", "-c", "${HOME}/.dotfiles/bin/weather/weather.sh")
 | 
							hum, err := strconv.ParseInt(h, 10, 64)
 | 
				
			||||||
	var out bytes.Buffer
 | 
							if err != nil {
 | 
				
			||||||
	weathercmd.Stdout = &out
 | 
					 | 
				
			||||||
	if err := weathercmd.Run(); err != nil {
 | 
					 | 
				
			||||||
			panic(err)
 | 
								panic(err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	// parsing json time :D
 | 
							humidity = int(hum)
 | 
				
			||||||
	var temp map[string]interface{}
 | 
						}
 | 
				
			||||||
	if err := json.Unmarshal(out.Bytes(), &temp); err != nil {
 | 
						if t != "" {
 | 
				
			||||||
 | 
							temp, err = strconv.ParseFloat(t, 64)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
			panic(err)
 | 
								panic(err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	// fmting hell
 | 
					 | 
				
			||||||
	weather := &Weather{City: city}
 | 
					 | 
				
			||||||
	for k, v := range temp {
 | 
					 | 
				
			||||||
		if k == "main" {
 | 
					 | 
				
			||||||
			// weather info
 | 
					 | 
				
			||||||
			for nk, nv := range v.(map[string]interface{}) {
 | 
					 | 
				
			||||||
				if nk == "temp" {
 | 
					 | 
				
			||||||
					weather.Temp = nv.(float64)
 | 
					 | 
				
			||||||
				} else if nk == "humidity" {
 | 
					 | 
				
			||||||
					weather.Humidity = int(nv.(float64)) // this is as gross as it gets
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		} else if k == "name" {
 | 
					 | 
				
			||||||
			weather.State = v.(string)
 | 
					 | 
				
			||||||
		} else if k == "weather" {
 | 
					 | 
				
			||||||
			m := v.([]interface{})
 | 
					 | 
				
			||||||
			for nk, nv := range m[0].(map[string]interface{}) {
 | 
					 | 
				
			||||||
				if nk == "icon" {
 | 
					 | 
				
			||||||
					weather.Icon = getIcon(nv.(string))
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						weather := &Weather{City: city, State: state, Icon: icon, Humidity: humidity, Temp: temp}
 | 
				
			||||||
	fmt.Print(weather)
 | 
						fmt.Print(weather)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func tod(day bool, dayrune weathercode, nightrune weathercode) rune {
 | 
					func tod(day bool, dayrune weathercode, nightrune weathercode) rune {
 | 
				
			||||||
	if day {
 | 
						if day {
 | 
				
			||||||
		return rune(dayrune)
 | 
							return rune(dayrune)
 | 
				
			||||||
 | 
				
			|||||||
										
											Binary file not shown.
										
									
								
							@ -1,7 +1,19 @@
 | 
				
			|||||||
#!/bin/bash
 | 
					#!/bin/bash
 | 
				
			||||||
 | 
					source "$HOME/.dotfiles/.priv/key"
 | 
				
			||||||
# if you get my email banned ill freak out
 | 
					source "$HOME/.dotfiles/bin/weather/.env.location"
 | 
				
			||||||
API_KEY='60844f78595f66ff8e94df50aed24397'
 | 
					source "$HOME/.dotfiles/bin/weather/.env.weather"
 | 
				
			||||||
 | 
					# API_KEY=$(cat "$HOME/.dotfiles/bin/weather/.priv/key")
 | 
				
			||||||
# getting weather
 | 
					# getting weather
 | 
				
			||||||
WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial)
 | 
					CUR_TIMESTAMP=$(date +%s)
 | 
				
			||||||
echo $WEATHER
 | 
					# 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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TEMP=$(echo $WEATHER | jq -r '.main.temp')
 | 
				
			||||||
 | 
					    HUMIDITY=$(echo $WEATHER | jq -r '.main.humidity')
 | 
				
			||||||
 | 
					    ICON=$(echo $WEATHER | jq -r '.weather[0].icon')
 | 
				
			||||||
 | 
					    EXPIRATION=$(($CUR_TIMESTAMP+30))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf 'TEMP=%s\nHUMIDITY=%s\nICON=%s\nEXPIRATION=%s\n' "$TEMP" "$HUMIDITY" "$ICON" "$EXPIRATION" >"$HOME/.dotfiles/bin/weather/.env.weather"
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								zshrc
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								zshrc
									
									
									
									
									
								
							@ -145,4 +145,4 @@ alias nalgene="$HOME/.dotfiles/bin/water/water.sh 32"
 | 
				
			|||||||
alias water="$HOME/.dotfiles/bin/water/water.sh"
 | 
					alias water="$HOME/.dotfiles/bin/water/water.sh"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# setting up location variables
 | 
					# setting up location variables
 | 
				
			||||||
source "$HOME/.dotfiles/bin/weather/location.sh"
 | 
					# source "$HOME/.dotfiles/bin/weather/location.sh"
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user