Skip to content

Instantly share code, notes, and snippets.

@aeneasr
Last active March 21, 2020 19:01
Show Gist options
  • Save aeneasr/a4c8949432d92c01ded50970d9cfcb08 to your computer and use it in GitHub Desktop.
Save aeneasr/a4c8949432d92c01ded50970d9cfcb08 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"time"
)
func check(err error) {
if err != nil {
fmt.Printf("%+v", err)
os.Exit(1)
}
}
type payload struct {
Features []struct {
Attributes struct {
AnzahlFall int64 `json:"AnzahlFall"`
Meldedatum int64 `json:"Meldedatum"`
} `json:"attributes"`
} `json:"features"`
}
type im struct {
ts time.Time
number int64
}
const (
All = "1=1"
Bayern = "Bundesland='Bayern'"
BaWü = "Bundesland='Baden-Württemberg'"
Hessen = "Bundesland='Hessen'"
Berlin = "Bundesland='Berlin'"
)
func main() {
var results = make(map[time.Time]int64)
var offset int64
var where = All
p := fetch(offset, where)
for len(p.Features) >= 2000 {
offset = offset + 2000
for _, f := range p.Features {
ts := time.Unix(f.Attributes.Meldedatum/1000, 0).Round(time.Hour * 24)
if _, found := results[ts]; found {
results[ts] += f.Attributes.AnzahlFall
} else {
results[ts] = f.Attributes.AnzahlFall
}
}
p = fetch(offset, where)
}
var interim []im
for ts, count := range results {
interim = append(interim, im{ts: ts, number: count})
}
sort.Slice(interim, func(i, j int) bool {
return interim[i].ts.Before(interim[j].ts)
})
fmt.Printf("Date, New Cases, Total Cases, Change, Doubles Every\n")
var total int64
for _, item := range interim {
change := (float64(total-total+item.number) / float64(total)) * 100.0
total += item.number
fmt.Printf("%s, %d, %d, %.4f%%, %.2f days\n", item.ts.Format("02/01/2006"), item.number, total, change, 100/change)
}
}
func fetch(offset int64, where string) *payload {
u, err := url.Parse("https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_COVID19/FeatureServer/0/query?f=json&where=1%%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=ObjectId%%2CAnzahlFall%%2CMeldedatum&orderByFields=Meldedatum%%20asc&cacheHint=true")
check(err)
q := u.Query()
q.Set("resultOffset", strconv.FormatInt(offset, 10))
q.Set("resultRecordCount", strconv.FormatInt(offset+2000, 10))
q.Set("where", where)
u.RawQuery = q.Encode()
res, err := http.DefaultClient.Get(fmt.Sprintf("https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_COVID19/FeatureServer/0/query?f=json&where=1%%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=ObjectId%%2CAnzahlFall%%2CMeldedatum&orderByFields=Meldedatum%%20asc&resultOffset=%d&resultRecordCount=%d&cacheHint=true", offset, offset+2000))
check(err)
defer res.Body.Close()
if res.StatusCode != 200 {
check(errors.New("expected status code 200"))
}
var p payload
check(json.NewDecoder(res.Body).Decode(&p))
return &p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment