-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawesome.go
195 lines (168 loc) · 5.64 KB
/
awesome.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
190
191
192
193
194
195
// Octocat - B3log 的 GitHub 仓库操作服务
// Copyright (c) 2019-present, b3log.org
//
// Lute is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
package main
import (
"encoding/base64"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/88250/gulu"
"github.com/PuerkitoBio/goquery"
"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
"github.com/parnurzeal/gorequest"
)
var blogs = &sync.Map{} // "88250/solo-blog" -> *blog
type blog struct {
title string // D 的个人博客 - 开源程序员,自由职业者
homepage string // https://88250.b3log.org
repo string // 88250/solo-blog
favicon string
articleCnt int
recentArticleTime uint64
}
type blogSlice []*blog
func (s blogSlice) Len() int { return len(s) }
func (s blogSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s blogSlice) Less(i, j int) bool { return s[i].recentArticleTime > s[j].recentArticleTime }
var orgAk = ""
var period = time.Hour * 6
func updateAwesomeSolo() {
if 1 > len(orgAk) {
return
}
for range time.Tick(period) {
updateAwesomeSoloNow()
}
}
func updateAwesomeSoloNow() {
defer gulu.Panic.Recover(nil)
updateAwesomeSoloReadme()
}
func sortAwesomeSolo() (ret blogSlice) {
blogs.Range(func(key, value interface{}) bool {
blog := value.(*blog)
ret = append(ret, blog)
return true
})
sort.Sort(ret)
for _, blog := range ret {
fmt.Println(blog.title, blog.homepage, blog.recentArticleTime, blog.articleCnt)
}
return
}
func updateAwesomeSoloReadme() (ok bool, blogCount, articleCount int) {
solos := sortAwesomeSolo()
result := map[string]interface{}{}
filePath := "README.md"
content := "| 图标 | 标题 | 链接 | 文章 | 仓库 |\n"
content += "| :---: | --- | --- | ---: | :---: |\n"
for _, solo := range solos {
title := solo.title
document, err := goquery.NewDocumentFromReader(strings.NewReader(title))
if nil == err {
title = document.Text()
}
title = sanitize(title)
runes := []rune(title)
if 26 <= len(runes) {
title = string(runes[:26])
}
title = strings.TrimSpace(title)
if strings.HasSuffix(title, "-") {
title = title[:len(title)-1]
title = strings.TrimSpace(title)
}
if 1 > len(title) {
continue
}
homepage := sanitize(solo.homepage)
favicon := sanitize(solo.favicon)
if 0 < len(favicon) {
favicon = "<img src=\"" + favicon + "\" width=\"24px\"/>"
favicon = strings.ReplaceAll(favicon, "/interlace/0", "")
}
if strings.Contains(favicon, "solo-") {
favicon = ""
}
ac := fmt.Sprintf("%d", solo.articleCnt)
if "0" == ac {
ac = ""
}
content += "| " + favicon + " | " + title + " | " + homepage + "| " + ac + " | [:octocat:](https://github.com/" + solo.repo + ") |\n"
blogCount++
articleCount += solo.articleCnt
}
if 1 > blogCount {
return
}
content += "\n注:\n\n"
content += "* 展示顺序按发布文章时间降序排列\n"
content += "* 通过 [Octocat](https://github.com/88250/octocat) 自动定时刷新,请勿 PR\n"
content = "<p align=\"center\">📈 目前已收录 " + strconv.Itoa(blogCount) + " 个站点,共 " + strconv.Itoa(articleCount) + " 篇文章</p>\n\n" + content
logger.Info("[awesome-solo]'s README.md content is [" + content + "]")
response, bytes, errors := gorequest.New().Get("https://api.github.com/repos/88250/awesome-solo/git/trees/master?access_token="+orgAk).
Set("User-Agent", UserAgent).Timeout(30 * time.Second).EndStruct(&result)
if nil != errors {
logger.Errorf("get git tree of file [%s] failed: %s", filePath, errors[0])
return
}
if http.StatusOK != response.StatusCode && http.StatusConflict != response.StatusCode {
logger.Errorf("get git tree of file [%s] status code [%d], body [%s]", filePath, response.StatusCode, string(bytes))
return
}
now := time.Now()
pattern := "2006-01-02 15:04:05"
body := map[string]interface{}{
"message": ":memo: 定时更新 " + now.Format(pattern) + ",下次刷新时间为 " + now.Add(period).Format(pattern),
"content": base64.StdEncoding.EncodeToString([]byte(content)),
}
if http.StatusOK == response.StatusCode {
tree := result["tree"].([]interface{})
for _, f := range tree {
file := f.(map[string]interface{})
if filePath == file["path"].(string) {
body["sha"] = file["sha"]
break
}
}
}
response, bytes, errors = gorequest.New().Put("https://api.github.com/repos/88250/awesome-solo/contents/"+filePath+"?access_token="+orgAk).
Set("User-Agent", UserAgent).Timeout(2 * time.Minute).
SendMap(body).EndStruct(&result)
if nil != errors {
logger.Errorf("update repo [88250/awesome-solo] file [%s] failed: %s", filePath, errors[0])
return
}
if http.StatusOK != response.StatusCode && http.StatusCreated != response.StatusCode {
logger.Errorf("update repo [88250/awesome-solo] file [%s] status code: %d, body: %s", filePath, response.StatusCode, string(bytes))
return
}
logger.Infof("updated repo [88250/awesome-solo] file [%s]", filePath)
ok = true
return
}
func refreshAwesomeSolo(c *gin.Context) {
updateAwesomeSoloNow()
c.Status(200)
}
func sanitize(str string) (ret string) {
ret = bluemonday.UGCPolicy().Sanitize(str)
ret = strings.ReplaceAll(ret, "\n", " ")
ret = strings.ReplaceAll(ret, "|", "\\|")
ret = strings.TrimSpace(ret)
return
}