changing weather script to support wttr.in

nvim
Keegan 2 years ago
parent 2e087b18c0
commit 60972cc069

@ -1,15 +1,35 @@
#!/usr/bin/env bash
# creates a hidden file of your location details based on your address, will expand to support addition via zip codes
if [[ -z $1 ]] ; then
usage() { echo "Usage: $0 [-z zipcode]" 1>&2; exit 1; }
if [[ $# -eq 0 ]] ; 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"
printf "CITY=%s\nLAT=%s\nLON=%s\n" "$CITY" $LAT $LON > "$HOME/.dotfiles/bin/weather/.env.location"
fi
while getopts "z:" arg; do
case ${arg} in
z)
ZIPCODE=${OPTARG}
source "$HOME/.dotfiles/.priv/key"
URL="http://api.openweathermap.org/geo/1.0/zip?zip=$ZIPCODE&appid=$API_KEY"
LOCATION=$(curl --silent "$URL")
CITY=$(echo $LOCATION | jq -r '.name')
LAT=$(echo $LOCATION | jq -r '.lat')
LON=$(echo $LOCATION | jq -r '.lon')
printf "CITY=%s\nLAT=%s\nLON=%s\n" "$CITY" $LAT $LON > "$HOME/.dotfiles/bin/weather/.env.location"
# emptying weather buffer
>"$HOME/.dotfiles/bin/weather/.env.weather"
;;
?)
usage
;;
esac
done

@ -15,7 +15,6 @@ type Weather struct {
Humidity int
Icon rune
City string
State string
LastPinged int64
}
@ -36,7 +35,7 @@ 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, 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", w.Icon, w.Temp, DEGREES_F, w.Humidity, HUMIDITY, w.City) // Output as ICON ##.#*F ##% in City, ST where the rune is a degree sign
}
func main() {
@ -56,7 +55,6 @@ func main() {
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")
@ -73,7 +71,7 @@ func main() {
panic(err)
}
}
weather := &Weather{City: city, State: state, Icon: icon, Humidity: humidity, Temp: temp}
weather := &Weather{City: city, Icon: icon, Humidity: humidity, Temp: temp}
fmt.Print(weather)
}

@ -0,0 +1,56 @@
package main
import "fmt"
var WeatherCodesDay map[string]rune
var WeatherCodesNight map[string]rune
func SymbolTest() {
WeatherCodesDay := map[string]rune{
"Unkown": '\ue374',
"Cloudy": '\ue302',
"Fog": '\ue303',
"HeavyRain": '\ue318',
"HeavyShowers": '\ue318',
"HeavySnow": '\ue31a',
"HeavySnowShowers": '\ue31a',
"LightRain": '\ue308',
"LightShowers": '\ue308',
"LightSleet": '\ue3aa',
"LightSleetShowers": '\ue3aa',
"LightSnow": '\ue30a',
"LightSnowShowers": '\ue30a',
"PartlyCloudy": '\ue37b',
"Sunny": '\ue30d',
"ThunderyHeavyRain": '\ue31d',
"ThunderyShowers": '\ue31d',
"ThunderySnowShowers": '\ue365',
"VeryCloudy": '\ue312',
}
WeatherCodesNight := map[string]rune{
"Unkown": '\ue374',
"Cloudy": '\ue37e',
"Fog": '\ue346',
"HeavyRain": '\ue318',
"HeavyShowers": '\ue318',
"HeavySnow": '\ue31a',
"HeavySnowShowers": '\ue31a',
"LightRain": '\ue325',
"LightShowers": '\ue325',
"LightSleet": '\ue3ac',
"LightSleetShowers": '\ue3ac',
"LightSnow": '\ue361',
"LightSnowShowers": '\ue361',
"PartlyCloudy": '\ue379',
"Sunny": '\ue32b',
"ThunderyHeavyRain": '\ue31d',
"ThunderyShowers": '\ue31d',
"ThunderySnowShowers": '\ue365',
"VeryCloudy": '\ue312',
}
for k, v := range WeatherCodesDay {
fmt.Printf("%s: %c %c\n", k, v, WeatherCodesNight[k])
}
}

Binary file not shown.

@ -10,6 +10,7 @@ CUR_TIMESTAMP=$(date +%s)
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')

Loading…
Cancel
Save