-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.go
173 lines (139 loc) · 3.6 KB
/
export.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
)
type exportRequest struct {
Task *exportTask `json:"task"`
}
type exportTask struct {
EventName string `json:"eventName"`
Request *exportTaskRequest `json:"request"`
}
type exportTaskRequest struct {
BlockID string `json:"blockId"`
Recursive bool `json:"recursive"`
ExportOptions *exportTaskRequestOptions `json:"exportOptions"`
}
type exportTaskRequestOptions struct {
ExportType string `json:"exportType"`
TimeZone string `json:"timeZone"`
Locale string `json:"locale"`
}
type exportRequestResponse struct {
TaskID string `json:"taskId"`
}
type getTasks struct {
TaskIDs []string `json:"taskIds"`
}
type getTasksResponse struct {
Results []*getTasksResponseResults `json:"results"`
}
type getTasksResponseResults struct {
Status *getTasksResponseStatus `json:"status"`
}
type getTasksResponseStatus struct {
Type string `json:"type"`
ExportURL string `json:"exportURL"`
}
// Return all file paths of exported zips
func getBackup() []string {
return enqueueTask()
}
func enqueueTask() []string {
var exportedFiles []string
blockIDS := getPages()
for _, val := range blockIDS {
fmt.Print("Working on: ", val)
t := &exportRequest{
Task: &exportTask{
EventName: "exportBlock",
Request: &exportTaskRequest{
BlockID: val,
Recursive: true,
ExportOptions: &exportTaskRequestOptions{
ExportType: "markdown",
TimeZone: "Europe/London",
Locale: "en",
},
},
},
}
// Serialize json request
reqBody, err := json.Marshal(t)
if err != nil {
fmt.Println("Error serializing json: ", err)
}
// Send request
reply := requestData("enqueueTask", reqBody)
// Deserialize json response
end := exportRequestResponse{}
json.Unmarshal(reply, &end)
// Download export and add its full path to 'exportedFiles'
exportedFiles = append(exportedFiles, downloadExport(end.TaskID))
fmt.Println(" ... Completed!")
}
return exportedFiles
}
func downloadExport(taskID string) string {
var exportURL string
// Get exportURL
for {
// Give some time for export file to be created
time.Sleep(1000 * time.Millisecond)
t := getTasks{
TaskIDs: []string{taskID},
}
// Serialize json request
reqBody, err := json.Marshal(t)
if err != nil {
fmt.Println("Error serializing json: ", err)
}
// Request task info
reply := requestData("getTasks", reqBody)
// Deserialize json response
end := getTasksResponse{}
json.Unmarshal(reply, &end)
// If status is complete, break from loop and set exportURL var
if end.Results != nil {
s := end.Results[0].Status
if s != nil && s.Type == "complete" {
exportURL = s.ExportURL
break
}
}
// Wait an extra second and tell user
fmt.Print(" ... Retry in 2s")
time.Sleep(1000 * time.Millisecond)
}
// Download file to 'export<randomstr>.zip'
var filename string = "export" + randomStr(10) + ".zip"
// Get the response bytes from the url
resp, err := http.Get(exportURL)
if err != nil {
fmt.Println("Error downloading file: ", err)
}
defer resp.Body.Close()
// Create an empty file
file, err := os.Create(filename)
if err != nil {
fmt.Println("Error creating file: ", err)
}
defer file.Close()
// Write the bytes to the file
_, err = io.Copy(file, resp.Body)
if err != nil {
fmt.Println("Error writing to file: ", err)
}
// Return downloaded files full path
path, err := filepath.Abs(filename)
if err != nil {
fmt.Println("Error getting exported zips absolute path ", err)
}
return path
}