Compare commits
8
Commits
05c80dd5a0
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c7b872782 | ||
|
|
53ec5bbccf | ||
|
|
f1fca715fb | ||
|
|
9cef9579e3 | ||
|
|
fc411c23d5 | ||
|
|
6a6afcc232 | ||
|
|
7325d252c7 | ||
|
|
6f5ab2ded6 |
@@ -5,16 +5,16 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func createFile(fileContents string) {
|
||||
file, err := os.Create("weather.json")
|
||||
func createFile(fileContents string, filename string) {
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
log.Fatalf("failed creating file: %s", err)
|
||||
log.Fatalf("failed creating file %s: %s", filename, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.WriteString(fileContents)
|
||||
if err != nil {
|
||||
log.Fatalf("failed writing to file: %s", err)
|
||||
log.Fatalf("failed writing to file %s: %s", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,5 @@ func removeFile(file string) {
|
||||
if e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,5 +7,5 @@ require github.com/fatih/color v1.13.0
|
||||
require (
|
||||
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
|
||||
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
|
||||
)
|
||||
|
||||
@@ -3,16 +3,12 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF
|
||||
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=
|
||||
github.com/mattn/go-gtk v0.0.0-20191030024613-af2e013261f5 h1:GMB3MVJnxysGrSvjWGsgK8L3XGI3F4etQQq37Py6W5A=
|
||||
github.com/mattn/go-gtk v0.0.0-20191030024613-af2e013261f5/go.mod h1:PwzwfeB5syFHXORC3MtPylVcjIoTDT/9cvkKpEndGVI=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
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=
|
||||
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=
|
||||
|
||||
@@ -6,13 +6,15 @@ 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
|
||||
|
||||
}
|
||||
|
||||
@@ -6,35 +6,40 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
//TODO: finish this!
|
||||
|
||||
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 + ""
|
||||
var filename string
|
||||
filename = "weather.json"
|
||||
|
||||
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + apiKey + ""
|
||||
|
||||
//read webpage
|
||||
html, err := loadWebPage(full)
|
||||
if err != nil {
|
||||
fmt.Println("4xx error, please try again!")
|
||||
//fmt.Println("4xx error, please try again!")
|
||||
fmt.Fprintf(os.Stderr, "ERROR: Could not read website: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
//Create file
|
||||
createFile(string(html))
|
||||
createFile(string(html), filename)
|
||||
defer removeFile(filename) //delete file at the end
|
||||
|
||||
//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)
|
||||
}
|
||||
|
||||
//Print the weather!
|
||||
printWeather(data)
|
||||
|
||||
//remove the file at the end
|
||||
removeFile("weather.json")
|
||||
}
|
||||
|
||||
+40
-35
@@ -7,62 +7,70 @@ import (
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
// WeatherAPI ...
|
||||
type WeatherAPI struct {
|
||||
Coord Coord `json:"coord"`
|
||||
Weather []Weather `json:"weather"`
|
||||
Base string `json:"base"`
|
||||
Name string `json:"name"`
|
||||
Weather []Weather `json:"weather"`
|
||||
Sys Sys `json:"sys"`
|
||||
Main Main `json:"main"`
|
||||
Visability int64 `json:"visibility"`
|
||||
Wind Wind `json:"wind"`
|
||||
Coord Coord `json:"coord"`
|
||||
Rain Rain `json:"rain"`
|
||||
Clouds Cloud `json:"cloud"`
|
||||
DT int64 `json:"dt"`
|
||||
Sys Sys `json:"sys"`
|
||||
Visability int64 `json:"visibility"`
|
||||
TimeZone int `json:"timezone"`
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
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"`
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"`
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
// 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"`
|
||||
Pressure float64 `json:"pressure"`
|
||||
Humidity float64 `json:"humidity"`
|
||||
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 {
|
||||
Types int `json:"type"`
|
||||
Id int64 `json:"id"`
|
||||
Country string `json:"country"`
|
||||
Types int `json:"type"`
|
||||
ID int64 `json:"id"`
|
||||
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,21 +6,15 @@ 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())
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
//check response codes
|
||||
if resp.StatusCode >= 400 && resp.StatusCode <= 499 {
|
||||
err = fmt.Errorf("response Code: %v", resp.StatusCode)
|
||||
err = fmt.Errorf("got response code %v", resp.StatusCode)
|
||||
}
|
||||
return html, err
|
||||
|
||||
|
||||
Reference in New Issue
Block a user