fixed weather script to only update every 30 seconds and persist location data to last seen

nvim
Keegan 2 years ago
parent 1840aea989
commit 2e087b18c0

1
.gitignore vendored

@ -2,4 +2,5 @@ tmux/resurrect
tmux/plugins
!tmux/plugins/tpm
bin/water/waterintake
bin/weather/.env*
*.swp

3
.gitmodules vendored

@ -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

@ -0,0 +1 @@
Subproject commit 66fe519826ba9b6870422179c368ea5d3a97e890

@ -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
go 1.18
require github.com/joho/godotenv v1.4.0 // indirect

@ -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
# 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
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

@ -1,12 +1,13 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"github.com/joho/godotenv"
)
type Weather struct {
@ -15,6 +16,7 @@ type Weather struct {
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)

Binary file not shown.

@ -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
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)
echo $WEATHER
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

@ -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"

Loading…
Cancel
Save