Repository navigation
Expand file tree
/
Copy pathexport.js
More file actions
281 lines (244 loc) · 9.65 KB
/
Copy pathexport.js
File metadata and controls
281 lines (244 loc) · 9.65 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const { chromium } = require("playwright");
const fs = require("fs");
const path = require("path");
const args = process.argv.slice(2);
const MERGE_MODE = args.includes("--merge");
const OUTPUT_FLAG_IDX = args.indexOf("--output");
const OUTPUT_DIR = OUTPUT_FLAG_IDX !== -1 && args[OUTPUT_FLAG_IDX + 1] ? path.resolve(args[OUTPUT_FLAG_IDX + 1]) : path.join(__dirname, "objects");
const SHARE_URLS = args.filter((a) => !a.startsWith("--") && a !== (OUTPUT_FLAG_IDX !== -1 ? args[OUTPUT_FLAG_IDX + 1] : ""));
const MAX_RETRIES = 3;
const ORGS_CONFIG_PATH = path.join(__dirname, "orgs.json");
let ORGS_CONFIG = {};
if (fs.existsSync(ORGS_CONFIG_PATH)) {
ORGS_CONFIG = JSON.parse(fs.readFileSync(ORGS_CONFIG_PATH, "utf8"));
}
if (SHARE_URLS.length === 0) {
console.error("Usage: node export.js [--merge] [--output <dir>] <url1> [url2] ...");
console.error("Example: node export.js https://airtable.com/appXXX/shrXXX/tblXXX");
console.error(" --merge Append new records to existing output file (by _id)");
console.error(" --output Specify output directory (default: ./objects)");
process.exit(1);
}
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function exportOneTable(browser, shareUrl, attempt = 1) {
const context = await browser.newContext();
const page = await context.newPage();
let capturedUrl = null;
page.on("request", (request) => {
const url = request.url();
if (url.includes("readSharedViewData") && !url.includes("allowMsgpackOfResult")) {
capturedUrl = url;
}
});
try {
console.log(`[${attempt}/${MAX_RETRIES}] Navigating to ${shareUrl}...`);
await page.goto(shareUrl, {
waitUntil: "domcontentloaded",
timeout: 60000
});
await page.waitForTimeout(5000);
if (!capturedUrl) {
throw new Error("Failed to capture readSharedViewData request URL");
}
console.log("Captured API URL with access policy");
const appIdMatch = capturedUrl.match(/applicationId%22%3A%22(app[A-Za-z0-9]+)/);
const appId = appIdMatch ? appIdMatch[1] : "";
const orgInfo = ORGS_CONFIG[appId] || {};
const excludeFields = orgInfo.exclude_fields || [];
console.log("Fetching data via internal API...");
const result = await page.evaluate(
async ({ url, appId, excludeFields }) => {
const resp = await fetch(url, {
headers: {
"X-Requested-With": "XMLHttpRequest",
"x-airtable-application-id": appId,
"x-airtable-accept-msgpack": "true",
"x-user-locale": "en",
"x-airtable-inter-service-client": "webClient",
"x-time-zone": "America/New_York"
},
credentials: "include"
});
if (!resp.ok) {
return { error: `HTTP ${resp.status}` };
}
const data = await resp.json();
const table = data.data.table;
// Build column ID -> name map
const colMap = {};
for (const col of table.columns) {
colMap[col.id] = col.name;
}
// Build select option ID -> name map
const selectOptions = {};
for (const col of table.columns) {
if (col.typeOptions && col.typeOptions.choices) {
const choices = col.typeOptions.choices;
if (Array.isArray(choices)) {
for (const choice of choices) {
selectOptions[choice.id] = choice.name;
}
} else if (typeof choices === "object") {
for (const choiceId of Object.keys(choices)) {
const choice = choices[choiceId];
selectOptions[choiceId] = choice.name || choice;
}
}
}
}
// Convert rows to objects with column names
const records = [];
const totalRows = table.rows.length;
for (let i = 0; i < totalRows; i++) {
const row = table.rows[i];
const record = { _id: row.id, _createdTime: row.createdTime };
const cells = row.cellValuesByColumnId || {};
for (const colId of Object.keys(cells)) {
const colName = colMap[colId] || colId;
if (excludeFields.includes(colName)) continue;
let value = cells[colId];
if (typeof value === "string" && selectOptions[value]) {
value = selectOptions[value];
} else if (Array.isArray(value)) {
value = value.map((v) => {
if (typeof v === "string" && selectOptions[v]) return selectOptions[v];
if (typeof v === "object" && v !== null) return v;
return v;
});
} else if (typeof value === "object" && value !== null) {
// Flatten Airtable rich text fields to plain text
if (value.documentValue && Array.isArray(value.documentValue)) {
value = value.documentValue.map((seg) => seg.insert || "").join("");
}
}
// Only normalize date strings to date-only format
if (typeof value === "string" && value.match(/^\d{4}-\d{2}-\d{2}T/)) {
value = value.split("T")[0];
}
record[colName] = value;
}
records.push(record);
}
return { records, totalRows };
},
{ url: capturedUrl, appId, excludeFields }
);
if (result.error) {
throw new Error(`API returned ${result.error}`);
}
const { records, totalRows } = result;
console.log(`Parsed ${totalRows} rows...`);
console.log(`Extracted ${records.length} records with ${Object.keys(records[0]).length} columns`);
// Derive output filename from page title + org config
// Format: "Airtable - <BaseName> - <TableName>" -> "<org-abbr>-<base-name>-<table-name>"
// Org abbreviation comes from orgs.json (mapped by applicationId)
const pageTitle = await page.title();
const titleSlug = pageTitle
.replace(/^Airtable - /, "")
.replace(/[^a-zA-Z0-9]+/g, "-")
.replace(/^-+|-+$/g, "")
.toLowerCase();
const orgAbbr = orgInfo.org_abbreviation ? orgInfo.org_abbreviation.toLowerCase() : "org";
const baseName = orgInfo.output_name || `${orgAbbr}-${titleSlug}`;
let finalRecords = records;
let newCount = records.length;
if (MERGE_MODE) {
// Find existing file matching this table (by sourceUrl in _meta)
const existingFiles = fs.readdirSync(OUTPUT_DIR).filter((f) => f.endsWith(".json"));
let existingPath = null;
let existingData = null;
for (const file of existingFiles) {
const filePath = path.join(OUTPUT_DIR, file);
const content = JSON.parse(fs.readFileSync(filePath, "utf8"));
if (content._meta && content._meta.sourceUrl === shareUrl) {
existingPath = filePath;
existingData = content;
break;
}
}
if (existingData) {
const existingIds = new Set(existingData.records.map((r) => r._id));
const newRecords = records.filter((r) => !existingIds.has(r._id));
finalRecords = [...existingData.records, ...newRecords];
newCount = newRecords.length;
console.log(`Merge: ${existingData.records.length} existing + ${newCount} new = ${finalRecords.length} total`);
if (existingPath) {
fs.unlinkSync(existingPath);
}
} else {
console.log("No existing file found, creating new output");
}
}
const outputName = baseName;
// Save JSON with metadata
const outputPath = {
_meta: {
sourceUrl: shareUrl,
pageTitle: pageTitle,
exportedAt: new Date().getFullYear() + "-" + String(new Date().getMonth() + 1).padStart(2, "0") + "-" + String(new Date().getDate()).padStart(2, "0"),
rowCount: finalRecords.length,
columnCount: Object.keys(finalRecords[0]).length
},
records: finalRecords
};
const jsonPath = path.join(OUTPUT_DIR, `${outputName}.json`);
const jsonStr = JSON.stringify(outputPath, null, 2);
const compactJson = jsonStr.replace(/\[\s*("(?:[^"\\]|\\.)*"(?:\s*,\s*"(?:[^"\\]|\\.)*")*)\s*\]/g, (match, inner) => {
const items = inner.match(/"(?:[^"\\]|\\.)*"/g);
if (items) return `[${items.join(", ")}]`;
return match;
});
fs.writeFileSync(jsonPath, compactJson + "\n");
console.log(`JSON saved to: ${jsonPath}`);
await context.close();
return {
success: true,
url: shareUrl,
count: finalRecords.length,
newCount
};
} catch (err) {
await context.close();
if (attempt < MAX_RETRIES) {
console.error(`Attempt ${attempt} failed: ${err.message}`);
console.log(`Retrying in 3s...`);
await sleep(3000);
return exportOneTable(browser, shareUrl, attempt + 1);
}
return { success: false, url: shareUrl, error: err.message };
}
}
async function main() {
console.log(`Exporting ${SHARE_URLS.length} table(s)...\n`);
const browser = await chromium.launch({ headless: true });
const results = [];
for (const url of SHARE_URLS) {
const result = await exportOneTable(browser, url);
results.push(result);
console.log("");
}
await browser.close();
// Summary
console.log("=== Summary ===");
for (const r of results) {
if (r.success) {
const newInfo = r.newCount !== undefined ? ` (${r.newCount} new)` : "";
console.log(` OK ${r.url} -> ${r.count} records${newInfo}`);
} else {
console.log(` FAIL ${r.url} -> ${r.error}`);
}
}
const failed = results.filter((r) => !r.success);
if (failed.length > 0) {
process.exit(1);
}
}
main().catch((err) => {
console.error("Error:", err);
process.exit(1);
});