-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstargazer.go
165 lines (133 loc) · 3.13 KB
/
stargazer.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
package gomodstarguard
import (
"io"
"net/http"
"strconv"
"strings"
"github.com/pkg/errors"
"golang.org/x/net/html"
)
type GithubQueryError struct {
Err error
RepositoryURL string
}
func (e GithubQueryError) Error() string {
return "Error on Querying Github for " + e.RepositoryURL + " - " + e.Err.Error()
}
type StarParserError struct {
Err error
RepositoryURL string
}
func (e StarParserError) Error() string {
return "Error on Parsing Stars for " + e.RepositoryURL + " - " + e.Err.Error()
}
type Stargazer struct {
Config *Configuration
}
func NewStargazer(config *Configuration) (*Stargazer, error) {
return &Stargazer{
Config: config,
}, nil
}
func (s *Stargazer) GetStars(repositoryURL string) (int, error) {
content, ghErr := s.loadHTML(repositoryURL)
if ghErr != nil {
return 0, &GithubQueryError{
Err: ghErr,
RepositoryURL: repositoryURL,
}
}
doc, err := html.Parse(strings.NewReader(content))
if err != nil {
return 0, &GithubQueryError{
Err: err,
RepositoryURL: repositoryURL,
}
}
starsElement := s.getElementByID(doc, "repo-stars-counter-star")
if starsElement == nil {
return 0, &StarParserError{
Err: errors.New("could not find stars element in fetched HTML"),
RepositoryURL: repositoryURL,
}
}
stars, parseErr := s.parseStarNumber(starsElement.FirstChild.Data)
if parseErr != nil {
return 0, &StarParserError{
Err: parseErr,
RepositoryURL: repositoryURL,
}
}
return stars, nil
}
func (s *Stargazer) parseStarNumber(stars string) (int, error) {
var multiplier float64 = 1
if stars[len(stars)-1:] == "k" {
multiplier = 1000
}
starNumberStr := strings.ReplaceAll(stars, "k", "")
starsFloat, err := strconv.ParseFloat(starNumberStr, 32)
if err != nil {
return 0, &StarParserError{
Err: err,
RepositoryURL: "",
}
}
return int(starsFloat * multiplier), nil
}
func (s *Stargazer) loadHTML(url string) (string, *GithubQueryError) {
res, err := http.Get(url) //nolint:gosec // TODO: add context
if err != nil {
return "", &GithubQueryError{
Err: err,
RepositoryURL: url,
}
}
content, err := io.ReadAll(res.Body)
if err != nil {
return "", &GithubQueryError{
Err: err,
RepositoryURL: url,
}
}
err = res.Body.Close()
if err != nil {
return "", &GithubQueryError{
Err: err,
RepositoryURL: url,
}
}
return string(content), nil
}
func (s *Stargazer) getAttribute(n *html.Node, key string) (string, bool) {
for _, attr := range n.Attr {
if attr.Key == key {
return attr.Val, true
}
}
return "", false
}
func (s *Stargazer) checkID(n *html.Node, id string) bool {
if n.Type == html.ElementNode {
s, ok := s.getAttribute(n, "id")
if ok && s == id {
return true
}
}
return false
}
func (s *Stargazer) traverse(n *html.Node, id string) *html.Node {
if s.checkID(n, id) {
return n
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
result := s.traverse(c, id)
if result != nil {
return result
}
}
return nil
}
func (s *Stargazer) getElementByID(n *html.Node, id string) *html.Node {
return s.traverse(n, id)
}