Compare commits
8
Commits
05c80dd5a0
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c7b872782 | ||
|
|
53ec5bbccf | ||
|
|
f1fca715fb | ||
|
|
9cef9579e3 | ||
|
|
fc411c23d5 | ||
|
|
6a6afcc232 | ||
|
|
7325d252c7 | ||
|
|
6f5ab2ded6 |
@@ -5,16 +5,16 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createFile(fileContents string) {
|
func createFile(fileContents string, filename string) {
|
||||||
file, err := os.Create("weather.json")
|
file, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed creating file: %s", err)
|
log.Fatalf("failed creating file %s: %s", filename, err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
_, err = file.WriteString(fileContents)
|
_, err = file.WriteString(fileContents)
|
||||||
if err != nil {
|
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 {
|
if e != nil {
|
||||||
log.Fatal(e)
|
log.Fatal(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ require github.com/fatih/color v1.13.0
|
|||||||
require (
|
require (
|
||||||
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
|
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.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=
|
||||||
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.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 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||||
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/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-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-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
|
||||||
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|||||||
@@ -6,13 +6,15 @@ 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
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,35 +6,40 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//TODO: finish this!
|
||||||
|
|
||||||
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 + ""
|
var filename string
|
||||||
|
filename = "weather.json"
|
||||||
|
|
||||||
|
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)
|
||||||
if err != nil {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Create file
|
//Create file
|
||||||
createFile(string(html))
|
createFile(string(html), filename)
|
||||||
|
defer removeFile(filename) //delete file at the end
|
||||||
|
|
||||||
//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)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Print the weather!
|
//Print the weather!
|
||||||
printWeather(data)
|
printWeather(data)
|
||||||
|
|
||||||
//remove the file at the end
|
|
||||||
removeFile("weather.json")
|
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-32
@@ -7,62 +7,70 @@ import (
|
|||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// WeatherAPI ...
|
||||||
type WeatherAPI struct {
|
type WeatherAPI struct {
|
||||||
Coord Coord `json:"coord"`
|
|
||||||
Weather []Weather `json:"weather"`
|
|
||||||
Base string `json:"base"`
|
Base string `json:"base"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Weather []Weather `json:"weather"`
|
||||||
|
Sys Sys `json:"sys"`
|
||||||
Main Main `json:"main"`
|
Main Main `json:"main"`
|
||||||
Visability int64 `json:"visibility"`
|
|
||||||
Wind Wind `json:"wind"`
|
Wind Wind `json:"wind"`
|
||||||
|
Coord Coord `json:"coord"`
|
||||||
Rain Rain `json:"rain"`
|
Rain Rain `json:"rain"`
|
||||||
Clouds Cloud `json:"cloud"`
|
Clouds Cloud `json:"cloud"`
|
||||||
DT int64 `json:"dt"`
|
DT int64 `json:"dt"`
|
||||||
Sys Sys `json:"sys"`
|
Visability int64 `json:"visibility"`
|
||||||
TimeZone int `json:"timezone"`
|
TimeZone int `json:"timezone"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Name string `json:"name"`
|
|
||||||
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"`
|
|
||||||
Main string `json:"main"`
|
Main string `json:"main"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
|
ID int `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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"`
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
Country string `json:"country"`
|
Country string `json:"country"`
|
||||||
|
Types int `json:"type"`
|
||||||
|
ID int64 `json:"id"`
|
||||||
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,21 +6,15 @@ 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 {
|
defer resp.Body.Close()
|
||||||
fmt.Println(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
//check response codes
|
//check response codes
|
||||||
if resp.StatusCode >= 400 && resp.StatusCode <= 499 {
|
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
|
return html, err
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user