|
|
@ -8,6 +8,7 @@ import ( |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
|
|
|
|
"time" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type WeatherAPI struct { |
|
|
|
type WeatherAPI struct { |
|
|
@ -75,27 +76,25 @@ func main() { |
|
|
|
var city, code, api_key string |
|
|
|
var city, code, api_key string |
|
|
|
flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc... (Default: London)") |
|
|
|
flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc... (Default: London)") |
|
|
|
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc... (Default: uk)") |
|
|
|
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc... (Default: uk)") |
|
|
|
flag.StringVar(&code, "api", "none", "Enter Code e.g. uk, us etc... Default(none)") |
|
|
|
flag.StringVar(&api_key, "api", "none", "Enter Code e.g. uk, us etc... Default(none)") |
|
|
|
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=" + api_key + "" |
|
|
|
|
|
|
|
|
|
|
|
//read webpage
|
|
|
|
//read webpage
|
|
|
|
url := full |
|
|
|
html, _ := LoadWebPage(full) |
|
|
|
resp, err := http.Get(url) |
|
|
|
|
|
|
|
//reads html as a slice of bytes
|
|
|
|
|
|
|
|
html, err := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
panic(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Create file
|
|
|
|
//Create file
|
|
|
|
CreateFile(string(html), false) |
|
|
|
CreateFile(string(html), false) |
|
|
|
|
|
|
|
|
|
|
|
data, _ := LoadConfiguration("weather.json") |
|
|
|
data, _ := LoadJson("weather.json") |
|
|
|
fmt.Println(data) |
|
|
|
|
|
|
|
|
|
|
|
//print the weather!
|
|
|
|
|
|
|
|
PrintWeather(data) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func LoadConfiguration(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 { |
|
|
@ -107,8 +106,6 @@ func LoadConfiguration(file string) (WeatherAPI, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func CreateFile(fileContents string, debugFlag bool) { |
|
|
|
func CreateFile(fileContents string, debugFlag bool) { |
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Writing to a file in Go lang\n") |
|
|
|
|
|
|
|
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) |
|
|
@ -127,3 +124,32 @@ func CreateFile(fileContents string, debugFlag bool) { |
|
|
|
fmt.Printf("\nLength: %d bytes", len) |
|
|
|
fmt.Printf("\nLength: %d bytes", len) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func LoadWebPage(web_url string) ([]byte, error) { |
|
|
|
|
|
|
|
url := web_url |
|
|
|
|
|
|
|
resp, err := http.Get(url) |
|
|
|
|
|
|
|
//reads html as a slice of bytes
|
|
|
|
|
|
|
|
html, err := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
panic(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return html, err |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func PrintWeather(data WeatherAPI) { |
|
|
|
|
|
|
|
weatherData := data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dt := time.Now() |
|
|
|
|
|
|
|
fmt.Println(dt.Format("01/02/2006\t15:04")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
str_loc := weatherData.Name |
|
|
|
|
|
|
|
str_code := weatherData.Sys.Country |
|
|
|
|
|
|
|
str_weather := weatherData.Weather[0].Main |
|
|
|
|
|
|
|
str_weather_detail := weatherData.Weather[0].Description |
|
|
|
|
|
|
|
str_temp := weatherData.Main.Temp |
|
|
|
|
|
|
|
str_temp_fl := weatherData.Main.Feels_like |
|
|
|
|
|
|
|
str_hum := weatherData.Main.Humidity |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
} |
|
|
|