-
Notifications
You must be signed in to change notification settings - Fork 119
/
index.ts
185 lines (159 loc) · 5.26 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
import "dotenv/config";
import axios from "axios";
import AdmZip from "adm-zip";
import { createWriteStream, promises as fs } from "fs";
import { join } from "path";
type NotionTask = {
id: string;
state: string;
status: {
pagesExported: number;
exportURL: string;
};
error?: string;
};
const { NOTION_TOKEN, NOTION_SPACE_ID, NOTION_USER_ID } = process.env;
if (!NOTION_TOKEN || !NOTION_SPACE_ID || !NOTION_USER_ID) {
throw new Error(
"Environment variable NOTION_TOKEN, NOTION_SPACE_ID or NOTION_USER_ID is missing. Check the README.md for more information."
);
}
const client = axios.create({
baseURL: "https://www.notion.so/api/v3", // Unofficial Notion API
headers: {
Cookie: `token_v2=${NOTION_TOKEN};`,
"x-notion-active-user-header": NOTION_USER_ID,
},
});
const sleep = async (seconds: number): Promise<void> => {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
};
const round = (number: number) => Math.round(number * 100) / 100;
const exportFromNotion = async (
destination: string,
format: string
): Promise<void> => {
const task = {
eventName: "exportSpace",
request: {
spaceId: NOTION_SPACE_ID,
shouldExportComments: false,
exportOptions: {
exportType: format,
collectionViewExportType: "currentView",
timeZone: "Europe/Berlin",
locale: "en",
preferredViewMap: {},
},
},
};
const {
data: { taskId },
}: { data: { taskId: string } } = await client.post("enqueueTask", { task });
console.log(`Started export as task [${taskId}].`);
let exportURL: string;
let fileTokenCookie: string | undefined;
let retries = 0;
while (true) {
await sleep(2 ** retries); // Exponential backoff
try {
const {
data: { results: tasks },
headers: { "set-cookie": getTasksRequestCookies },
}: {
data: { results: NotionTask[] };
headers: { [key: string]: string[] };
} = await client.post("getTasks", { taskIds: [taskId] });
const task = tasks.find((t) => t.id === taskId);
if (!task) throw new Error(`Task [${taskId}] not found.`);
if (task.error) throw new Error(`Export failed with reason: ${task.error}`);
console.log(`Exported ${task.status.pagesExported} pages.`);
if (task.state === "success") {
exportURL = task.status.exportURL;
fileTokenCookie = getTasksRequestCookies.find((cookie) =>
cookie.includes("file_token=")
);
if (!fileTokenCookie) {
throw new Error("Task finished but file_token cookie not found.");
}
console.log(`Export finished.`);
break;
}
// Reset retries on success
retries = 0;
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 429) {
console.log("Received response with HTTP 429 (Too Many Requests), retrying after backoff.");
retries += 1;
continue;
}
// Rethrow if it's not a 429 error
throw error;
}
}
const response = await client({
method: "GET",
url: exportURL,
responseType: "stream",
headers: { Cookie: fileTokenCookie },
});
const size = response.headers["content-length"];
console.log(`Downloading ${round(size / 1000 / 1000)}mb...`);
const stream = response.data.pipe(createWriteStream(destination));
await new Promise((resolve, reject) => {
stream.on("close", resolve);
stream.on("error", reject);
});
};
const extractZip = async (
filename: string,
destination: string
): Promise<void> => {
const zip = new AdmZip(filename);
zip.extractAllTo(destination, true);
const extractedFiles = zip.getEntries().map((entry) => entry.entryName);
const partFiles = extractedFiles.filter((name) =>
name.match(/Part-\d+\.zip/)
);
// Extract found "Part-*.zip" files to destination and delete them:
await Promise.all(
partFiles.map(async (partFile: string) => {
partFile = join(destination, partFile);
const partZip = new AdmZip(partFile);
partZip.extractAllTo(destination, true);
await fs.unlink(partFile);
})
);
const extractedFolders = await fs.readdir(destination);
const exportFolders = extractedFolders.filter((name: string) =>
name.startsWith("Export-")
);
// Move the contents of found "Export-*" folders to destination and delete them:
await Promise.all(
exportFolders.map(async (folderName: string) => {
const folderPath = join(destination, folderName);
const contents = await fs.readdir(folderPath);
await Promise.all(
contents.map(async (file: string) => {
const filePath = join(folderPath, file);
const newFilePath = join(destination, file);
await fs.rename(filePath, newFilePath);
})
);
await fs.rmdir(folderPath);
})
);
};
const run = async (): Promise<void> => {
const workspaceDir = join(process.cwd(), "workspace");
const workspaceZip = join(process.cwd(), "workspace.zip");
await exportFromNotion(workspaceZip, "markdown");
await fs.rm(workspaceDir, { recursive: true, force: true });
await fs.mkdir(workspaceDir, { recursive: true });
await extractZip(workspaceZip, workspaceDir);
await fs.unlink(workspaceZip);
console.log("✅ Export downloaded and unzipped.");
};
run();