From ff765c0fdf2b1edae47a57b3ce35535c83273588 Mon Sep 17 00:00:00 2001 From: NiseVoid Date: Tue, 28 Jan 2020 11:18:34 +0100 Subject: [PATCH] Update to new api --- main.go | 48 +++++++++++------------------------------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/main.go b/main.go index 5f1793e..0785646 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "encoding/csv" "encoding/json" "fmt" @@ -13,39 +12,13 @@ import ( "git.ultraware.nl/Ultraware/util" ) -type iso8601 time.Time - -func (d iso8601) String() string { - return time.Time(d).String() -} - -func (d *iso8601) UnmarshalJSON(data []byte) error { - data = bytes.Trim(data, `"`) - - t, err := time.Parse(`2006-01-02T15:04:05-0700`, string(data)) - if err != nil { - return err - } - - *d = iso8601(t) - return nil -} - type day struct { - Summary struct { - Date struct { - ISO iso8601 `json:"iso8601"` - } `json:"date"` - MaxTemperature int `json:"max_temperature"` - AvgTemperature int `json:"temperature"` - MinTemperature int `json:"min_temperature"` - } `json:"summary"` + Date int `json:"valid_time_gmt"` + AvgTemperature int `json:"temp"` } type data struct { - History struct { - Days []day `json:"days"` - } `json:"history"` + Observations []day `json:"observations"` } func main() { @@ -60,9 +33,7 @@ func main() { to = time.Now() } - url := `https://api-ak.wunderground.com/api/d8585d80376a429e/history_` + from.Format(`20060102`) + to.Format(`20060102`) + `/lang:EN/units:english/bestfct:1/v:2.0/q/EHGG.json?showObs=0&ttl=120` - fmt.Println(url) - os.Exit(0) + url := `https://api.weather.com/v1/location/EHGG:9:NL/observations/historical.json?apiKey=6532d6454b8aa370768e63d6ba5a832e&units=e&startDate=` + from.Format(`20060102`) + `&endDate=` + to.Format(`20060102`) resp, err := http.Get(url) util.PanicOnErr(err) @@ -78,8 +49,8 @@ func main() { util.PanicOnErr(err) csvout := csv.NewWriter(out) - for _, day := range d.History.Days { - csvout.Write([]string{day.Summary.Date.ISO.String(), fToCString(day.Summary.MinTemperature), fToCString(day.Summary.AvgTemperature), fToCString(day.Summary.MaxTemperature)}) + for _, day := range d.Observations { + csvout.Write([]string{strconv.Itoa(day.Date), fToCString(day.AvgTemperature)}) } csvout.Flush() } @@ -87,7 +58,7 @@ func main() { func getNextDate() time.Time { f, err := os.Open(`weather.csv`) if err != nil { - return time.Date(2019, 2, 1, 0, 0, 0, 0, time.Local) + return time.Date(2019, 11, 1, 0, 0, 0, 0, time.Local) } csvf := csv.NewReader(f) @@ -99,9 +70,12 @@ func getNextDate() time.Time { } last = row } - t, err := time.Parse(`2006-01-02 15:04:05.999999999 -0700 MST`, last[0]) + + u, err := strconv.ParseInt(last[0], 10, 64) util.PanicOnErr(err) + t := time.Unix(u, 0) + return t.AddDate(0, 0, 1) }