You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-weather/weather.go

139 lines
3.1 KiB

package main
import (
"fmt"
"time"
"github.com/fatih/color"
)
// WeatherAPI ...
type WeatherAPI struct {
Base string `json:"base"`
Name string `json:"name"`
Weather []Weather `json:"weather"`
Sys Sys `json:"sys"`
Main Main `json:"main"`
Wind Wind `json:"wind"`
Coord Coord `json:"coord"`
Rain Rain `json:"rain"`
Clouds Cloud `json:"cloud"`
DT int64 `json:"dt"`
Visability int64 `json:"visibility"`
TimeZone int `json:"timezone"`
ID int `json:"id"`
COD int `json:"cod"`
}
// Coord ...
type Coord struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
}
// Weather ...
type Weather struct {
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
ID int `json:"id"`
}
// Main ...
type Main struct {
Temp float64 `json:"temp"`
FeelsLike float64 `json:"feels_like"`
TempMin float64 `json:"temp_min"`
TempMax float64 `json:"temp_max"`
Pressure float64 `json:"pressure"`
Humidity float64 `json:"humidity"`
}
// Wind ...
type Wind struct {
Speed float64 `json:"speed"`
Deg float64 `json:"deg"`
Gust float64 `json:"gust"`
}
// Rain ...
type Rain struct {
T1h float64 `json:"1h"`
}
// Cloud ...
type Cloud struct {
All int `json:"all"`
}
// Sys ...
type Sys struct {
Country string `json:"country"`
Types int `json:"type"`
ID int64 `json:"id"`
Sunrise int64 `json:"sunrise"`
Sunset int64 `json:"sunset"`
}
func iconToEmoji(icon string) {
//info. from https://openweathermap.org/weather-conditions
var emoji string
switch icon {
case "Clouds":
emoji = "☁"
case "Drizzle":
emoji = "🌧"
case "Rain":
emoji = "🌧"
case "Snow":
emoji = "❄"
case "Clear":
emoji = "☀"
case "Mist":
emoji = "🌫"
case "Tornado":
emoji = "🌪"
case "Thunderstorm":
emoji = "⛈"
default:
emoji = ""
}
fmt.Print(emoji)
}
func printWeather(data WeatherAPI) {
weatherData := data
white := color.New(color.FgWhite)
boldWhite := white.Add(color.Bold)
dt := time.Now()
boldWhite.Println(dt.Format("01/02/2006 15:04"))
strLoc := weatherData.Name
strCode := weatherData.Sys.Country
strWeather := weatherData.Weather[0].Main
strWeatherDetail := weatherData.Weather[0].Description
strWeatherIcon := weatherData.Weather[0].Main
strTemp := weatherData.Main.Temp
strTempFl := weatherData.Main.FeelsLike
strTempMin := weatherData.Main.TempMin
strTempMax := weatherData.Main.TempMax
strSunset := time.Unix(weatherData.Sys.Sunset, 0)
strSunrise := time.Unix(weatherData.Sys.Sunrise, 0)
boldWhite.Printf("📍Location: ")
fmt.Printf("%v, %v\n", strLoc, strCode)
boldWhite.Printf("Weather:")
iconToEmoji(strWeatherIcon)
fmt.Printf(" %v (%v)\n", strWeather, strWeatherDetail)
boldWhite.Printf("🌡 Temp: ")
fmt.Printf("%v°C (Feels like: %v°C)\n(Min: %v°C, Max: %v°C)\n", strTemp, strTempFl, strTempMin, strTempMax)
boldWhite.Printf("🌅Sunrise: ")
fmt.Printf("%v\t", strSunrise.Format("15:04"))
boldWhite.Printf("🌇Sunset: ")
fmt.Printf("%v\n", strSunset.Format("15:04"))
}