gfsmos/main.go

60 lines
1.1 KiB
Go
Raw Normal View History

2020-02-25 16:14:46 +00:00
package main
import (
"fmt"
2020-02-25 21:29:43 +00:00
"github.com/antchfx/xpath"
"github.com/antchfx/xquery/html"
"io/ioutil"
2020-02-25 16:14:46 +00:00
"log"
2020-02-25 21:29:43 +00:00
"net/http"
2020-02-25 16:14:46 +00:00
"os"
2020-02-25 21:29:43 +00:00
"strings"
2020-02-25 16:14:46 +00:00
cli "github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
2020-02-25 21:29:43 +00:00
Name: "GFS MOS",
Usage: "Parse NOAA's GFS MOS for a given airport",
2020-02-25 16:14:46 +00:00
Action: func(c *cli.Context) error {
2020-02-25 21:29:43 +00:00
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)
2020-02-25 16:14:46 +00:00
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
2020-02-25 21:29:43 +00:00
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)
}