60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/antchfx/xpath"
|
|
"github.com/antchfx/xquery/html"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
cli "github.com/urfave/cli/v2"
|
|
)
|
|
|
|
func main() {
|
|
app := &cli.App{
|
|
Name: "GFS MOS",
|
|
Usage: "Parse NOAA's GFS MOS for a given airport",
|
|
Action: func(c *cli.Context) error {
|
|
airport := strings.ToUpper(c.Args().First())
|
|
if airport == "" {
|
|
return cli.NewExitError("please pass the airport's ICAO as an argument", 1)
|
|
}
|
|
pre := getDataFromPre(airport)
|
|
fmt.Println(pre)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
err := app.Run(os.Args)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func getDataFromPre(airport string) string {
|
|
url := fmt.Sprintf("https://www.nws.noaa.gov/cgi-bin/mos/getmav.pl?sta=%s", airport)
|
|
|
|
response, err := http.Get(url)
|
|
defer response.Body.Close()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
bytes, err := ioutil.ReadAll(response.Body)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
doc, err := htmlquery.Parse(string(bytes))
|
|
if err != nil{
|
|
panic(err)
|
|
}
|
|
for _, n := range htmlquery.Find(doc, "//a/@href") {
|
|
fmt.Printf("%s \n", htmlquery.SelectAttr(n, "href")) // output href
|
|
}
|
|
|
|
return string(bytes)
|
|
}
|