Tweaked weather script to add better icon support and location caching

nvim
Keegan 2 years ago
parent 83eb0d8537
commit 5bb4143b68

@ -0,0 +1,66 @@
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",
}

@ -0,0 +1,9 @@
#!/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)
export LAT LON CITY

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)
@ -12,13 +13,37 @@ type Weather struct {
Temp float64
Humidity int
Icon rune
City string
State string
}
type weathercode rune
const (
CLEAR_DAY weathercode = '\ue30d'
CLEAR_NIGHT weathercode = '\ue32b'
PARTLY_CLOUDY_DAY weathercode = '\ue302'
PARTLY_CLOUDY_NIGHT weathercode = '\ue379'
CLOUDY weathercode = '\ue312'
RAIN weathercode = '\ue318'
THUNDERSTORM weathercode = '\ue31d'
SNOW weathercode = '\ue31a'
FOG weathercode = '\ue313'
DEGREES_F weathercode = '\ue341'
HUMIDITY weathercode = '\ue373'
)
func (w *Weather) String() string {
return fmt.Sprintf("%c %.1f%c %d%%", w.Icon, w.Temp, '\ue341', w.Humidity) // Output as ICON ##.#*F ##% 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, USStates[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
@ -31,7 +56,7 @@ func main() {
panic(err)
}
// fmting hell
weather := &Weather{}
weather := &Weather{City: city}
for k, v := range temp {
if k == "main" {
// weather info
@ -42,6 +67,8 @@ func main() {
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{}) {
@ -53,31 +80,37 @@ func main() {
}
fmt.Print(weather)
}
func tod(day bool, dayrune weathercode, nightrune weathercode) rune {
if day {
return rune(dayrune)
}
return rune(nightrune)
}
func getIcon(code string) rune {
weather := strings.Trim(code, "nd") // trimming day or night tag
var day bool
if strings.Contains(code, "d") {
day = true
} else {
day = false
}
switch weather {
case "01":
// clear skys
return '\ue30d' // sun code
return tod(day, CLEAR_DAY, CLEAR_NIGHT) // clear skies
case "02", "03":
// few clouds and scattered
return '\ue302' // small clouds with sun
return tod(day, PARTLY_CLOUDY_DAY, PARTLY_CLOUDY_NIGHT) // small clouds
case "04":
// broken clouds
return '\ue312' // big clouds with sun
return rune(CLOUDY) // big clouds with sun
case "09", "10":
// rain
return '\ue318' // cloud with rain
return rune(RAIN)
case "11":
// thunderstorm
return '\ue31d' // thunderstorm cloud
return rune(THUNDERSTORM)
case "13":
// snow
return '\ue31a' // snow cloud
return rune(SNOW)
case "50":
// mist
return '\ue313' // fog
return rune(FOG)
default:
return '\uFFFD' // question mark
}

@ -0,0 +1,11 @@
#!/usr/bin/env bash
# prints all symbols that can be used to test output with font
echo -e '\ue30d Clear (Day)'
echo -e '\ue32b Clear (Night)'
echo -e '\ue302 Partly Cloud (Day)'
echo -e '\ue379 Partly Cloudy (Night)'
echo -e '\ue312 Cloud'
echo -e '\ue318 Rain'
echo -e '\ue31d Thunderstorm'
echo -e '\ue31a Snow'
echo -e '\ue313 Fog'

Binary file not shown.

@ -2,11 +2,6 @@
# if you get my email banned ill freak out
API_KEY='60844f78595f66ff8e94df50aed24397'
LOCATION=$(curl --silent http://ip-api.com/csv)
CITY=$(echo "$LOCATION" | cut -d , -f 6)
LAT=$(echo "$LOCATION" | cut -d , -f 8)
LON=$(echo "$LOCATION" | cut -d , -f 9)
# getting weather
WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial)
echo $WEATHER

@ -24,7 +24,8 @@
path: oh-my-zsh/custom/plugins/zsh-autosuggestions
~/.oh-my-zsh/custom/themes/powerlevel10k.zsh-theme: oh-my-zsh/custom/themes/powerlevel10k/powerlevel10k.zsh-theme
- shell:
shell:
- [$HOME/.dotfiles/bin/weather/symbol_test.sh, Testing weather symbols]
- [cp ./vim/hybrid/colors/hybrid.vim ./vim/colors/hybrid.vim, Setting up theme]
- [tmux/plugins/tpm/bin/install_plugins, Installing Tmux Plugins]
- [tmux/plugins/tpm/bin/update_plugins all, Updating Tmux Plugins]

@ -144,3 +144,5 @@ alias c="cd && clear"
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"

Loading…
Cancel
Save