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/fatih/color v1.13.0
require ( require (
github.com/keegancsmith/rpc v1.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // 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 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= 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.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 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= 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/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 h1:OvySnanP8CQIKS+MTq9AXBwEXzm0YaKeu331bWql3ug=
github.com/mqu/go-notify v0.0.0-20130719194048-ef6f6f49d093/go.mod h1:AthsKyBZ9hqwU7DBWFiOxYObyF8nVyYVubXv/pQNC5E= 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-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-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-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-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 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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" "os"
) )
func loadJson(file string) (WeatherAPI, error) { func loadJSON(file string) (WeatherAPI, error) {
var config WeatherAPI var config WeatherAPI
configFile, err := os.Open(file) configFile, err := os.Open(file)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
jsonParser := json.NewDecoder(configFile) jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config) jsonParser.Decode(&config)
return config, err return config, err

@ -8,13 +8,13 @@ import (
func main() { func main() {
//set up the flags for the program to use! //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(&city, "city", "London", "Enter city e.g. London")
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk") 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() 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 //read webpage
html, err := loadWebPage(full) html, err := loadWebPage(full)
@ -27,7 +27,7 @@ func main() {
createFile(string(html)) createFile(string(html))
//Load the file //Load the file
data, err := loadJson("weather.json") data, err := loadJSON("weather.json")
if err != nil { if err != nil {
fmt.Println("Cannot load weather.json file!") fmt.Println("Cannot load weather.json file!")
os.Exit(1) os.Exit(1)

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

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

Loading…
Cancel
Save