-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
177 lines (164 loc) · 4.04 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/schollz/pluck/pluck"
"github.com/urfave/cli"
)
var version string
func main() {
app := cli.NewApp()
app.Version = version
app.Compiled = time.Now()
app.Name = "pluck"
app.Usage = ""
app.UsageText = `
1) Pluck all URLs from a website
$ pluck -a '<' -a 'href' -a '"' -d '"' -l -1 -u https://nytimes.com
2) Pluck title from a HTML file
$ pluck -a '<title>' -d '<' -f test.html
3) Pluck using a configuration file.
$ # Example config file
$ cat config.toml
[[pluck]]
activators = ["<title>"]
deactivator = "</title>"
name = "title"
[[pluck]]
activators = ["<label","Ingredient",">"]
deactivator = "<"
limit = -1
name = "ingredients"
$ pluck -c config.toml -u https://goo.gl/DHmqmv
4) Get headlines from news.google.com
$ pluck -a 'role="heading"' -a '>' -d '<' -t -s -u 'https://news.google.com/news/?ned=us&hl=en'
5) Pluck items from a block
$ pluck -a 'Section 2' -a '<a' -a 'href' -a '"' -d '"' -p 1 -finisher "Section 3" -u https://cowyo.com/test38/raw
`
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "file,f",
Value: "",
Usage: "file to pluck",
},
cli.StringFlag{
Name: "url,u",
Value: "",
Usage: "url to pluck",
},
cli.StringFlag{
Name: "config,c",
Value: "",
Usage: "specify toml config file",
},
cli.StringSliceFlag{
Name: "activator,a",
Usage: "text to find in order to start capture (can specify multiple times)",
},
cli.StringFlag{
Name: "deactivator,d",
Value: "",
Usage: "text to find to restart capturing",
},
cli.IntFlag{
Name: "permanent,p",
Value: 0,
Usage: "number of activators that stay activated (from left to right)",
},
cli.StringFlag{
Name: "finisher",
Value: "",
Usage: "text to find to stop capturing completely",
},
cli.IntFlag{
Name: "limit,l",
Value: -1,
Usage: "maximum number of items to capture",
},
cli.BoolFlag{
Name: "sanitize,s",
Usage: "sanitize output (html tag stripping and hex conversion)",
},
cli.BoolFlag{
Name: "text, t",
Usage: "output as plain text, not JSON",
},
cli.BoolFlag{
Name: "verbose",
Usage: "turn on verbose mode",
},
cli.StringFlag{
Name: "output,o",
Value: "",
Usage: "direct output to file",
},
}
app.Action = func(c *cli.Context) (err error) {
if c.GlobalString("file") == "" && c.GlobalString("url") == "" {
fmt.Println("Must specify file or url. For example -u https://nytimes.com.\nSee help and usage with -h")
return nil
}
p, _ := pluck.New()
if c.GlobalBool("verbose") {
p.Verbose(true)
}
if len(c.GlobalString("config")) > 0 {
p.Load(c.GlobalString("config"))
} else {
if len(c.GlobalStringSlice("activator")) == 0 {
fmt.Println("Must specify at least one activator. For example -a 'start'.\nSee help and usage with -h")
return nil
}
if len(c.GlobalString("deactivator")) == 0 {
fmt.Println("Must specify at deactivator. For example -d 'end'.\nSee help and usage with -h")
return nil
}
p.Add(pluck.Config{
Activators: c.GlobalStringSlice("activator"),
Deactivator: c.GlobalString("deactivator"),
Limit: c.GlobalInt("limit"),
Sanitize: c.GlobalBool("sanitize"),
Finisher: c.GlobalString("finisher"),
Permanent: c.GlobalInt("permanent"),
})
}
if len(c.GlobalString("file")) > 0 {
err = p.PluckFile(c.GlobalString("file"))
} else {
err = p.PluckURL(c.GlobalString("url"))
}
if err != nil {
return err
}
var result string
if c.GlobalBool("text") {
results, ok := p.Result()["0"].([]string)
if !ok {
results2, ok2 := p.Result()["0"].(string)
if !ok2 {
fmt.Println("Error?")
os.Exit(-1)
} else {
result = results2
}
} else {
result = strings.Join(results, "\n\n")
}
} else {
result = p.ResultJSON(true)
}
if c.GlobalString("output") != "" {
return ioutil.WriteFile(c.GlobalString("output"), []byte(result), 0644)
} else {
fmt.Println(result)
}
return nil
}
err := app.Run(os.Args)
if err != nil {
fmt.Print(err)
}
}