This repository has been archived by the owner on Jun 30, 2024. It is now read-only.
forked from upleveled/notion-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
229 lines (195 loc) · 6.07 KB
/
index.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { createWriteStream, mkdirSync, rmSync, unlinkSync } from 'node:fs';
import path from 'node:path';
import { Stream } from 'node:stream';
import axios from 'axios';
import extract from 'extract-zip';
import pMap from 'p-map';
const blocks = [
{
// Find the page block ID by either:
// 1. Copying the alphanumeric part at the end of the Notion
// page URL and separate it with dashes in the same format
// as below (number of characters between dashes:
// 8-4-4-4-12)
// 2. Inspecting network requests in the DevTools
id: 'a972a1cb-d73a-4ae6-a314-b3ddfe5fde7c',
// Find the space ID associated with a block by running this
// in the DevTools Console while on the page you want to
// export:
// ```
// $('img[src*="spaceId="]').src.replace(/^.+&spaceId=([^&]+)&.+$/, '$1')
// ```
spaceId: 'efaffe08-86ac-4005-a962-98307672ba3a',
// Choose a directory name for your export to appear in the
// `exports` folder
dirName: 'notion-page-a',
// Should all of the subpages also be exported?
recursive: false,
},
];
if (!process.env.NOTION_TOKEN) {
console.error(
'Environment variable NOTION_TOKEN is missing. Check the README.md for more information.',
);
process.exit(1);
}
type BlockTask = {
id: string;
state: string | null;
status: {
pagesExported: number | null;
exportURL: string | null;
};
};
type Task = {
id: string;
state: string | null;
status?: {
pagesExported: number | null;
exportURL: string | null;
};
};
const client = axios.create({
// Notion unofficial API
baseURL: 'https://www.notion.so/api/v3',
headers: {
Cookie: `token_v2=${process.env.NOTION_TOKEN}`,
},
});
function delay(ms: number) {
console.log(
`Waiting ${ms / 1000} second${ms > 1000 ? 's' : ''} before polling again...`,
);
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// Enqueue all export tasks immediately, without waiting for the
// export tasks to complete
const enqueuedBlocks = await pMap(blocks, async (block) => {
const {
data: { taskId },
}: { data: { taskId: string } } = await client.post('enqueueTask', {
task: {
eventName: 'exportBlock',
request: {
block: {
id: block.id,
spaceId: block.spaceId,
},
exportOptions: {
exportType: 'markdown',
locale: 'en',
timeZone: 'Europe/Vienna',
},
recursive: block.recursive,
},
},
});
if (!taskId) {
throw new Error('No taskId returned from enqueueTask');
}
console.log(`Started export of block ${block.dirName} as task ${taskId}`);
const task: BlockTask = {
id: taskId,
state: null,
status: {
pagesExported: null,
exportURL: null,
},
};
return {
...block,
task: task,
};
});
let retries = 0;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
while (true) {
const incompleteEnqueuedBlocks = enqueuedBlocks.filter(
({ task }) => task.state !== 'success',
);
const taskIds = incompleteEnqueuedBlocks.map(({ task }) => task.id);
try {
const {
data: { results },
headers: { 'set-cookie': getTasksRequestCookies },
}: { data: { results: Task[] }; headers: { 'set-cookie': string[] } } =
await client.post('getTasks', {
taskIds: taskIds,
});
const blocksWithTaskProgress = results.reduce(
(blocksAcc, task) => {
const block = enqueuedBlocks.find(({ task: { id } }) => id === task.id);
if (!block || !task.status) return blocksAcc;
// Mutate original object in enqueuedBlocks for while loop
// exit condition
block.task.state = task.state;
block.task.status.pagesExported = task.status.pagesExported;
block.task.status.exportURL = task.status.exportURL;
return blocksAcc.concat(block);
},
[] as typeof incompleteEnqueuedBlocks,
);
for (const block of blocksWithTaskProgress) {
console.log(
`Exported ${block.task.status.pagesExported} pages for ${block.dirName}`,
);
if (block.task.state === 'success') {
const backupDirPath = path.join(
process.cwd(),
'exports',
block.dirName,
);
const temporaryZipPath = path.join(
process.cwd(),
'exports',
`${block.dirName}.zip`,
);
console.log(`Export finished for ${block.dirName}`);
const response = await client<Stream>({
method: 'GET',
url: block.task.status.exportURL || undefined,
responseType: 'stream',
headers: {
Cookie: getTasksRequestCookies.find((cookie) =>
cookie.includes('file_token='),
),
},
});
const sizeInMb =
Number(response.headers['content-length']) / 1000 / 1000;
console.log(`Downloading ${Math.round(sizeInMb * 1000) / 1000}mb...`);
const stream = response.data.pipe(createWriteStream(temporaryZipPath));
await new Promise((resolve, reject) => {
stream.on('close', resolve);
stream.on('error', reject);
});
rmSync(backupDirPath, { recursive: true, force: true });
mkdirSync(backupDirPath, { recursive: true });
await extract(temporaryZipPath, { dir: backupDirPath });
unlinkSync(temporaryZipPath);
console.log(`✅ Export of ${block.dirName} downloaded and unzipped`);
}
}
// If all blocks are done, break out of the loop
if (!enqueuedBlocks.find(({ task }) => task.state !== 'success')) {
break;
}
// Reset retries on success
retries = 0;
} catch (error) {
if (!axios.isAxiosError(error) || error.response?.status !== 429) {
// Rethrow errors which do not contain an HTTP 429 status
// code
throw error;
}
console.log(
'Received response with HTTP 429 (Too Many Requests), increasing delay...',
);
retries += 1;
}
// Rate limit polling, with incremental backoff
await delay(1000 + 1000 * retries);
}
console.log('✅ All exports successful');