-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnotion.js
156 lines (136 loc) · 3.95 KB
/
notion.js
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
#!/usr/bin/env node
const fetch = require("node-fetch");
const fs = require("fs");
const statusInterval = 5000;
const login = async (email, password) => {
const res = await fetch("https://www.notion.so/api/v3/loginWithEmail", {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
method: "POST",
});
try {
const token = res.headers
.raw()
["set-cookie"].join("")
.match(/token_v2=([0-9a-f]+);/)[1];
return token;
} catch (error) {
console.log("Login failed", await res.json());
process.exit(-1);
}
};
const notionExport = async (token, spaceId, exportType) => {
console.log(
`Starting export in ${exportType}. SpaceId: ${spaceId.slice(0, 6)}...`
);
const task = {
task: {
eventName: "exportSpace",
request: {
spaceId,
exportOptions: {
exportType,
timeZone: "Asia/Yekaterinburg",
locale: "en",
},
},
},
};
const res = await fetch("https://www.notion.so/api/v3/enqueueTask", {
credentials: "include",
headers: {
Cookie: `token_v2=${token};`,
"Content-Type": "application/json",
},
body: JSON.stringify(task),
method: "POST",
mode: "cors",
});
const { taskId } = await res.json();
console.log(`Waiting for export. Task: ${taskId.slice(0, 6)}...`);
const exportURL = await new Promise((resolve, reject) => {
const interval = setInterval(async () => {
const res = await fetch("https://www.notion.so/api/v3/getTasks", {
headers: {
Cookie: `token_v2=${token};`,
"Content-Type": "application/json",
},
body: JSON.stringify({ taskIds: [taskId] }),
method: "POST",
});
const json = await res.json();
const status = json.results[0].status;
if (!status) {
clearInterval(interval);
reject(new Error(json));
} else if (status.type == "progress") {
console.log(`${status.pagesExported} pages exported`);
} else if (status.type == "complete") {
clearInterval(interval);
console.log("Export done");
resolve(status.exportURL);
}
}, statusInterval);
});
return exportURL;
};
const download = async (url) => {
const res = await fetch(url);
const dest = fs.createWriteStream(
`./data/${url.match(/Export[0-9a-f-]+.zip/)[0]}`
);
res.body.pipe(dest);
const size = res.headers.get("content-length");
let recived = 0;
let lastRecived = 0;
res.body.on("data", (chunk) => {
recived += chunk.length;
});
const interval = setInterval(() => {
console.log(
`Downloading ${(recived / 1048576).toFixed(1)}/${(size / 1048576).toFixed(
1
)} mb (${((recived / size) * 100).toFixed(1)}%) ${(
((recived - lastRecived) / 1048576 / (statusInterval / 1000)) *
8
).toFixed(2)}mbps`
);
lastRecived = recived;
}, statusInterval);
await new Promise((r) => {
res.body.on("end", r);
});
clearInterval(interval);
console.log("done");
};
(async () => {
const email = process.env.EMAIL;
const password = process.env.PASSWORD;
const exportType = process.env.EXPORT_TYPE || "both";
const token = await login(email, password);
console.log("Login successful");
const res = await fetch("https://www.notion.so/api/v3/getSpaces", {
headers: {
Cookie: `token_v2=${token};`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
method: "POST",
});
const json = await res.json();
for (const user in json) {
for (const spaceId in json[user].space) {
if (exportType === "both") {
const mdUrl = await notionExport(token, spaceId, "markdown");
await download(mdUrl);
const htmlUrl = await notionExport(token, spaceId, "html");
await download(htmlUrl);
} else {
const url = await notionExport(token, spaceId, exportType);
await download(url);
}
}
}
})();