-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.js
107 lines (88 loc) · 3.17 KB
/
batch.js
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
require('dotenv').config();
const AnkiExport = require('anki-apkg-export').default;
const fs = require('fs-extra');
const path = require('path');
const getTranslation = require('./utils/getTranslation');
const addCard = require('./utils/addCard');
const promptUser = require('./utils/promptUser');
// Set to keep track of added words
const existingWords = new Set();
async function processBatch(phrases, deckPath) {
try {
// Environment variable checks
const requiredEnvVars = [
'ANTHROPIC_API_KEY',
'NARAKEET_API_KEY',
'NATIVE_LANGUAGE',
'TARGET_LANGUAGE',
'NATIVE_LANGUAGE_VOICE',
'TARGET_LANGUAGE_VOICE'
];
for (const envVar of requiredEnvVars) {
if (!process.env[envVar]) {
console.error(`${envVar} is not set in the environment variables.`);
return;
}
}
// Ensure the directory exists
await fs.ensureDir(path.dirname(deckPath));
const apkg = new AnkiExport(`${process.env.TARGET_LANGUAGE}`);
let newCardsAdded = 0;
for (const native of phrases) {
try {
const target = await getTranslation(native, process.env.TARGET_LANGUAGE);
console.log(`Translation: ${native} -> ${target}`);
const confirm = await promptUser("Is this translation correct? (y/n): ");
if (confirm.toLowerCase() === 'y') {
const added = await addCard(apkg, native, target, existingWords);
if (added) {
newCardsAdded++;
console.log(`Added: ${native} - ${target}`);
}
} else {
console.log(`Skipping: ${native}`);
continue;
}
} catch (err) {
console.log(`Error processing "${native}":`, err);
continue; // Skip to next phrase on error
}
}
if (newCardsAdded > 0) {
const zip = await apkg.save();
const tempPath = `${deckPath}.temp`;
await fs.writeFile(tempPath, zip);
// Rename the temp file to the actual deck file
await fs.move(tempPath, deckPath, { overwrite: true });
console.log(`Anki deck updated successfully! Added ${newCardsAdded} new cards.`);
console.log(`Deck saved to: ${deckPath}`);
console.log('Please manually import this deck into Anki to see the changes.');
} else {
console.log('No new cards to add. Deck remains unchanged.');
}
} catch (error) {
console.error('Error updating Anki deck:', error);
}
}
// Read phrases from text file
try {
const phrases = fs.readFileSync('phrases.txt', 'utf-8')
.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#')); // Remove empty lines and comments
if (phrases.length === 0) {
console.log('No phrases found in phrases.txt');
process.exit(1);
}
console.log(`Found ${phrases.length} phrases to process...`);
const deckPath = `./${process.env.TARGET_LANGUAGE}.apkg`;
processBatch(phrases, deckPath);
} catch (error) {
if (error.code === 'ENOENT') {
console.error('phrases.txt not found. Please create a file with one phrase per line.');
} else {
console.error('Error reading phrases.txt:', error);
}
process.exit(1);
}
module.exports = processBatch;