-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkgproxy.go
More file actions
188 lines (166 loc) · 5.18 KB
/
pkgproxy.go
File metadata and controls
188 lines (166 loc) · 5.18 KB
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
178
179
180
181
182
183
184
185
186
187
188
package pkgproxy
import (
"encoding/json"
"fmt"
"strings"
"github.com/gocolly/colly"
)
// Package represents information about
// Go package received from the pkg.go.dev.
type Package struct {
Name string `json:"name"`
Repository string `json:"repository"`
Version string `json:"version"`
PublishedDate string `json:"publishedDate"`
License string `json:"license"`
Imports string `json:"imports"`
ImportedBy string `json:"importedBy"`
ValidGoMod string `json:"validGomod"`
RedistributableLicense string `json:"redistributableLicense"`
TaggedVersion string `json:"taggedVersion"`
StableVersion string `json:"stableVersion"`
}
// PkgCollector represents the web scraper (collector).
type PkgCollector struct {
BaseURL string
Collector *colly.Collector
}
// NewPkCollector configures and returns defualt
// PkgCollector ready to interact with pkg.go.dev.
//
// The collector creates a default cache dir `.pkg_cache`.
func NewPkgCollector() *PkgCollector {
c := colly.NewCollector(
colly.AllowedDomains("pkg.go.dev", "www.pkg.go.dev"),
colly.CacheDir("./.pkg_cache"),
colly.UserAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"),
)
return &PkgCollector{
Collector: c,
BaseURL: "https://pkg.go.dev",
}
}
// Get takes the Go package name, collects information
// and returns Package type populated with data.
func (p *PkgCollector) Get(pkgName string) Package {
// Repository URL
var repo string
p.Collector.OnHTML(".UnitMeta-repo", func(e *colly.HTMLElement) {
repo = e.ChildText("a")
})
// Version
var version string
p.Collector.OnHTML(".go-Main-headerDetails", func(e *colly.HTMLElement) {
version = e.ChildAttr("a", "aria-label")
version = sanitizeVersion(version)
})
// Published Date
var published string
p.Collector.OnHTML(".go-Main-headerDetailItem[data-test-id='UnitHeader-commitTime']", func(e *colly.HTMLElement) {
published = sanitizeDate(e.Text)
})
// License
var license string
p.Collector.OnHTML(".go-Main-headerDetailItem[data-test-id='UnitHeader-licenses']", func(e *colly.HTMLElement) {
license = strings.TrimSpace(e.ChildText("a"))
})
// Imports
var imports string
p.Collector.OnHTML(".go-Main-headerDetailItem[data-test-id='UnitHeader-imports']", func(e *colly.HTMLElement) {
imports = sanitizeImports(strings.TrimSpace(e.ChildText("a")))
})
// ImportedBy
var importedBy string
p.Collector.OnHTML(".go-Main-headerDetailItem[data-test-id='UnitHeader-importedby']", func(e *colly.HTMLElement) {
importedBy = sanitizeImports(strings.TrimSpace(e.ChildText("a")))
})
// Package details
var (
details []string
validGoMod string
redistributableLic string
taggedVersion string
stableVersion string
)
p.Collector.OnHTML(".UnitMeta-details", func(e *colly.HTMLElement) {
var v string
e.ForEach("li", func(i int, h *colly.HTMLElement) {
v = fmt.Sprintf("%s", h.ChildAttr("details>summary>img", "alt"))
details = append(details, strings.TrimSpace(v))
})
if len(details) < 4 {
return
}
validGoMod = sanitizeTrueFalse(details[0])
redistributableLic = sanitizeTrueFalse(details[1])
taggedVersion = sanitizeTrueFalse(details[2])
stableVersion = sanitizeTrueFalse(details[3])
})
p.Collector.Visit(p.BaseURL + "/" + pkgName)
return Package{
Name: pkgName,
Repository: repo,
Version: version,
PublishedDate: published,
License: license,
Imports: imports,
ImportedBy: importedBy,
ValidGoMod: validGoMod,
RedistributableLicense: redistributableLic,
TaggedVersion: taggedVersion,
StableVersion: stableVersion,
}
}
func sanitizeTrueFalse(v string) string {
if v == "unchecked" {
return "No"
}
if v == "checked" {
return "Yes"
}
return "Undetected"
}
func sanitizeDate(date string) string {
chunks := strings.Split(strings.TrimSpace(date), ":")
if len(chunks) < 2 {
return "Undetected"
}
return strings.TrimSpace(chunks[1])
}
func sanitizeVersion(version string) string {
chunks := strings.Split(strings.TrimSpace(version), ":")
if len(chunks) < 2 {
return "Undetected"
}
v := strings.TrimSpace(chunks[1])
if strings.Contains(v, "-...-") {
return strings.TrimSpace(strings.Split(v, "-")[0])
}
return v
}
func sanitizeImports(imports string) string {
chunks := strings.Split(imports, ":")
if len(chunks) < 2 {
return "Undetected"
}
return strings.TrimSpace(chunks[1])
}
// Get takes a string representing Go pkg name
// and returns the Package info.
// It uses default collector configured to
// interact with pkg.go.dev.
func Get(name string) Package {
return NewPkgCollector().Get(name)
}
// GetJSON takes a name representing the Go package name
// and returns JSON representation of the package info or an error.
//
// It uses default PkgCollector configured to interact with pkg.go.dev.
func GetJSON(name string) (string, error) {
p := Get(name)
data, err := json.Marshal(p)
if err != nil {
return "", fmt.Errorf("marshaling data: %w", err)
}
return string(data), nil
}