-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjournal-weekly.go
132 lines (109 loc) · 3.05 KB
/
journal-weekly.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/dstotijn/go-notion"
"github.com/zhuochun/notion-toolset/transformer"
)
type WeeklyJournalConfig struct {
DatabaseID string `yaml:"databaseID"`
Limit int `yaml:"limit"`
PageQuery string `yaml:"pageQuery"`
PageProperties string `yaml:"pageProperties"`
}
type WeeklyJournal struct {
DebugMode bool
Client *notion.Client
WeeklyJournalConfig
}
func (d *WeeklyJournal) Validate() error {
return nil
}
func (d *WeeklyJournal) Run() error {
now := time.Now()
tCursor := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
pages, err := d.GetPages(tCursor)
if err != nil {
return err
}
if d.DebugMode {
log.Printf("Pages found: %v", pages)
}
for i := 0; i < d.Limit; i++ {
tCursor = d.NextMonday(tCursor)
tSunday := tCursor.AddDate(0, 0, 6)
title := tCursor.Format(layoutDate) + "/" + tSunday.Format(layoutDate)
if pages[title] {
continue
}
page, err := d.CreatePage(title, tCursor, tSunday)
if err != nil {
log.Printf("Create Page `%v` met Error: %v", title, err)
continue
}
log.Printf("Created page `%v` with ID: %v", title, page.ID)
}
return nil
}
func (d *WeeklyJournal) NextMonday(tCursor time.Time) time.Time {
if tCursor.Weekday() == time.Sunday {
return tCursor.AddDate(0, 0, 1)
} else {
return tCursor.AddDate(0, 0, 8-int(tCursor.Weekday()))
}
}
func (d *WeeklyJournal) GetPages(tCursor time.Time) (map[string]bool, error) {
queryData, err := Tmpl("DatabaseQuery", d.PageQuery, QueryBuilder{
Date: tCursor.Format(layoutDate),
})
if err != nil {
return nil, err
}
query := ¬ion.DatabaseQuery{}
if err := json.Unmarshal(queryData, query); err != nil {
return nil, fmt.Errorf("unmarshal DatabaseQuery: %w", err)
}
if d.DebugMode {
log.Printf("DatabaseQuery Filter: %+v", query.Filter)
log.Printf("DatabaseQuery Sorter: %+v", query.Sorts)
}
resp, err := d.Client.QueryDatabase(context.TODO(), d.DatabaseID, query)
if err != nil {
return nil, err
}
pages := map[string]bool{}
for _, page := range resp.Results {
title, err := transformer.GetPageTitle(page)
if err != nil {
return nil, fmt.Errorf("invalid DatabaseQuery response: %w", err)
}
pages[title] = true
}
return pages, nil
}
func (d *WeeklyJournal) CreatePage(title string, date, dateEnd time.Time) (notion.Page, error) {
propData, err := Tmpl("CreatePage Properties", d.PageProperties, PageBuilder{
Title: title,
Date: date.Format(layoutDate),
DateEnd: dateEnd.Format(layoutDate),
DatabaseID: d.DatabaseID,
})
if err != nil {
return notion.Page{}, err
}
props := ¬ion.DatabasePageProperties{}
if err := json.Unmarshal(propData, props); err != nil {
return notion.Page{}, fmt.Errorf("unmarshal Page properties: %w", err)
}
if d.DebugMode {
log.Printf("Page properties: %+v", props)
}
return d.Client.CreatePage(context.TODO(), notion.CreatePageParams{
ParentType: notion.ParentTypeDatabase,
ParentID: d.DatabaseID,
DatabasePageProperties: props,
})
}