This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
godoc.go
119 lines (103 loc) · 2.56 KB
/
godoc.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
package repository
import (
"bufio"
"context"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/go-kit/kit/metrics"
"github.com/pkg/errors"
)
var (
rePackage = regexp.MustCompile(`imports (\d+) packages \(graph\) and is imported by (\d+)`)
reUpdated = regexp.MustCompile(`Updated (.{10})\.`)
)
// GoDoc scrapes godoc.org
type GoDoc struct {
client *http.Client
apiCalls metrics.Histogram
}
// NewGoDoc initializes GoDoc with a http client
func NewGoDoc(apiCalls metrics.Histogram) (*GoDoc, error) {
gd := &GoDoc{
client: &http.Client{
Timeout: 5 * time.Second,
},
apiCalls: apiCalls.With("service", "godoc"),
}
// Initialize metric with a zero value
gd.apiCalls.Observe(0)
return gd, nil
}
// Get godoc's repository data from a urlPath
func (gd *GoDoc) Get(ctx context.Context, urlPath string) (*GoDocInfo, error) {
defer func(start time.Time) {
gd.apiCalls.Observe(time.Since(start).Seconds())
}(time.Now())
u, err := url.Parse("https://godoc.org")
if err != nil {
return nil, errors.Wrap(err, "failed to parse base url")
}
u.Path = urlPath
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
resp, err := gd.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to do the request")
}
if resp.StatusCode != http.StatusOK {
return nil, ErrNotFound
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return nil, errors.Wrap(err, "failed to open the godoc document from response")
}
s := doc.Find("#x-pkginfo p").First()
return parseInfo(strings.NewReader(s.Text()))
}
// GoDocInfo is the parsed meta information from a godoc.org page
type GoDocInfo struct {
Imports int
Importers int
Updated time.Time
}
func parseInfo(r io.Reader) (*GoDocInfo, error) {
p := &GoDocInfo{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Package") {
e := rePackage.FindStringSubmatch(line)
if len(e) == 3 {
imports, err := strconv.ParseInt(e[1], 10, 64)
if err != nil {
return p, err
}
importers, err := strconv.ParseInt(e[2], 10, 64)
if err != nil {
return p, err
}
p.Imports = int(imports)
p.Importers = int(importers)
}
}
if strings.HasPrefix(line, "Updated") {
e := reUpdated.FindStringSubmatch(line)
if len(e) == 2 {
t, err := time.Parse("2006-01-02", e[1])
if err != nil {
return p, err
}
p.Updated = t
}
}
}
return p, nil
}