-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
duplicate-checker.go
99 lines (81 loc) · 2.45 KB
/
duplicate-checker.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
package main
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/dstotijn/go-notion"
"github.com/zhuochun/notion-toolset/transformer"
)
type DuplicateCheckerConfig struct {
DatabaseID string `yaml:"databaseID"`
DatabaseQuery string `yaml:"databaseQuery"`
CheckProperties []string `yaml:"checkProperties"` // TODO Check by specific properties
DuplicateDumpID string `yaml:"duplicateDumpID"`
DuplicateDumpTextBlock string `yaml:"duplicateDumpTextBlock"` // Format https://pkg.go.dev/github.com/dstotijn/go-notion#ParagraphBlock
// DuplicateDumpBlock string `yaml:"duplicateDumpBlock"` // DEPRECATED (2023-12) use duplicateDumpTextBlock
}
type DuplicateChecker struct {
DebugMode bool
Client *notion.Client
DuplicateCheckerConfig
}
func (d *DuplicateChecker) Validate() error {
if len(d.DuplicateDumpTextBlock) == 0 {
return errors.Join(ErrConfigRequired, fmt.Errorf("set duplicateDumpTextBlock"))
}
return nil
}
func (d *DuplicateChecker) Run() error {
pagesChan, errChan := d.ScanPages()
pageNum := 0
set := map[string]string{}
for pages := range pagesChan {
for _, page := range pages {
pageNum += 1
title, err := transformer.GetPageTitle(page)
if err != nil {
log.Printf("Err pageID: %v, err: %v", page.ID, err)
continue
}
if id, ok := set[title]; ok {
d.WriteBlock(page.ID)
d.WriteBlock(id)
} else {
set[title] = page.ID
}
if d.DebugMode && pageNum%500 == 0 {
log.Printf("Scanned pages: %v so far", pageNum)
}
}
}
log.Printf("Scanned pages: %v, unique: %v", pageNum, len(set))
select {
case err := <-errChan:
return err
default:
return nil
}
}
func (d *DuplicateChecker) ScanPages() (chan []notion.Page, chan error) {
q := NewDatabaseQuery(d.Client, d.DatabaseID)
if err := q.SetQuery(d.DatabaseQuery, QueryBuilder{}); err != nil {
log.Panicf("Invalid query: %v, err: %v", d.DatabaseQuery, err)
}
if d.DebugMode {
log.Printf("DatabaseQuery Filter: %+v", q.Query.Filter)
log.Printf("DatabaseQuery Sorter: %+v", q.Query.Sorts)
}
return q.Go(context.TODO(), 3)
}
func (d *DuplicateChecker) WriteBlock(pageID string) (notion.BlockChildrenResponse, error) {
w := NewAppendBlock(d.Client, d.DuplicateDumpID)
if err := w.AddParagraph("Duplicate", d.DuplicateDumpTextBlock, BlockBuilder{
Date: time.Now().Format(layoutDate),
PageID: pageID,
}); err != nil {
return notion.BlockChildrenResponse{}, err
}
return w.Do(context.TODO())
}