tweaks and fixes

master
simonkellet 2 years ago
parent 6f5ab2ded6
commit 7325d252c7
  1. 6
      go.mod
  2. 18
      go.sum
  3. 3
      json.go
  4. 8
      main.go
  5. 55
      weather.go
  6. 11
      web.go

@ -5,7 +5,11 @@ go 1.17
require github.com/fatih/color v1.13.0
require (
github.com/keegancsmith/rpc v1.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
github.com/stamblerre/gocode v1.0.0 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
golang.org/x/tools v0.1.12 // indirect
)

@ -1,5 +1,8 @@
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/keegancsmith/rpc v1.1.0/go.mod h1:Xow74TKX34OPPiPCdz6x1o9c0SCxRqGxDuKGk7ZOo8s=
github.com/keegancsmith/rpc v1.3.0 h1:wGWOpjcNrZaY8GDYZJfvyxmlLljm3YQWF+p918DXtDk=
github.com/keegancsmith/rpc v1.3.0/go.mod h1:6O2xnOGjPyvIPbvp0MdrOe5r6cu1GZ4JoTzpzDhWeo0=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
@ -10,9 +13,24 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mqu/go-notify v0.0.0-20130719194048-ef6f6f49d093 h1:OvySnanP8CQIKS+MTq9AXBwEXzm0YaKeu331bWql3ug=
github.com/mqu/go-notify v0.0.0-20130719194048-ef6f6f49d093/go.mod h1:AthsKyBZ9hqwU7DBWFiOxYObyF8nVyYVubXv/pQNC5E=
github.com/stamblerre/gocode v1.0.0 h1:5aTRgkRTOS8mELHoKatkwhfX44OdEV3iwu3FCXyvLzk=
github.com/stamblerre/gocode v1.0.0/go.mod h1:ONyGamdxpnxaG2+XLyGkNuuoYISmz0QFVHScxvsXsqM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -6,12 +6,13 @@ import (
"os"
)
func loadJson(file string) (WeatherAPI, error) {
func loadJSON(file string) (WeatherAPI, error) {
var config WeatherAPI
configFile, err := os.Open(file)
if err != nil {
fmt.Println(err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
return config, err

@ -8,13 +8,13 @@ import (
func main() {
//set up the flags for the program to use!
var city, code, api_key string
var city, code, apiKey string
flag.StringVar(&city, "city", "London", "Enter city e.g. London")
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk")
flag.StringVar(&api_key, "api", "none", "Enter you api key here")
flag.StringVar(&apiKey, "api", "none", "Enter you api key here")
flag.Parse()
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + api_key + ""
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + apiKey + ""
//read webpage
html, err := loadWebPage(full)
@ -27,7 +27,7 @@ func main() {
createFile(string(html))
//Load the file
data, err := loadJson("weather.json")
data, err := loadJSON("weather.json")
if err != nil {
fmt.Println("Cannot load weather.json file!")
os.Exit(1)

@ -7,6 +7,7 @@ import (
"github.com/fatih/color"
)
// WeatherAPI ...
type WeatherAPI struct {
Coord Coord `json:"coord"`
Weather []Weather `json:"weather"`
@ -24,11 +25,13 @@ type WeatherAPI struct {
COD int `json:"cod"`
}
// Coord ...
type Coord struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
}
// Weather ...
type Weather struct {
ID int `json:"id"`
Main string `json:"main"`
@ -36,32 +39,37 @@ type Weather struct {
Icon string `json:"icon"`
}
// Main ...
type Main struct {
Temp float64 `json:"temp"`
Feels_like float64 `json:"feels_like"`
Temp_min float64 `json:"temp_min"`
Temp_max float64 `json:"temp_max"`
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 {
Types int `json:"type"`
Id int64 `json:"id"`
ID int64 `json:"id"`
Country string `json:"country"`
Sunrise int64 `json:"sunrise"`
Sunset int64 `json:"sunset"`
@ -88,7 +96,7 @@ func iconToEmoji(icon string) {
case "Thunderstorm":
emoji = "⛈"
default:
emoji = ""
emoji = ""
}
fmt.Print(emoji)
}
@ -102,33 +110,30 @@ func printWeather(data WeatherAPI) {
dt := time.Now()
boldWhite.Println(dt.Format("01/02/2006 15:04"))
str_loc := weatherData.Name
str_code := weatherData.Sys.Country
str_weather := weatherData.Weather[0].Main
str_weather_detail := weatherData.Weather[0].Description
str_weather_icon := weatherData.Weather[0].Main
str_temp := weatherData.Main.Temp
str_temp_fl := weatherData.Main.Feels_like
str_temp_min := weatherData.Main.Temp_min
str_temp_max := weatherData.Main.Temp_max
str_sunset := time.Unix(weatherData.Sys.Sunset, 0)
str_sunrise := time.Unix(weatherData.Sys.Sunrise, 0)
//str_wind_speed := weatherData.Wind.Speed
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", str_loc, str_code)
fmt.Printf("%v, %v\n", strLoc, strCode)
boldWhite.Printf("Weather:")
iconToEmoji(str_weather_icon)
fmt.Printf(" %v (%v)\n", str_weather, str_weather_detail)
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", str_temp, str_temp_fl, str_temp_min, str_temp_max)
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", str_sunrise.Format("15:04"))
fmt.Printf("%v\t", strSunrise.Format("15:04"))
boldWhite.Printf("🌇Sunset: ")
fmt.Printf("%v\n", str_sunset.Format("15:04"))
//fmt.Printf("Wind Speed: %vm/s", str_wind_speed)
fmt.Printf("%v\n", strSunset.Format("15:04"))
}

@ -6,17 +6,10 @@ import (
"net/http"
)
func loadWebPage(web_url string) ([]byte, error) {
resp, err := http.Get(web_url)
if err != nil {
fmt.Println(err.Error())
}
func loadWebPage(webURL string) ([]byte, error) {
resp, err := http.Get(webURL)
//reads html as a slice of bytes
html, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
}
//check response codes
if resp.StatusCode >= 400 && resp.StatusCode <= 499 {

Loading…
Cancel
Save