Files
go-weather/main.go
T

46 lines
1.0 KiB
Go
Raw Normal View History

2022-02-13 12:04:27 +00:00
package main
2022-02-13 22:52:57 +00:00
import (
2022-02-13 23:27:23 +00:00
"flag"
2022-02-13 22:52:57 +00:00
"fmt"
"os"
)
2022-09-16 13:09:03 +01:00
//TODO: finish this!
2022-02-13 12:04:27 +00:00
func main() {
2022-02-13 23:27:23 +00:00
//set up the flags for the program to use!
2022-08-23 14:52:06 +01:00
var city, code, apiKey string
2022-02-25 13:55:53 +00:00
flag.StringVar(&city, "city", "London", "Enter city e.g. London")
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk")
2022-08-23 14:52:06 +01:00
flag.StringVar(&apiKey, "api", "none", "Enter you api key here")
2022-02-13 23:27:23 +00:00
flag.Parse()
2022-02-15 10:05:10 +00:00
2023-02-09 11:56:15 +00:00
var filename string
filename = "weather.json"
2022-08-23 14:52:06 +01:00
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + apiKey + ""
2022-02-13 22:52:57 +00:00
2022-02-13 23:27:23 +00:00
//read webpage
2022-07-25 12:57:17 +01:00
html, err := loadWebPage(full)
if err != nil {
2022-09-03 13:46:41 +01:00
//fmt.Println("4xx error, please try again!")
fmt.Fprintf(os.Stderr, "ERROR: Could not read website: %s\n", err)
2022-07-25 12:57:17 +01:00
os.Exit(1)
}
2022-02-15 21:12:18 +00:00
2022-02-15 10:05:10 +00:00
//Create file
2023-02-09 11:56:15 +00:00
createFile(string(html), filename)
defer removeFile(filename) //delete file at the end
2022-07-25 12:57:17 +01:00
2022-02-17 13:38:57 +00:00
//Load the file
2022-08-23 14:52:06 +01:00
data, err := loadJSON("weather.json")
2022-07-25 12:57:17 +01:00
if err != nil {
fmt.Println("Cannot load weather.json file!")
2022-07-25 13:56:19 +01:00
os.Exit(1)
2022-07-25 12:57:17 +01:00
}
2022-02-17 13:38:57 +00:00
//Print the weather!
2022-02-17 10:45:34 +00:00
printWeather(data)
2022-02-15 21:12:18 +00:00
}