-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
57 lines (51 loc) · 1.29 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
// Copyright (c) The microformats project authors.
// SPDX-License-Identifier: MIT
// 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"
"strings"
"github.com/PuerkitoBio/goquery"
"willnorris.com/go/microformats"
)
func main() {
u, _ := url.Parse(strings.TrimSpace(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("", " ")
if err := enc.Encode(data); err != nil {
log.Fatal(err)
}
}