-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_readme.go
298 lines (282 loc) · 7.14 KB
/
update_readme.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"context"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
URL "net/url"
"os"
"regexp"
"sync"
"github.com/mmcdole/gofeed"
"golang.org/x/sync/errgroup"
)
func buildProgressBar(percent float64, span int) string {
bar := ""
for i := 0; i < 100/span; i++ {
if percent >= float64(span) {
bar += "█"
} else if percent >= float64(span)/2 {
bar += "▓"
} else if percent >= 0 {
bar += "▒"
} else {
bar += "░"
}
percent -= float64(span)
}
return bar
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
type POI struct {
Lon float64
Lat float64
}
func fetchMap(geos []POI) error {
mapboxToken := os.Getenv("MAPBOX_TOKEN")
if mapboxToken == "" {
return errors.New("should provice MAPBOX_TOKEN")
}
markers := ""
for idx, geo := range geos {
if idx == 0 {
markers += "/"
} else {
markers += ","
}
markers += fmt.Sprintf("url-%s(%f,%f)", URL.PathEscape("https://cdn.jellow.site/spot.png"), geo.Lon, geo.Lat)
}
url := fmt.Sprintf("https://api.mapbox.com/styles/v1/mapbox/dark-v10/static%s/auto/1280x600@2x?logo=false&access_token=%s", markers, mapboxToken)
oldHash, err := ioutil.ReadFile("./footprint.hash")
if err != nil {
return err
}
newHash := fmt.Sprintf("%x", md5.Sum([]byte(url)))
if string(oldHash) == newHash {
log.Println("footprint does not change")
return nil
}
defer ioutil.WriteFile("./footprint.hash", []byte(newHash), os.ModePerm)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
file, err := os.OpenFile("./footprint.png", os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
return err
}
_, err = io.Copy(file, res.Body)
if err != nil {
return err
}
return nil
}
func fetchFootprint(ctx context.Context) error {
jikeUsername := os.Getenv("JIKE_USERNAME")
if jikeUsername == "" {
return errors.New("should provice JIKE_USERNAME")
}
url := fmt.Sprintf("https://api.ruguoapp.com/1.0/footprint-service/footprints/show?username=%s", jikeUsername)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
raw, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
body := struct {
Data struct {
FeatureCollection struct {
Type string `json:"type"`
Features []struct {
Type string `json:"type"`
Geometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
} `json:"geometry"`
Properties struct {
Count int `json:"count"`
} `json:"properties"`
} `json:"features"`
} `json:"featureCollection"`
Center struct {
Type string `json:"type"`
Properties struct {
} `json:"properties"`
Geometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
} `json:"geometry"`
} `json:"center"`
Envelope struct {
Type string `json:"type"`
Properties struct {
} `json:"properties"`
Geometry struct {
Type string `json:"type"`
Coordinates [][][]float64 `json:"coordinates"`
} `json:"geometry"`
} `json:"envelope"`
} `json:"data"`
}{}
if err := json.Unmarshal(raw, &body); err != nil {
return err
}
pois := make([]POI, 0)
for _, f := range body.Data.FeatureCollection.Features {
pois = append(pois, POI{
Lon: f.Geometry.Coordinates[0],
Lat: f.Geometry.Coordinates[1],
})
}
return fetchMap(pois)
}
func fetchWakatime(ctx context.Context) (string, error) {
wakatimeToken := os.Getenv("WAKATIME_TOKEN")
if wakatimeToken == "" {
return "", errors.New("should provide WAKATIME_TOKEN")
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("https://wakatime.com/api/v1/users/current/stats/last_30_days?api_key=%s", wakatimeToken),
nil,
)
if err != nil {
return "", err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
raw, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
body := struct {
Data struct {
Languages []struct {
Digital string `json:"digital"`
Hours int `json:"languages"`
Minutes int `json:"minutes"`
Name string `json:"name"`
Percent float64 `json:"percent"`
Text string `json:"text"`
TotalSeconds float64 `json:"total_seconds"`
} `json:"languages"`
} `json:"data"`
}{}
if err := json.Unmarshal(raw, &body); err != nil {
return "", err
}
section := ""
langs := body.Data.Languages
if len(langs) > 10 {
langs = langs[:10]
}
maxNameLen, maxTextLen := 0, 0
progressBarSpan := 10
for _, lang := range langs {
maxNameLen = max(len(lang.Name), maxNameLen)
maxTextLen = max(len(lang.Text), maxTextLen)
}
for _, lang := range langs {
pattern := fmt.Sprintf("%%-%ds %%-%ds %%-%ds %%.2f%%%%\n", maxNameLen, maxTextLen, progressBarSpan)
section += fmt.Sprintf(pattern, lang.Name, lang.Text, buildProgressBar(lang.Percent, progressBarSpan), lang.Percent)
}
return fmt.Sprintf("```text\n%s```", section), nil
}
func fetchDouban(ctx context.Context) (string, error) {
const doubanURL = "https://www.douban.com/feed/people/sorcererxw/interests"
fp := gofeed.NewParser()
feed, err := fp.ParseURLWithContext(doubanURL, ctx)
if err != nil {
return "", err
}
section := ""
for _, it := range feed.Items {
if regexp.MustCompile("^想").MatchString(it.Title) {
continue
}
emoji := ""
if regexp.MustCompile("^(读过)").MatchString(it.Title) {
emoji = "📚"
} else if regexp.MustCompile("^(看过)").MatchString(it.Title) {
emoji = "📺"
}
section += fmt.Sprintf("* %s[%s](%s)", emoji, it.Title, it.Link)
if it.PublishedParsed != nil {
section += fmt.Sprintf(" <code>%s</code>", it.PublishedParsed.Format("2006/01/02"))
}
section += "\n"
}
return section, nil
}
var mux sync.RWMutex
func writeSection(section string, newContent string) error {
file := "./README.md"
mux.Lock()
defer mux.Unlock()
raw, err := ioutil.ReadFile(file)
if err != nil {
return err
}
content := string(raw)
pattern := fmt.Sprintf("<!--START_SECTION:%s-->[\\S\\s]*\\s<!--END_SECTION:%s-->", section, section)
re := regexp.MustCompile(pattern)
content = re.ReplaceAllString(
content,
fmt.Sprintf("<!--START_SECTION:%s-->\n%s\n<!--END_SECTION:%s-->", section, newContent, section),
)
if err := ioutil.WriteFile(file, []byte(content), os.ModeExclusive); err != nil {
return err
}
return nil
}
func main() {
g, gctx := errgroup.WithContext(context.Background())
g.Go(func() error {
wakatimeSection, err := fetchWakatime(gctx)
if err != nil {
return err
}
log.Print(wakatimeSection)
return writeSection("waka", wakatimeSection)
})
//g.Go(func() error {
// doubanSection, err := fetchDouban(gctx)
// if err != nil {
// return err
// }
// log.Print(doubanSection)
// return writeSection("douban", doubanSection)
//})
//g.Go(func() error {
// return fetchFootprint(gctx)
//})
if err := g.Wait(); err != nil {
panic(err)
}
}