1
0
Fork 0

Compare commits

...

3 Commits

2 changed files with 39 additions and 55 deletions

View File

@ -3,79 +3,68 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"os" "os"
) )
func main() { const UPSTREAM_URL = "https://keyserver.freifunk-franken.de/v2/"
func run() error {
var root []root var root []root
err := os.Mkdir("hoods", 0777) err := os.MkdirAll("hoods", 0777)
if err != nil { 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 { 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 { if err != nil {
log.Panic(err) return err
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Panic(err)
}
err = json.Unmarshal(body, &root)
if err != nil {
log.Panic(err)
} }
for _, h := range root { 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 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 { if err != nil {
log.Panic(err) return 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)
} }
if len(h.Polygons) == 0 { if len(h.Polygons) == 0 {
hood.Location = []HoodCoordinate{{Lat: h.Lat, Long: h.Long}} hood.Location = []GeoCoordinate{{Lat: h.Lat, Long: h.Long}}
} else { } else {
hood.Location = []HoodCoordinate{} hood.Location = h.Polygons[0]
for _, p := range h.Polygons[0] {
hood.Location = append(hood.Location, HoodCoordinate{Lat: p.Lat, Long: p.Long})
}
} }
b, err := json.MarshalIndent(hood, "", " ") b, err := json.MarshalIndent(hood, "", " ")
if err != nil { if err != nil {
log.Panic(err) return err
} }
err = os.WriteFile("hoods/"+hood.HoodInfo.Name+".json", b, 0666) err = os.WriteFile("hoods/"+hood.HoodInfo.Name+".json", b, 0666)
if err != nil { 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)
} }
} }

View File

@ -1,16 +1,16 @@
package main package main
type root struct { type root struct {
Id uint `json:"id"` Id uint `json:"id"`
Active uint `json:"active"` Active uint `json:"active"`
Name string `json:"name"` Name string `json:"name"`
Lat float64 `json:"lat"` Lat float64 `json:"lat"`
Long float64 `json:"lon"` Long float64 `json:"lon"`
Polygons [][]InputCoordinate `json:"polygons"` Polygons [][]GeoCoordinate `json:"polygons"`
Hood Hood Hood Hood
} }
type InputCoordinate struct { type GeoCoordinate struct {
Lat float64 `json:"lat"` Lat float64 `json:"lat"`
Long float64 `json:"lon"` Long float64 `json:"lon"`
} }
@ -24,7 +24,7 @@ type Hood struct {
// one coordinate: voronoi // one coordinate: voronoi
// two coordinates: invalid // two coordinates: invalid
// three or more coordinates: polygon // three or more coordinates: polygon
Location []HoodCoordinate `json:"location,omitempty"` Location []GeoCoordinate `json:"location,omitempty"`
} }
type HoodNetwork struct { type HoodNetwork struct {
@ -58,8 +58,3 @@ type HoodInfo struct {
NtpIp string `json:"ntp_ip"` NtpIp string `json:"ntp_ip"`
Timestamp uint64 `json:"timestamp"` Timestamp uint64 `json:"timestamp"`
} }
type HoodCoordinate struct {
Lat float64 `json:"lat"`
Long float64 `json:"lon"`
}