-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
journal-daily.go
122 lines (100 loc) · 2.67 KB
/
journal-daily.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/dstotijn/go-notion"
"github.com/zhuochun/notion-toolset/transformer"
)
type DailyJournalConfig struct {
DatabaseID string `yaml:"databaseID"`
Limit int `yaml:"limit"`
PageQuery string `yaml:"pageQuery"`
PageProperties string `yaml:"pageProperties"`
}
type DailyJournal struct {
DebugMode bool
Client *notion.Client
DailyJournalConfig
}
func (d *DailyJournal) Validate() error {
return nil
}
func (d *DailyJournal) 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 = tCursor.AddDate(0, 0, 1)
title := tCursor.Format(layoutDate)
if pages[title] {
continue
}
page, err := d.CreatePage(title)
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 *DailyJournal) 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 *DailyJournal) CreatePage(title string) (notion.Page, error) {
propData, err := Tmpl("CreatePage Properties", d.PageProperties, PageBuilder{
Title: title,
Date: title,
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,
})
}