diff --git a/cmd/fetch/fetch.go b/cmd/fetch/fetch.go index 98f7d66..3a80bcc 100644 --- a/cmd/fetch/fetch.go +++ b/cmd/fetch/fetch.go @@ -3,79 +3,68 @@ package main import ( "encoding/json" "fmt" - "io" "log" "net/http" "os" ) -func main() { +const UPSTREAM_URL = "https://keyserver.freifunk-franken.de/v2/" + +func run() error { var root []root - err := os.Mkdir("hoods", 0777) + err := os.MkdirAll("hoods", 0777) if err != nil { - log.Panic(err) + return err } - req, err := http.NewRequest(http.MethodGet, "https://keyserver.freifunk-franken.de/v2/hoods.php", nil) + res, err := http.Get(UPSTREAM_URL + "hoods.php") if err != nil { - log.Panic(err) + return err } + defer res.Body.Close() - res, err := http.DefaultClient.Do(req) + err = json.NewDecoder(res.Body).Decode(&root) if err != nil { - log.Panic(err) - } - - body, err := io.ReadAll(res.Body) - if err != nil { - log.Panic(err) - } - - err = json.Unmarshal(body, &root) - if err != nil { - log.Panic(err) + return err } for _, h := range root { + res, err := http.Get(fmt.Sprintf("%s/?hoodid=%d", UPSTREAM_URL, h.Id)) + if err != nil { + return err + } + defer res.Body.Close() + var hood Hood - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://keyserver.freifunk-franken.de/v2/?hoodid=%d", h.Id), nil) + err = json.NewDecoder(res.Body).Decode(&hood) if err != nil { - log.Panic(err) - } - - res, err := http.DefaultClient.Do(req) - if err != nil { - log.Panic(err) - } - - body, err := io.ReadAll(res.Body) - if err != nil { - log.Panic(err) - } - - err = json.Unmarshal(body, &hood) - if err != nil { - log.Panic(err) + return err } if len(h.Polygons) == 0 { - hood.Location = []HoodCoordinate{{Lat: h.Lat, Long: h.Long}} + hood.Location = []GeoCoordinate{{Lat: h.Lat, Long: h.Long}} } else { - hood.Location = []HoodCoordinate{} - for _, p := range h.Polygons[0] { - hood.Location = append(hood.Location, HoodCoordinate{Lat: p.Lat, Long: p.Long}) - } + hood.Location = h.Polygons[0] } b, err := json.MarshalIndent(hood, "", " ") if err != nil { - log.Panic(err) + return err } err = os.WriteFile("hoods/"+hood.HoodInfo.Name+".json", b, 0666) if err != nil { - log.Panic(err) + return err } + + log.Printf("%s\n", hood.HoodInfo.Name) + } + return nil +} + +func main() { + if err := run(); err != nil { + log.Fatal(err) } } diff --git a/cmd/fetch/types.go b/cmd/fetch/types.go index e8445af..96a69ec 100644 --- a/cmd/fetch/types.go +++ b/cmd/fetch/types.go @@ -1,16 +1,16 @@ package main type root struct { - Id uint `json:"id"` - Active uint `json:"active"` - Name string `json:"name"` - Lat float64 `json:"lat"` - Long float64 `json:"lon"` - Polygons [][]InputCoordinate `json:"polygons"` + Id uint `json:"id"` + Active uint `json:"active"` + Name string `json:"name"` + Lat float64 `json:"lat"` + Long float64 `json:"lon"` + Polygons [][]GeoCoordinate `json:"polygons"` Hood Hood } -type InputCoordinate struct { +type GeoCoordinate struct { Lat float64 `json:"lat"` Long float64 `json:"lon"` } @@ -24,7 +24,7 @@ type Hood struct { // one coordinate: voronoi // two coordinates: invalid // three or more coordinates: polygon - Location []HoodCoordinate `json:"location,omitempty"` + Location []GeoCoordinate `json:"location,omitempty"` } type HoodNetwork struct { @@ -58,8 +58,3 @@ type HoodInfo struct { NtpIp string `json:"ntp_ip"` Timestamp uint64 `json:"timestamp"` } - -type HoodCoordinate struct { - Lat float64 `json:"lat"` - Long float64 `json:"lon"` -}