summaryrefslogtreecommitdiff
path: root/weather.go
diff options
context:
space:
mode:
Diffstat (limited to 'weather.go')
-rw-r--r--weather.go65
1 files changed, 28 insertions, 37 deletions
diff --git a/weather.go b/weather.go
index b81cc9f..56d6bdf 100644
--- a/weather.go
+++ b/weather.go
@@ -12,7 +12,6 @@ import (
"io/ioutil"
"log"
"net/http"
- "sort"
)
const defid = "310118"
@@ -34,24 +33,31 @@ var wkey = map[string]string{
type Response struct {
BestFcst struct {
- Location struct {
- Days []Day `json:"Day"`
+ Forecast struct {
+ Location struct {
+ Days []Day `json:"Day"`
+ }
}
}
}
type Day struct {
- DayValues WeatherParam
- NightValues WeatherParam
- TimeSteps struct {
- TimeStep struct {
- Time string `json:"@time"`
- WeatherParams []WeatherParam
+ Date string `json:"@date"`
+ DayValues struct {
+ WeatherParameters WeatherParams
+ }
+ NightValues struct {
+ WeatherParameters WeatherParams
+ }
+ TimeSteps struct {
+ TimeStep []struct {
+ Time string `json:"@time"`
+ WeatherParameters WeatherParams
}
}
}
-type WeatherParam struct {
+type WeatherParams struct {
AQIndex float64
F float64
H float64
@@ -67,7 +73,7 @@ type WeatherParam struct {
}
func main() {
- var j map[string]interface{}
+ var r Response
var id string
flag.Parse()
@@ -77,7 +83,6 @@ func main() {
id = defid
}
- // Use http.Client.Get as http.Get doesn't seem to handle TLS
client := &http.Client{Transport: &http.Transport{}}
resp, err := client.Get("https://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Forecast/" + id + ".json?concise=true")
if err != nil {
@@ -85,42 +90,28 @@ func main() {
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
- err = json.Unmarshal(b, &j)
+ err = json.Unmarshal(b, &r)
if err != nil {
log.Fatal(err)
}
- // TODO: make this cleaner, perhaps by pre-speccing the structs
- days := j["BestFcst"].(map[string]interface{})["Forecast"].(map[string]interface{})["Location"].(map[string]interface{})["Day"].([]interface{})
- for _, d := range days {
+ for _, d := range r.BestFcst.Forecast.Location.Days {
fmt.Printf("-----------------------------\n")
- fmt.Printf("Date: %v\n", d.(map[string]interface{})["@date"])
+ fmt.Printf("Date: %v\n", d.Date)
fmt.Printf("\nDay weather:\n")
- prettyWeather(d.(map[string]interface{})["DayValues"].(map[string]interface{})["WeatherParameters"].(map[string]interface{}))
+ prettyWeather(d.DayValues.WeatherParameters)
fmt.Printf("\nNight weather:\n")
- prettyWeather(d.(map[string]interface{})["NightValues"].(map[string]interface{})["WeatherParameters"].(map[string]interface{}))
+ prettyWeather(d.NightValues.WeatherParameters)
fmt.Printf("\nTime steps:\n")
- timesteps := d.(map[string]interface{})["TimeSteps"].(map[string]interface{})["TimeStep"].([]interface{})
- for _, t := range timesteps {
- fmt.Printf("\nTime: %s\n", t.(map[string]interface{})["@time"])
- prettyWeather(t.(map[string]interface{})["WeatherParameters"].(map[string]interface{}))
+ for _, t := range d.TimeSteps.TimeStep {
+ fmt.Printf("\nTime: %s\n", t.Time)
+ prettyWeather(t.WeatherParameters)
}
}
}
-func prettyWeather(w map[string]interface{}) {
- // TODO: choose what to show, and in what order, to have a compact output
- var keys []string
- for k, _ := range wkey {
- keys = append(keys, k)
- }
- sort.Strings(keys)
-
- for _, k := range keys {
- t, ok := w[k]
- if ok {
- fmt.Printf("%s: %v\n", wkey[k], t)
- }
- }
+func prettyWeather(w WeatherParams) {
+ // TODO: Print weather nicely
+ fmt.Printf("%+v\n", w)
}