forked from mmp/aisscraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (61 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// main.go
// Copyright(c) Matt Pharr 2024
// SPDX: MIT-only
package main
import (
"encoding/json"
"io"
"log"
"maps"
"net/http"
"os"
"slices"
"golang.org/x/exp/constraints"
)
func main() {
ScrapeCallsigns()
cifp := DownloadCIFP()
airports, navaids, fixes, airways := ParseARINC424(cifp)
log.Printf("Got %d airports, %d navaids, %d fixes, %d airways from CIFP", len(airports), len(navaids), len(fixes), len(airways))
WriteJSON(airports, "airports.json")
WriteJSON(navaids, "navaids.json")
WriteJSON(fixes, "fixes.json")
WriteJSON(airways, "airways.json")
}
func WriteJSON[T any](data T, filename string) {
f, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
enc := json.NewEncoder(f)
if err := enc.Encode(data); err != nil {
log.Fatal(err)
}
log.Printf("Wrote %q", filename)
}
func FetchURL(url string) []byte {
log.Printf("Fetching %s", url)
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
var text []byte
if text, err = io.ReadAll(response.Body); err != nil {
log.Fatal(err)
}
log.Printf("Received %d bytes", len(text))
return text
}
func Select[T any](sel bool, a, b T) T {
if sel {
return a
} else {
return b
}
}
// SortedMapKeys returns the keys of the given map, sorted from low to high.
func SortedMapKeys[K constraints.Ordered, V any](m map[K]V) []K {
return slices.Sorted(maps.Keys(m))
}