forked from mmp/aisscraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcifp.go
93 lines (76 loc) · 1.89 KB
/
cifp.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// cifp.go
// Copyright(c) Matt Pharr 2024
// SPDX: MIT-only
package main
import (
"archive/zip"
"bytes"
"io"
"log"
"golang.org/x/net/html"
)
func DownloadCIFP() []byte {
zipURL := GetCIFPZipURL()
if zipURL == "" {
log.Fatal("Unable to find URL for CIFP ZIP file")
}
log.Printf("CIFP is at %s", zipURL)
cifpZipBytes := FetchURL(zipURL)
cifpZip, err := zip.NewReader(bytes.NewReader(cifpZipBytes), int64(len(cifpZipBytes)))
if err != nil {
log.Fatal(err)
}
const cifpFilename = "FAACIFP18"
var cifpFile *zip.File
for _, f := range cifpZip.File {
log.Printf("zip entry: %s (%d bytes)\n", f.Name, f.UncompressedSize64)
if f.Name == cifpFilename {
cifpFile = f
}
}
if cifpFile == nil {
log.Fatalf("Didn't find %q in CIFP zip file", cifpFilename)
}
r, err := cifpFile.Open()
if err != nil {
log.Fatal(err)
}
defer r.Close()
b, err := io.ReadAll(r)
if err != nil {
log.Fatal(err)
}
log.Printf("CIFP is %d bytes after decompression", len(b))
return b
}
// Scrape the FAA CIFP webpage to get the URL to the zip file with the latest CIFP.
func GetCIFPZipURL() string {
url := "https://www.faa.gov/air_traffic/flight_info/aeronav/digital_products/cifp/download/"
h := FetchURL(url)
doc, err := html.Parse(bytes.NewReader(h))
if err != nil {
log.Fatal(err)
}
zipURL := ""
var parse func(*html.Node)
parse = func(node *html.Node) {
if node.Type == html.ElementNode && node.Data == "cfoutput" {
for child := node.FirstChild; child != nil; child = child.NextSibling {
if child.Type == html.ElementNode && child.Data == "a" {
for _, attr := range child.Attr {
if attr.Key == "href" && zipURL == "" {
zipURL = attr.Val
}
}
}
}
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
parse(child)
}
}
for child := doc.FirstChild; child != nil; child = child.NextSibling {
parse(child)
}
return zipURL
}