|
|
@ -9,6 +9,9 @@ import ( |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/fatih/color" |
|
|
|
|
|
|
|
"github.com/sami2020pro/gmoji" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type WeatherAPI struct { |
|
|
|
type WeatherAPI struct { |
|
|
@ -82,19 +85,20 @@ func main() { |
|
|
|
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=" + api_key + "" |
|
|
|
|
|
|
|
|
|
|
|
//read webpage
|
|
|
|
//read webpage
|
|
|
|
html, _ := LoadWebPage(full) |
|
|
|
html, _ := loadWebPage(full) |
|
|
|
|
|
|
|
|
|
|
|
//Create file
|
|
|
|
//Create file
|
|
|
|
CreateFile(string(html), false) |
|
|
|
createFile(string(html), false) |
|
|
|
|
|
|
|
|
|
|
|
data, _ := LoadJson("weather.json") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data, _ := loadJson("weather.json") |
|
|
|
//print the weather!
|
|
|
|
//print the weather!
|
|
|
|
PrintWeather(data) |
|
|
|
printWeather(data) |
|
|
|
|
|
|
|
color.Cyan("Prints text in cyan.") |
|
|
|
|
|
|
|
color.Red("Hello!") |
|
|
|
|
|
|
|
fmt.Printf("Hello %v\n", gmoji.Fire) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
@ -105,7 +109,7 @@ func LoadJson(file string) (WeatherAPI, error) { |
|
|
|
return config, err |
|
|
|
return config, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func CreateFile(fileContents string, debugFlag bool) { |
|
|
|
func createFile(fileContents string, debugFlag bool) { |
|
|
|
file, err := os.Create("weather.json") |
|
|
|
file, err := os.Create("weather.json") |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Fatalf("failed creating file: %s", err) |
|
|
|
log.Fatalf("failed creating file: %s", err) |
|
|
@ -125,7 +129,7 @@ func CreateFile(fileContents string, debugFlag bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func LoadWebPage(web_url string) ([]byte, error) { |
|
|
|
func loadWebPage(web_url string) ([]byte, error) { |
|
|
|
url := web_url |
|
|
|
url := web_url |
|
|
|
resp, err := http.Get(url) |
|
|
|
resp, err := http.Get(url) |
|
|
|
//reads html as a slice of bytes
|
|
|
|
//reads html as a slice of bytes
|
|
|
@ -137,11 +141,20 @@ func LoadWebPage(web_url string) ([]byte, error) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func PrintWeather(data WeatherAPI) { |
|
|
|
/* |
|
|
|
|
|
|
|
func iconToEmoji(icon string) (emoji string) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func printWeather(data WeatherAPI) { |
|
|
|
weatherData := data |
|
|
|
weatherData := data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
whilte := color.New(color.FgWhite) |
|
|
|
|
|
|
|
boldWhite := whilte.Add(color.Bold) |
|
|
|
|
|
|
|
|
|
|
|
dt := time.Now() |
|
|
|
dt := time.Now() |
|
|
|
fmt.Println(dt.Format("01/02/2006\t15:04")) |
|
|
|
fmt.Println(dt.Format("01/02/2006 15:04")) |
|
|
|
|
|
|
|
|
|
|
|
str_loc := weatherData.Name |
|
|
|
str_loc := weatherData.Name |
|
|
|
str_code := weatherData.Sys.Country |
|
|
|
str_code := weatherData.Sys.Country |
|
|
@ -149,7 +162,18 @@ func PrintWeather(data WeatherAPI) { |
|
|
|
str_weather_detail := weatherData.Weather[0].Description |
|
|
|
str_weather_detail := weatherData.Weather[0].Description |
|
|
|
str_temp := weatherData.Main.Temp |
|
|
|
str_temp := weatherData.Main.Temp |
|
|
|
str_temp_fl := weatherData.Main.Feels_like |
|
|
|
str_temp_fl := weatherData.Main.Feels_like |
|
|
|
str_hum := weatherData.Main.Humidity |
|
|
|
str_temp_min := weatherData.Main.Temp_min |
|
|
|
|
|
|
|
str_temp_max := weatherData.Main.Temp_max |
|
|
|
fmt.Printf("Location: %v, %v\nWeather: %v (%v)\nTemp: %v°C (Feels like: %v°C)\nHumidity: %v", str_loc, str_code, str_weather, str_weather_detail, str_temp, str_temp_fl, str_hum) |
|
|
|
str_sunset := time.Unix(weatherData.Sys.Sunset, 0) |
|
|
|
|
|
|
|
str_sunrise := time.Unix(weatherData.Sys.Sunrise, 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boldWhite.Printf("Location: %v, %v\n"+ |
|
|
|
|
|
|
|
"Weather: %v (%v)\n"+ |
|
|
|
|
|
|
|
"Temp: %v°C (Feels like: %v°C)\n(Min: %v°C, Max: %v°C)\n"+ |
|
|
|
|
|
|
|
"Sunrise: %v\t"+ |
|
|
|
|
|
|
|
"Sunset: %v\n", |
|
|
|
|
|
|
|
str_loc, str_code, |
|
|
|
|
|
|
|
str_weather, str_weather_detail, |
|
|
|
|
|
|
|
str_temp, str_temp_fl, str_temp_min, str_temp_max, |
|
|
|
|
|
|
|
str_sunrise.Format("15:04"), str_sunset.Format("15:04")) |
|
|
|
} |
|
|
|
} |
|
|
|