-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
149 lines (126 loc) · 2.97 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
package main
import (
_ "embed"
"log"
"sort"
"sync"
"time"
"github.com/BurntSushi/toml"
"github.com/dstotijn/go-notion"
"github.com/samber/lo"
"golang.org/x/time/rate"
)
type Config struct {
Secret string `toml:"secret"`
RootPageID string `toml:"root_page_id"`
}
//go:embed config.toml
var configStr string
var (
config Config
pageTree *TreeNode
)
type notionClient struct {
RateLimiter *rate.Limiter
*notion.Client
}
var clt *notionClient
var timeZone = "Asia/Shanghai"
var loc *time.Location
func init() {
_, err := toml.Decode(configStr, &config)
if err != nil {
log.Panicln("decode config.toml", err)
}
loc, err = time.LoadLocation(timeZone)
if err != nil {
log.Panicln("get timezone failed", err)
}
log.Println("init notion start")
nClt := notion.NewClient(config.Secret)
clt = ¬ionClient{
rate.NewLimiter(rate.Every(time.Second), 1),
nClt,
}
pageTree, err = getRootTree()
if err != nil {
log.Panicln("get root tree failed", err)
}
log.Println("init notion finished")
}
func main() {
createdPages()
// _ = addTweetToCallout(tweets[0], true)
addTweets()
}
func createdPages() {
// get all month
months := make(map[int64]string)
for _, tweet := range tweets {
month := time.Date(tweet.CreatedAt.Year(), tweet.CreatedAt.Month(), 1, 0, 0, 0, 0, loc).Unix()
months[month] = ""
}
firstDays := lo.MapToSlice(months, func(k int64, v string) time.Time {
return time.Unix(k, 0)
})
sort.Slice(firstDays, func(i, j int) bool {
return firstDays[i].Unix() < firstDays[j].Unix()
})
// create all pages
for _, month := range firstDays {
err := createPageOnDate(month)
if err != nil {
break
}
}
}
type tweetGroup struct {
month int64
tweets []*Tweet
}
func addTweets() {
// group tweets by month
var tweetGroups []tweetGroup
for k, v := range lo.GroupBy(tweets, func(tweet *Tweet) int64 {
return time.Date(tweet.CreatedAt.Year(), tweet.CreatedAt.Month(), 1, 0, 0, 0, 0, loc).Unix()
}) {
tweetGroups = append(tweetGroups, tweetGroup{k, v})
}
sort.Slice(tweetGroups, func(i, j int) bool {
return tweetGroups[i].month < tweetGroups[j].month
})
numJobs := 20
jobs := make(chan tweetGroup, numJobs)
var wg sync.WaitGroup
for w := 0; w < 10; w++ {
go worker(w, jobs, &wg)
}
for i := range tweetGroups {
jobs <- tweetGroups[i]
wg.Add(1)
}
wg.Wait()
close(jobs)
}
func worker(id int, jobs <-chan tweetGroup, wg *sync.WaitGroup) {
// append tweet to correct month
for g := range jobs {
today := g.tweets[0].CreatedAt.YearDay() - 1
var isFirst bool
log.Printf("%d begin month %d.%d", id, time.Unix(g.month, 0).Year(), time.Unix(g.month, 0).Month())
for _, tweet := range g.tweets {
if tweet.CreatedAt.YearDay() != today {
isFirst = true
today = tweet.CreatedAt.YearDay()
} else {
isFirst = false
}
err := addTweetToCallout(tweet, isFirst)
if err != nil {
break
}
}
log.Printf("%d finish month %d.%d", id, time.Unix(g.month, 0).Year(), time.Unix(g.month, 0).Month())
wg.Done()
}
}