-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.js
More file actions
115 lines (102 loc) Β· 4 KB
/
extract.js
File metadata and controls
115 lines (102 loc) Β· 4 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
const fs = require("fs");
const path = require("path");
const Database = require("better-sqlite3");
const dbFile = process.argv[2];
const outFile = process.argv[3];
if (!dbFile || !outFile) {
console.error("β Invalid arguments passed to extract.js");
console.error(" dbFile:", dbFile);
console.error(" outFile:", outFile);
console.error("β Usage: node extract.js path/to/chat.db path/to/output.csv");
process.exit(1);
}
const resolvedDbFile = path.resolve(dbFile);
const resolvedOutFile = path.resolve(outFile);
console.log("π§ͺ Running extract.js");
console.log("π dbFile:", resolvedDbFile);
console.log("π€ outFile:", resolvedOutFile);
if (!fs.existsSync(resolvedDbFile)) {
console.error("β Missing or invalid chat.db file.");
process.exit(1);
}
try {
const db = new Database(resolvedDbFile);
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table';").all();
console.log("𧬠Tables in DB:", tables.map(t => t.name).join(", "));
const stmt = db.prepare(`
SELECT
datetime(message.date / 1000000000 + strftime('%s','2001-01-01'), 'unixepoch', 'localtime') AS message_date,
handle.id AS sender_or_recipient,
message.is_from_me,
message.text,
chat.rowid AS chat_id
FROM
message
LEFT JOIN
handle ON message.handle_id = handle.rowid
LEFT JOIN
chat_message_join ON message.rowid = chat_message_join.message_id
LEFT JOIN
chat ON chat_message_join.chat_id = chat.rowid
ORDER BY
message.date ASC;
`);
const rows = stmt.all();
console.log("π Rows returned:", rows.length);
if (rows.length > 0) {
console.log("π First row:", rows[0]);
}
const csv = [
"message_date,sender_or_recipient,is_from_me,text,chat_id",
...rows.map((row, index) => {
const message_date = row.message_date || "";
const sender = row.sender_or_recipient || "Unknown";
const is_from_me = row.is_from_me != null ? row.is_from_me : "";
const text = typeof row.text === "string" ? row.text : JSON.stringify(row.text || "");
const chat_id = row.chat_id || "";
try {
return `"${message_date}","${sender}","${is_from_me}","${text.replace(/\n/g, ' ').replace(/"/g, '""')}","${chat_id}"`;
} catch (err) {
console.error(`β Failed to process row ${index}:`, row);
throw err;
}
})
].join("\n");
console.log("π Attempting to write CSV to:", resolvedOutFile);
const resolvedDir = path.dirname(resolvedOutFile);
if (!fs.existsSync(resolvedDir)) {
console.warn("π Output directory does not exist. Creating:", resolvedDir);
fs.mkdirSync(resolvedDir, { recursive: true });
}
// Debug log for the resolved output file path
console.log("π§ Writing to this exact path:", resolvedOutFile);
try {
fs.writeFileSync(resolvedOutFile, csv, "utf-8");
console.log(`β
CSV successfully written to: ${resolvedOutFile}`);
} catch (writeErr) {
console.error("β WRITE FAILURE:");
console.error("π£ Failed Path:", resolvedOutFile);
console.error("π Resolved Directory Exists:", fs.existsSync(resolvedDir));
console.error("π Directory Path:", resolvedDir);
console.error("π₯ Error Message:", writeErr.stack || writeErr.message || writeErr);
process.exit(1);
}
const preview = csv.split('\n').slice(0, 5).join('\n');
console.log("π Preview of CSV output:\n", preview);
console.log(`β
Export complete: ${resolvedOutFile}`);
console.log("β
CSV generation logic executed without error.");
} catch (err) {
console.error("β UNCAUGHT ERROR:");
console.error(err.stack || err.message || err);
process.exit(1);
}
// Allow direct execution for dev testing
if (require.main === module) {
const [,, dbPath, outputPath] = process.argv;
if (!dbPath || !outputPath) {
console.error("β Usage: node extract.js <path/to/chat.db> <path/to/output.csv>");
process.exit(1);
}
// You can test this line in dev directly:
console.log(`β
Dev Test - Running with DB: ${dbPath} to OUTPUT: ${outputPath}`);
}