-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
189 lines (171 loc) · 4.02 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
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
const (
ghAPI string = "https://api.github.com/repos/"
vwsPath string = "/traffic/views"
cnsPath string = "/traffic/clones"
rlsPath string = "/releases"
)
var (
user = os.Getenv("GH_USER")
token = os.Getenv("GH_TOKEN")
today = time.Now().UTC().Truncate(24 * time.Hour)
yesterday = today.AddDate(0, 0, -1)
)
func main() {
d, err := getData("spectolabs", "hoverfly-java", yesterday)
if err != nil {
log.Fatal(err)
}
fmt.Println(d)
}
type Repo struct {
Owner, Repo string
Date time.Time
Stars, Forks, Issues, Views, UniqueViews, Clones, UniqueClones, Downloads int
}
func buildURLs(owner, repo string) map[string]string {
u := make(map[string]string)
u["repo"] = ghAPI + owner + "/" + repo
u["views"] = u["repo"] + vwsPath
u["clones"] = u["repo"] + cnsPath
u["rels"] = u["repo"] + rlsPath
return u
}
func getData(owner, repo string, day time.Time) (*Repo, error) {
urls := buildURLs(owner, repo)
rd, err := getRepoData(urls["repo"])
if err != nil {
return nil, err
}
vc, err := getViewsData(urls["views"], day)
if err != nil {
return nil, err
}
cc, err := getClonesData(urls["clones"], day)
if err != nil {
return nil, err
}
dc, err := getDownloadCount(urls["rels"])
if err != nil {
return nil, err
}
r := new(Repo)
r.Owner = owner
r.Repo = repo
r.Date = today
r.Stars = rd.Stars
r.Forks = rd.Forks
r.Issues = rd.Issues
r.Views = vc.LatestCount
r.UniqueViews = vc.LatestUniques
r.Clones = cc.LatestCount
r.UniqueClones = cc.LatestUniques
r.Downloads = dc
return r, err
}
func request(url string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(user, token)
req.Header.Set("Accept-Encoding", "identity")
req.Header.Set("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
err := fmt.Errorf("%v returned %v", url, res.StatusCode)
return nil, err
}
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return bytes, err
}
type RepoData struct {
Stars int `json:"stargazers_count"`
Forks int `json:"forks_count"`
Issues int `json:"open_issues_count"`
}
func getRepoData(url string) (*RepoData, error) {
rd := new(RepoData)
b, err := request(url)
json.Unmarshal(b, &rd)
return rd, err
}
type ViewsData struct {
Views []struct {
Timestamp time.Time `json:"timestamp"`
Count int `json:"count"`
Uniques int `json:"uniques"`
} `json:"views"`
LatestCount int
LatestUniques int
}
func getViewsData(url string, day time.Time) (*ViewsData, error) {
vs := new(ViewsData)
b, err := request(url)
json.Unmarshal(b, &vs)
for _, d := range vs.Views {
if d.Timestamp == day {
vs.LatestCount = d.Count
vs.LatestUniques = d.Uniques
}
}
return vs, err
}
type ClonesData struct {
Clones []struct {
Timestamp time.Time `json:"timestamp"`
Count int `json:"count"`
Uniques int `json:"uniques"`
} `json:"clones"`
LatestCount int
LatestUniques int
}
func getClonesData(url string, day time.Time) (*ClonesData, error) {
cs := new(ClonesData)
b, err := request(url)
json.Unmarshal(b, &cs)
for _, d := range cs.Clones {
if d.Timestamp == day {
cs.LatestCount = d.Count
cs.LatestUniques = d.Uniques
}
}
return cs, err
}
type ReleasesData []struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
PublishedAt time.Time `json:"published_at"`
Assets []struct {
Name string `json:"name"`
DownloadCount int `json:"download_count"`
} `json:"assets"`
}
func getDownloadCount(url string) (int, error) {
var dls int
var rs ReleasesData
b, err := request(url)
json.Unmarshal(b, &rs)
for _, r := range rs {
for _, a := range r.Assets {
dls += a.DownloadCount
}
}
return dls, err
}