/
main.go
51 lines (46 loc) · 1.14 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
// The gomf tool is a command line tool which parses microformats from the
// specified URL. If selector is provided, the first element that matches the
// selector will be used as the root node for parsing.
//
// Usage: gomf <URL> [optional selector]
//
// For example, to parse all microformats from https://microformats.io inside
// the <main> element, call:
//
// gomf "https://microformats.io" "main"
package main
import (
"encoding/json"
"log"
"net/http"
"net/url"
"os"
"github.com/PuerkitoBio/goquery"
"willnorris.com/go/microformats"
)
func main() {
u, _ := url.Parse(os.Args[1])
resp, err := http.Get(u.String())
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var data *microformats.Data
if len(os.Args) > 2 {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
s := doc.Find(os.Args[2])
if s.Length() == 0 {
log.Fatal("selector did not match any elements")
}
data = microformats.ParseNode(s.Get(0), u)
} else {
data = microformats.Parse(resp.Body, u)
}
enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
enc.Encode(data)
}