-
Notifications
You must be signed in to change notification settings - Fork 85
/
export_page.go
131 lines (113 loc) · 3.16 KB
/
export_page.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
package notionapi
import (
"fmt"
"time"
)
const (
eventExportBlock = "exportBlock"
defaultExportTimeZone = "America/Los_Angeles"
statusComplete = "complete"
ExportTypeMarkdown = "markdown"
ExportTypeHTML = "html"
)
type exportPageTaskRequest struct {
Task *exportPageTask `json:"task"`
}
type exportPageTask struct {
EventName string `json:"eventName"`
Request *exportPageRequest `json:"request"`
}
type exportPageRequest struct {
BlockID string `json:"blockId"`
Recursive bool `json:"recursive"`
ExportOptions *exportPageOptions `json:"exportOptions"`
}
type exportPageOptions struct {
ExportType string `json:"exportType"`
TimeZone string `json:"timeZone"`
}
type enqueueTaskResponse struct {
TaskID string `json:"taskId"`
RawJSON map[string]interface{} `json:"-"`
}
type getTasksExportPageResponse struct {
Results []*exportPageResult `json:"results"`
}
type exportPageResult struct {
ID string `json:"id"`
EventName string `json:"eventName"`
Request *exportPageRequest `json:"request"`
UserID string `json:"userId"`
State string `json:"state"`
Status *exportPageStatus `json:"status"`
}
type exportPageStatus struct {
Type string `json:"type"`
ExportURL string `json:"exportURL"`
PagesExported int64 `json:"pagesExported"`
}
type getTasksRequest struct {
TaskIDS []string `json:"taskIds"`
}
// RequestPageExportURL executes a raw API call to enqueue an export of pages
// and returns the URL to the exported data once the task is complete
func (c *Client) RequestPageExportURL(id string, exportType string, recursive bool) (string, error) {
id = ToDashID(id)
if !IsValidDashID(id) {
return "", fmt.Errorf("'%s' is not a valid notion id", id)
}
req := &exportPageTaskRequest{
Task: &exportPageTask{
EventName: eventExportBlock,
Request: &exportPageRequest{
BlockID: id,
Recursive: recursive,
ExportOptions: &exportPageOptions{
ExportType: exportType,
TimeZone: defaultExportTimeZone,
},
},
},
}
var rsp enqueueTaskResponse
var err error
apiURL := "/api/v3/enqueueTask"
err = c.doNotionAPI(apiURL, req, &rsp, &rsp.RawJSON)
if err != nil {
return "", err
}
var exportURL string
taskID := rsp.TaskID
for {
time.Sleep(250 * time.Millisecond)
req := getTasksRequest{
TaskIDS: []string{taskID},
}
var err error
var rsp getTasksExportPageResponse
apiURL = "/api/v3/getTasks"
err = c.doNotionAPI(apiURL, req, &rsp, nil)
if err != nil {
return "", err
}
status := rsp.Results[0].Status
if status != nil && status.Type == statusComplete {
exportURL = status.ExportURL
break
}
time.Sleep(750 * time.Millisecond)
}
return exportURL, nil
}
// ExportPages exports a page as html or markdown, potentially recursively
func (c *Client) ExportPages(id string, exportType string, recursive bool) ([]byte, error) {
exportURL, err := c.RequestPageExportURL(id, exportType, recursive)
if err != nil {
return nil, err
}
dlRsp, err := c.DownloadFile(exportURL, nil)
if err != nil {
return nil, err
}
return dlRsp.Data, nil
}