Open a Character.AI data export (ZIP or JSON) locally and save chats as .txt. Pure parser ported from kamiko.fun's hosted viewer, including the nested turn_key.chat_id split; minimal dependency-free ZIP reader using DecompressionStream in the browser and zlib in Node; synthetic sample fixture; MIT. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
126 lines
4.2 KiB
JavaScript
126 lines
4.2 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* cli.js — turn a Character.AI export into plain .txt files from the terminal.
|
|
*
|
|
* node cli.js export.zip
|
|
* node cli.js message.json character.json
|
|
* node cli.js export.zip -o ./my-chats
|
|
* node cli.js export.zip --single one file with every chat
|
|
*
|
|
* Uses the same parser as index.html. Nothing leaves your machine. No dependencies.
|
|
*/
|
|
'use strict';
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const zlib = require('node:zlib');
|
|
|
|
const { parseCaiExport, chatToText, allChatsToText, chatFileName } = require('./parser.js');
|
|
const { readJsonEntries } = require('./zip.js');
|
|
|
|
const DEFAULT_OUT_DIR = 'character-ai-chats';
|
|
const ALL_IN_ONE_NAME = 'character-ai-chats.txt';
|
|
|
|
function usage() {
|
|
return [
|
|
'Usage: node cli.js <export.zip | file.json> [more files] [-o out-dir] [--single]',
|
|
'',
|
|
' -o, --out <dir> where to write .txt files (default: ./character-ai-chats)',
|
|
' --single write one combined file instead of one file per chat',
|
|
' -h, --help show this help',
|
|
].join('\n');
|
|
}
|
|
|
|
/** @returns {{ inputs: string[], outDir: string, single: boolean, help: boolean }} */
|
|
function parseArgs(argv) {
|
|
const inputs = [];
|
|
let outDir = DEFAULT_OUT_DIR;
|
|
let single = false;
|
|
let help = false;
|
|
for (let i = 0; i < argv.length; i += 1) {
|
|
const arg = argv[i];
|
|
if (arg === '-h' || arg === '--help') help = true;
|
|
else if (arg === '--single') single = true;
|
|
else if (arg === '-o' || arg === '--out') {
|
|
outDir = argv[i + 1] || '';
|
|
i += 1;
|
|
} else inputs.push(arg);
|
|
}
|
|
return { inputs, outDir, single, help };
|
|
}
|
|
|
|
/** Read one input path into { name, text } sources. ZIPs expand to their JSON entries. */
|
|
async function readInput(file) {
|
|
const bytes = new Uint8Array(fs.readFileSync(file));
|
|
if (!file.toLowerCase().endsWith('.zip')) {
|
|
return { files: [{ name: path.basename(file), text: new TextDecoder('utf-8').decode(bytes) }], skipped: [], error: '' };
|
|
}
|
|
return readJsonEntries(bytes, (data) => new Uint8Array(zlib.inflateRawSync(data)));
|
|
}
|
|
|
|
function plural(n, noun) {
|
|
return `${n} ${noun}${n === 1 ? '' : 's'}`;
|
|
}
|
|
|
|
async function main() {
|
|
const { inputs, outDir, single, help } = parseArgs(process.argv.slice(2));
|
|
if (help || inputs.length === 0 || !outDir) {
|
|
process.stdout.write(`${usage()}\n`);
|
|
process.exitCode = help ? 0 : 2;
|
|
return;
|
|
}
|
|
|
|
const sources = [];
|
|
const skipped = [];
|
|
for (const input of inputs) {
|
|
if (!fs.existsSync(input)) {
|
|
process.stderr.write(`error: no such file: ${input}\n`);
|
|
process.exitCode = 2;
|
|
return;
|
|
}
|
|
const result = await readInput(input);
|
|
if (result.error === 'not_a_zip') {
|
|
process.stderr.write(`error: ${input} is not a ZIP file this tool can read\n`);
|
|
process.exitCode = 2;
|
|
return;
|
|
}
|
|
sources.push(...result.files);
|
|
skipped.push(...result.skipped);
|
|
}
|
|
|
|
const data = parseCaiExport(sources);
|
|
if (data.unreadable.length > 0) process.stdout.write(`skipped, not valid JSON: ${data.unreadable.join(', ')}\n`);
|
|
if (skipped.length > 0) process.stdout.write(`skipped, could not unpack: ${skipped.join(', ')}\n`);
|
|
|
|
if (data.chats.length === 0) {
|
|
process.stdout.write(
|
|
data.characters.length > 0
|
|
? `found ${plural(data.characters.length, 'character')} but no chats. Nothing written.\n`
|
|
: 'no chats or characters recognised in that file. Nothing written.\n',
|
|
);
|
|
process.exitCode = 1;
|
|
return;
|
|
}
|
|
|
|
fs.mkdirSync(outDir, { recursive: true });
|
|
const written = single
|
|
? [writeFile(outDir, ALL_IN_ONE_NAME, allChatsToText(data.chats))]
|
|
: data.chats.map((chat, i) => writeFile(outDir, chatFileName(chat, i), chatToText(chat)));
|
|
|
|
process.stdout.write(
|
|
`${plural(data.characters.length, 'character')}, ${plural(data.chats.length, 'chat')}, ${plural(data.messageCount, 'message')}\n`,
|
|
);
|
|
for (const file of written) process.stdout.write(`wrote ${file}\n`);
|
|
}
|
|
|
|
function writeFile(dir, name, text) {
|
|
const target = path.join(dir, name);
|
|
fs.writeFileSync(target, text, 'utf8');
|
|
return target;
|
|
}
|
|
|
|
main().catch((error) => {
|
|
process.stderr.write(`error: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
process.exitCode = 1;
|
|
});
|