Tweaked weather script to add better icon support and location caching
This commit is contained in:
		
							parent
							
								
									83eb0d8537
								
							
						
					
					
						commit
						5bb4143b68
					
				
							
								
								
									
										66
									
								
								bin/weather/definitions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								bin/weather/definitions.go
									
									
									
									
									
										Normal file
									
								
							@ -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",
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										9
									
								
								bin/weather/location.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										9
									
								
								bin/weather/location.sh
									
									
									
									
									
										Executable file
									
								
							@ -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"
 | 
						"bytes"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -12,13 +13,37 @@ type Weather struct {
 | 
				
			|||||||
	Temp     float64
 | 
						Temp     float64
 | 
				
			||||||
	Humidity int
 | 
						Humidity int
 | 
				
			||||||
	Icon     rune
 | 
						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 {
 | 
					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() {
 | 
					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")
 | 
						weathercmd := exec.Command("sh", "-c", "${HOME}/.dotfiles/bin/weather/weather.sh")
 | 
				
			||||||
	var out bytes.Buffer
 | 
						var out bytes.Buffer
 | 
				
			||||||
	weathercmd.Stdout = &out
 | 
						weathercmd.Stdout = &out
 | 
				
			||||||
@ -31,7 +56,7 @@ func main() {
 | 
				
			|||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// fmting hell
 | 
						// fmting hell
 | 
				
			||||||
	weather := &Weather{}
 | 
						weather := &Weather{City: city}
 | 
				
			||||||
	for k, v := range temp {
 | 
						for k, v := range temp {
 | 
				
			||||||
		if k == "main" {
 | 
							if k == "main" {
 | 
				
			||||||
			// weather info
 | 
								// weather info
 | 
				
			||||||
@ -42,6 +67,8 @@ func main() {
 | 
				
			|||||||
					weather.Humidity = int(nv.(float64)) // this is as gross as it gets
 | 
										weather.Humidity = int(nv.(float64)) // this is as gross as it gets
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							} else if k == "name" {
 | 
				
			||||||
 | 
								weather.State = v.(string)
 | 
				
			||||||
		} else if k == "weather" {
 | 
							} else if k == "weather" {
 | 
				
			||||||
			m := v.([]interface{})
 | 
								m := v.([]interface{})
 | 
				
			||||||
			for nk, nv := range m[0].(map[string]interface{}) {
 | 
								for nk, nv := range m[0].(map[string]interface{}) {
 | 
				
			||||||
@ -53,31 +80,37 @@ func main() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	fmt.Print(weather)
 | 
						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 {
 | 
					func getIcon(code string) rune {
 | 
				
			||||||
	weather := strings.Trim(code, "nd") // trimming day or night tag
 | 
						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 {
 | 
						switch weather {
 | 
				
			||||||
	case "01":
 | 
						case "01":
 | 
				
			||||||
		// clear skys
 | 
							return tod(day, CLEAR_DAY, CLEAR_NIGHT) // clear skies
 | 
				
			||||||
		return '\ue30d' // sun code
 | 
					 | 
				
			||||||
	case "02", "03":
 | 
						case "02", "03":
 | 
				
			||||||
		// few clouds and scattered
 | 
							return tod(day, PARTLY_CLOUDY_DAY, PARTLY_CLOUDY_NIGHT) // small clouds
 | 
				
			||||||
		return '\ue302' // small clouds with sun
 | 
					 | 
				
			||||||
	case "04":
 | 
						case "04":
 | 
				
			||||||
		// broken clouds
 | 
							return rune(CLOUDY) // big clouds with sun
 | 
				
			||||||
		return '\ue312' // big clouds with sun
 | 
					 | 
				
			||||||
	case "09", "10":
 | 
						case "09", "10":
 | 
				
			||||||
		// rain
 | 
							return rune(RAIN)
 | 
				
			||||||
		return '\ue318' // cloud with rain
 | 
					 | 
				
			||||||
	case "11":
 | 
						case "11":
 | 
				
			||||||
		// thunderstorm
 | 
							return rune(THUNDERSTORM)
 | 
				
			||||||
		return '\ue31d' // thunderstorm cloud
 | 
					 | 
				
			||||||
	case "13":
 | 
						case "13":
 | 
				
			||||||
		// snow
 | 
							return rune(SNOW)
 | 
				
			||||||
		return '\ue31a' // snow cloud
 | 
					 | 
				
			||||||
	case "50":
 | 
						case "50":
 | 
				
			||||||
		// mist
 | 
							return rune(FOG)
 | 
				
			||||||
		return '\ue313' // fog
 | 
					 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		return '\uFFFD' // question mark
 | 
							return '\uFFFD' // question mark
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										11
									
								
								bin/weather/symbol_test.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										11
									
								
								bin/weather/symbol_test.sh
									
									
									
									
									
										Executable file
									
								
							@ -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
 | 
					# if you get my email banned ill freak out
 | 
				
			||||||
API_KEY='60844f78595f66ff8e94df50aed24397'
 | 
					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
 | 
					# getting weather
 | 
				
			||||||
WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial)
 | 
					WEATHER=$(curl --silent http://api.openweathermap.org/data/2.5/weather\?lat="$LAT"\&lon="$LON"\&appid="$API_KEY"\&units=imperial)
 | 
				
			||||||
echo $WEATHER
 | 
					echo $WEATHER
 | 
				
			||||||
 | 
				
			|||||||
@ -24,7 +24,8 @@
 | 
				
			|||||||
        path: oh-my-zsh/custom/plugins/zsh-autosuggestions
 | 
					        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
 | 
					    ~/.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]
 | 
					    - [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/install_plugins, Installing Tmux Plugins]
 | 
				
			||||||
    - [tmux/plugins/tpm/bin/update_plugins all, Updating Tmux Plugins]
 | 
					    - [tmux/plugins/tpm/bin/update_plugins all, Updating Tmux Plugins]
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								zshrc
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								zshrc
									
									
									
									
									
								
							@ -144,3 +144,5 @@ alias c="cd && clear"
 | 
				
			|||||||
alias nalgene="$HOME/.dotfiles/bin/water/water.sh 32"
 | 
					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
 | 
				
			||||||
 | 
					source "$HOME/.dotfiles/bin/weather/location.sh"
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user