-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
172 lines (147 loc) · 3.69 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
package main
import (
"flag"
"fmt"
"math"
"net/http"
"sort"
"strings"
"sync"
"github.com/mingrammer/cfmt"
"golang.org/x/net/html"
)
const (
perPage = 10
spClass = "SearchSnippet"
hdClass = "SearchSnippet-header"
snClass = "SearchSnippet-synopsis"
ilClass = "SearchSnippet-infoLabel"
)
type pkg struct {
repo string
desc string
version string
pubDate string
importCnt string
license string
}
type page struct {
seq int
pkgs []*pkg
}
type pages []*page
func (p pages) Len() int { return len(p) }
func (p pages) Less(i, j int) bool { return p[i].seq < p[j].seq }
func (p pages) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func main() {
count := flag.Int("n", 10, "the number of packages to search.")
exact := flag.Bool("e", false, "search for an exact match.")
useOR := flag.Bool("o", false, "combine searches. if true, query will be like 'yaml OR json'.")
flag.Parse()
// Build a query.
glue := "+"
if *useOR { // Put OR between each search query.
glue = "+OR+"
}
query := strings.Join(flag.Args(), glue)
if *exact { // Put a word or phrase inside quotes.
query = "\"" + query + "\""
}
pageN := int(math.Ceil(float64(*count) / perPage))
// Search the packages concurrently.
pc := make(chan *page, pageN)
wg := new(sync.WaitGroup)
for n := 1; n < pageN+1; n++ {
wg.Add(1)
go search(query, n, pc, wg)
}
go func() {
wg.Wait()
close(pc)
}()
// Order by sequence.
ps := make(pages, 0)
for p := range pc {
ps = append(ps, p)
}
sort.Sort(ps)
// Print all found packages.
for i, p := range ps {
for j, pkg := range p.pkgs {
if i*perPage+j >= *count {
return
}
prettyPrint(pkg)
}
}
}
func search(query string, seq int, pc chan<- *page, wg *sync.WaitGroup) {
defer wg.Done()
baseURL := "https://pkg.go.dev/search"
fullURL := fmt.Sprintf("%s?q=%s&page=%d", baseURL, query, seq)
resp, err := http.Get(fullURL)
if err != nil {
panic(err)
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
panic(err)
}
pkgs := make([]*pkg, 0)
spNodes := find(doc, condHasClass(spClass))
for _, spNode := range spNodes {
hdNodes := find(spNode, condHasClass(hdClass))
pkgRepo := find(hdNodes[0], condValidTxt())[0]
pkgDesc := ""
snNodes := find(spNode, condHasClass(snClass))
txtNode := find(snNodes[0], condValidTxt())
if len(txtNode) > 0 {
pkgDesc = txtNode[0].Data
}
ilNodes := find(spNode, condHasClass(ilClass))
pkgMeta := find(ilNodes[0], condValidTxt())
pkgs = append(pkgs, &pkg{
repo: strings.TrimSpace(pkgRepo.Data),
desc: strings.TrimSpace(pkgDesc),
version: strings.TrimSpace(pkgMeta[1].Data),
pubDate: strings.TrimSpace(pkgMeta[3].Data),
importCnt: strings.TrimSpace(pkgMeta[5].Data),
license: strings.TrimSpace(pkgMeta[7].Data),
})
}
pc <- &page{seq, pkgs}
}
func find(node *html.Node, by cond) []*html.Node {
nodes := make([]*html.Node, 0)
for c := node.FirstChild; c != nil; c = c.NextSibling {
if by(c) {
nodes = append(nodes, c)
}
nodes = append(nodes, find(c, by)...)
}
return nodes
}
type cond func(*html.Node) bool
func condHasClass(class string) cond {
return func(node *html.Node) bool {
for _, attr := range node.Attr {
if attr.Key == "class" && attr.Val == class {
return true
}
}
return false
}
}
func condValidTxt() cond {
return func(node *html.Node) bool {
return node.Type == html.TextNode && strings.TrimSpace(node.Data) != "" && node.Data != "|"
}
}
func prettyPrint(p *pkg) {
fmt.Printf("%s (%s)\n", cfmt.Ssuccess(p.repo), cfmt.Sinfo(p.version))
if p.desc != "" {
fmt.Printf("├ %s\n", p.desc)
}
fmt.Printf("└ Published: %s | Imported by: %s | License: %s\n\n", p.pubDate, p.importCnt, p.license)
}