/
notion_util.go
84 lines (74 loc) · 1.55 KB
/
notion_util.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
package main
import (
"github.com/kjk/notionapi"
)
var (
didPrintTokenStatus bool
)
func makeNotionClient() *notionapi.Client {
client := newClient()
if !didPrintTokenStatus {
didPrintTokenStatus = true
if client.AuthToken == "" {
logf("NOTION_TOKEN env variable not set. Can only access public pages\n")
} else {
// TODO: validate that the token looks legit
logf("NOTION_TOKEN env variable set, can access private pages\n")
}
}
return client
}
var (
eventsPerID = map[string]string{}
)
func downloadPage(client *notionapi.Client, pageID string) (*notionapi.Page, error) {
d, err := notionapi.NewCachingClient(cacheDir, client)
if err != nil {
return nil, err
}
d.Policy = notionapi.PolicyDownloadNewer
if flgNoCache {
d.Policy = notionapi.PolicyDownloadAlways
}
return d.DownloadPage(pageID)
}
const (
idNoDashLength = 32
)
// only hex chars seem to be valid
func isValidNoDashIDChar(c byte) bool {
switch {
case c >= '0' && c <= '9':
return true
case c >= 'a' && c <= 'f':
return true
case c >= 'A' && c <= 'F':
// currently not used but just in case notion starts using them
return true
}
return false
}
// given e.g.:
// /p/foo-395f6c6af50d44e48919a45fcc064d3e
// returns:
// 395f6c6af50d44e48919a45fcc064d3e
func extractNotionIDFromURL(uri string) string {
n := len(uri)
if n < idNoDashLength {
return ""
}
s := ""
for i := n - 1; i > 0; i-- {
c := uri[i]
if c == '-' {
continue
}
if isValidNoDashIDChar(c) {
s = string(c) + s
if len(s) == idNoDashLength {
return s
}
}
}
return ""
}