-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (167 loc) · 3.94 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/vicanso/go-axios"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
apiRepoInfo = "https://api.github.com/repos/%s"
)
var repoReg = regexp.MustCompile(`\- \[[\s\S]+\]\(\S+\)`)
var githubRepoReg = regexp.MustCompile(`\(https://github.com/(\S+)\)`)
var ins = axios.NewInstance(&axios.InstanceConfig{
Timeout: time.Minute,
})
var defaultLogger *zap.Logger
var startCounts = map[string]int{}
// github api的认证参数
var clientToken *string
type Repo struct {
content string
star int
}
type Repos []*Repo
func (rs Repos) Len() int {
return len(rs)
}
func (rs Repos) Less(i, j int) bool {
return rs[i].star < rs[j].star
}
func (rs Repos) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
}
type GithubRepoInfo struct {
StargazersCount int `json:"stargazers_count"`
}
func init() {
c := zap.NewProductionConfig()
c.DisableCaller = true
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
// 只针对panic 以上的日志增加stack trace
l, err := c.Build(zap.AddStacktrace(zap.DPanicLevel))
if err != nil {
panic(err)
}
defaultLogger = l
}
// 获取star的数量
func getStarCount(name string) (count int, err error) {
count, ok := startCounts[name]
if ok {
return count, nil
}
url := fmt.Sprintf(apiRepoInfo, name)
// 如果出错则认为0
headers := make(http.Header)
headers.Add("Authorization", fmt.Sprintf("token %s", *clientToken))
resp, err := ins.Request(&axios.Config{
URL: url,
Headers: headers,
})
if err != nil {
return
}
info := &GithubRepoInfo{}
err = resp.JSON(info)
if err != nil {
return
}
count = info.StargazersCount
startCounts[name] = count
return
}
// 按star对同一分类的repo重排
func arrangeByStar(lines []string, min int) (result []string, err error) {
result = make([]string, 0, len(lines))
newCatStart := false
var repos Repos
for _, line := range lines {
if repoReg.MatchString(line) {
// 新的分类开始
if !newCatStart {
newCatStart = true
repos = make(Repos, 0)
}
subs := githubRepoReg.FindAllStringSubmatch(line, -1)
repo := &Repo{
content: line,
}
if len(subs) > 0 && len(subs[0]) == 2 {
name := subs[0][1]
nameSplits := strings.Split(name, "/")
if len(nameSplits) > 2 {
name = strings.Join(nameSplits[0:2], "/")
}
count, e := getStarCount(name)
if e != nil {
defaultLogger.Error("get repo info fail",
zap.String("repo", name),
zap.Error(e),
)
}
repo.star = count
if repo.star != 0 {
stars := strconv.Itoa(repo.star)
if repo.star >= 1000 {
stars = fmt.Sprintf("%.1fK", float32(repo.star)/1000)
}
repo.content += fmt.Sprintf(" Stars:`%s`.", stars)
}
defaultLogger.Info("get repo info success",
zap.String("repo", name),
zap.Int("star", count),
)
}
if repo.star > min {
repos = append(repos, repo)
}
continue
}
// 如果开始不匹配,则该分类结束
if newCatStart {
newCatStart = false
// 对repo按star从高至低排序
sort.Sort(sort.Reverse(repos))
for _, item := range repos {
result = append(result, item.content)
}
}
result = append(result, line)
}
return
}
func main() {
// 如果没有不匹配,调用github api的频率限制较低
clientToken = flag.String("clientToken", "", "github personal access token")
flag.Parse()
resp, err := ins.Get("https://raw.githubusercontent.com/avelino/awesome-go/main/README.md")
if err != nil {
panic(err)
}
lines := strings.SplitN(string(resp.Data), "\n", -1)
result, err := arrangeByStar(lines, -1)
if err != nil {
panic(err)
}
err = os.WriteFile("./README.md", []byte(strings.Join(result, "\n")), 0600)
if err != nil {
panic(err)
}
result, err = arrangeByStar(lines, 1000)
if err != nil {
panic(err)
}
err = os.WriteFile("./README-1k.md", []byte(strings.Join(result, "\n")), 0600)
if err != nil {
panic(err)
}
}