Update to new api

This commit is contained in:
Nise Void 2020-01-28 11:18:34 +01:00
parent 83361f8072
commit ff765c0fdf
Signed by: NiseVoid
GPG Key ID: FBA14AC83EA602F3

48
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -13,39 +12,13 @@ import (
"git.ultraware.nl/Ultraware/util" "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 { type day struct {
Summary struct { Date int `json:"valid_time_gmt"`
Date struct { AvgTemperature int `json:"temp"`
ISO iso8601 `json:"iso8601"`
} `json:"date"`
MaxTemperature int `json:"max_temperature"`
AvgTemperature int `json:"temperature"`
MinTemperature int `json:"min_temperature"`
} `json:"summary"`
} }
type data struct { type data struct {
History struct { Observations []day `json:"observations"`
Days []day `json:"days"`
} `json:"history"`
} }
func main() { func main() {
@ -60,9 +33,7 @@ func main() {
to = time.Now() 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` 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`)
fmt.Println(url)
os.Exit(0)
resp, err := http.Get(url) resp, err := http.Get(url)
util.PanicOnErr(err) util.PanicOnErr(err)
@ -78,8 +49,8 @@ func main() {
util.PanicOnErr(err) util.PanicOnErr(err)
csvout := csv.NewWriter(out) csvout := csv.NewWriter(out)
for _, day := range d.History.Days { for _, day := range d.Observations {
csvout.Write([]string{day.Summary.Date.ISO.String(), fToCString(day.Summary.MinTemperature), fToCString(day.Summary.AvgTemperature), fToCString(day.Summary.MaxTemperature)}) csvout.Write([]string{strconv.Itoa(day.Date), fToCString(day.AvgTemperature)})
} }
csvout.Flush() csvout.Flush()
} }
@ -87,7 +58,7 @@ func main() {
func getNextDate() time.Time { func getNextDate() time.Time {
f, err := os.Open(`weather.csv`) f, err := os.Open(`weather.csv`)
if err != nil { 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) csvf := csv.NewReader(f)
@ -99,9 +70,12 @@ func getNextDate() time.Time {
} }
last = row 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) util.PanicOnErr(err)
t := time.Unix(u, 0)
return t.AddDate(0, 0, 1) return t.AddDate(0, 0, 1)
} }