summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.name>2018-03-15 23:41:24 +0000
committerNick White <git@njw.name>2018-03-15 23:41:24 +0000
commit0765fa94a107369979ee8316afd2af8656f5489b (patch)
tree04ba16b510b4615b2b004d692ecc44e566d4be1c
downloadweather-0765fa94a107369979ee8316afd2af8656f5489b.tar.bz2
weather-0765fa94a107369979ee8316afd2af8656f5489b.zip
Add wip weather
-rw-r--r--weather.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/weather.go b/weather.go
new file mode 100644
index 0000000..161fd7b
--- /dev/null
+++ b/weather.go
@@ -0,0 +1,91 @@
+package main
+
+// TODO: allow free-text lookups of place names, rather than ids
+// TODO: decode weather type number
+
+import (
+ "encoding/json"
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "net/http"
+ "sort"
+)
+
+const defid = "310118"
+
+var wkey = map[string]string{
+ "f": "Feels Like Temperature",
+ "h": "Humidity",
+ "p": "Pressure",
+ "t": "Temperature",
+ "v": "Visibility",
+ "wg": "Wind Gust",
+ "ws": "Wind Speed",
+ "pp": "Precipitaton Probability",
+ "uv": "Max UV Index",
+ "wd": "Wind Direction",
+ "wt": "Weather Type",
+ "AQIndex": "Air Quality",
+}
+
+func main() {
+ var j map[string]interface{}
+ var id string
+
+ flag.Parse()
+ if flag.NArg() > 0 {
+ id = flag.Arg(0)
+ } else {
+ id = defid
+ }
+
+ tr := &http.Transport{}
+ client := &http.Client{Transport: tr}
+ resp, err := client.Get("https://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Forecast/" + id + ".json?concise=true")
+ //resp, err := http.Get("https://www.metoffice.gov.uk/public/data/PWSCache/BestForecast/Forecast/" + id + ".json?concise=true")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer resp.Body.Close()
+ b, err := ioutil.ReadAll(resp.Body)
+ err = json.Unmarshal(b, &j)
+ 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 {
+ fmt.Printf("-----------------------------\n")
+ fmt.Printf("Date: %v\n", d.(map[string]interface{})["@date"])
+ fmt.Printf("\nDay weather:\n")
+ prettyWeather(d.(map[string]interface{})["DayValues"].(map[string]interface{})["WeatherParameters"].(map[string]interface{}))
+ fmt.Printf("\nNight weather:\n")
+ prettyWeather(d.(map[string]interface{})["NightValues"].(map[string]interface{})["WeatherParameters"].(map[string]interface{}))
+
+ 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{}))
+ }
+ }
+}
+
+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)
+ }
+ }
+}