diff --git a/.gitignore b/.gitignore index b03ebb5..4ee31d8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ tmux/resurrect tmux/plugins !tmux/plugins/tpm bin/water/waterintake +bin/weather/.env* *.swp diff --git a/.gitmodules b/.gitmodules index a03fd18..c93c8d5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -29,3 +29,6 @@ [submodule "tpm"] path = tpm url = https://github.com/tmux-plugins/tpm +[submodule ".priv"] + path = .priv + url = git@github.com:KeeganForelight/private_info.git diff --git a/.priv b/.priv new file mode 160000 index 0000000..66fe519 --- /dev/null +++ b/.priv @@ -0,0 +1 @@ +Subproject commit 66fe519826ba9b6870422179c368ea5d3a97e890 diff --git a/bin/weather/.location b/bin/weather/.location new file mode 100644 index 0000000..3ee6deb --- /dev/null +++ b/bin/weather/.location @@ -0,0 +1,4 @@ +STATE=MA +CITY=Cambridge +LAT=42.3737 +LON=-71.1284 diff --git a/bin/weather/definitions.go b/bin/weather/definitions.go deleted file mode 100644 index 62d89db..0000000 --- a/bin/weather/definitions.go +++ /dev/null @@ -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", -} diff --git a/bin/weather/go.mod b/bin/weather/go.mod index d8d9456..9d28ef2 100644 --- a/bin/weather/go.mod +++ b/bin/weather/go.mod @@ -1,3 +1,5 @@ module weather go 1.18 + +require github.com/joho/godotenv v1.4.0 // indirect diff --git a/bin/weather/go.sum b/bin/weather/go.sum new file mode 100644 index 0000000..8c9f290 --- /dev/null +++ b/bin/weather/go.sum @@ -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= diff --git a/bin/weather/location.sh b/bin/weather/location.sh index 61d5798..6ec0c52 100755 --- a/bin/weather/location.sh +++ b/bin/weather/location.sh @@ -1,9 +1,15 @@ #!/usr/bin/env bash -# sets enviroment variables on login -CURRENT_LOCATION=$(curl --silent http://ip-api.com/csv) -CITY=$(echo "$CURRENT_LOCATION" | cut -d , -f 6) -LAT=$(echo "$CURRENT_LOCATION" | cut -d , -f 8) -LON=$(echo "$CURRENT_LOCATION" | cut -d , -f 9) +# creates a hidden file of your location details based on your address, will expand to support addition via zip codes + +if [[ -z $1 ]] ; then + # called without args + 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 diff --git a/bin/weather/main.go b/bin/weather/main.go index a631d3e..232fcb8 100644 --- a/bin/weather/main.go +++ b/bin/weather/main.go @@ -1,20 +1,22 @@ package main import ( - "bytes" - "encoding/json" "fmt" "os" "os/exec" + "strconv" "strings" + + "github.com/joho/godotenv" ) type Weather struct { - Temp float64 - Humidity int - Icon rune - City string - State string + Temp float64 + Humidity int + Icon rune + City string + State string + LastPinged int64 } type weathercode rune @@ -34,52 +36,47 @@ const ( ) 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() { - city := os.Getenv("CITY") - if city == "" { - fmt.Print("No location found!") - os.Exit(1) - } - city = strings.Trim(city, " \n") - weathercmd := exec.Command("sh", "-c", "${HOME}/.dotfiles/bin/weather/weather.sh") - var out bytes.Buffer - weathercmd.Stdout = &out - if err := weathercmd.Run(); err != nil { + // getting weather + if err := exec.Command(os.ExpandEnv("$HOME/.dotfiles/bin/weather/weather.sh")).Run(); err != nil { panic(err) } - // parsing json time :D - var temp map[string]interface{} - if err := json.Unmarshal(out.Bytes(), &temp); err != nil { + // 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) } - // 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)) - } - } + + var humidity int + var temp float64 + var err error + + city := os.Getenv("CITY") + state := os.Getenv("STATE") // fine as strings + icon := getIcon(os.Getenv("ICON")) + h := os.Getenv("HUMIDITY") + t := os.Getenv("TEMP") + if h != "" { + hum, err := strconv.ParseInt(h, 10, 64) + if err != nil { + panic(err) + } + humidity = int(hum) + } + if t != "" { + temp, err = strconv.ParseFloat(t, 64) + if err != nil { + panic(err) } } + weather := &Weather{City: city, State: state, Icon: icon, Humidity: humidity, Temp: temp} fmt.Print(weather) } + func tod(day bool, dayrune weathercode, nightrune weathercode) rune { if day { return rune(dayrune) diff --git a/bin/weather/weather b/bin/weather/weather index 0fa43bd..d00f436 100755 Binary files a/bin/weather/weather and b/bin/weather/weather differ diff --git a/bin/weather/weather.sh b/bin/weather/weather.sh index 38852a8..c7ee34c 100755 --- a/bin/weather/weather.sh +++ b/bin/weather/weather.sh @@ -1,7 +1,19 @@ #!/bin/bash - -# if you get my email banned ill freak out -API_KEY='60844f78595f66ff8e94df50aed24397' +source "$HOME/.dotfiles/.priv/key" +source "$HOME/.dotfiles/bin/weather/.env.location" +source "$HOME/.dotfiles/bin/weather/.env.weather" +# API_KEY=$(cat "$HOME/.dotfiles/bin/weather/.priv/key") # getting weather -WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial) -echo $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) + + 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 diff --git a/zshrc b/zshrc index 07908c8..5172353 100644 --- a/zshrc +++ b/zshrc @@ -145,4 +145,4 @@ alias nalgene="$HOME/.dotfiles/bin/water/water.sh 32" alias water="$HOME/.dotfiles/bin/water/water.sh" # setting up location variables -source "$HOME/.dotfiles/bin/weather/location.sh" +# source "$HOME/.dotfiles/bin/weather/location.sh"