-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·374 lines (336 loc) · 12.1 KB
/
index.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env node
import dotenv from "dotenv";
import axios from "axios";
import fastcsv from "fast-csv";
import fs from "fs";
import path from "path";
import ISO6391 from "iso-639-1";
import os from "os";
import chalk from "chalk";
import { Command } from "commander";
import { fileURLToPath } from "url";
import { dirname } from "path";
// Load environment variables from .env file
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, "package.json"), "utf8")
);
const FORVO_API_BASE =
process.env.FORVO_API_BASE || "https://apifree.forvo.com";
const program = new Command();
program
.name("npx " + packageJson.name)
.description(packageJson.description)
.version(packageJson.version)
.argument(
"<path-to-file>",
'Path to Anki flashcards export file. (Use "Notes as in Plain text" format. No HTML, no tags included. )'
)
.argument("[lang]", "ISO 639-1 language code", process.env.LANGUAGE)
.option(
"-a, --articles <list>",
"Comma separated list of article in <lang>",
process.env.ARTICLES
)
.option("-u, --user <n>", "Anki user name", process.env.ANKI_USER || "User 1")
.option("-k, --key <key>", "Forvo API key", process.env.FORVO_API_KEY)
.action((filePath, lang, options) => {
// Validate API key
if (!options.key) {
console.error(
chalk.red(
"Forvo API key is required. Please provide it via the --key option or FORVO_API_KEY environment variable."
)
);
process.exit(1);
}
// Validate language
if (!lang) {
console.error(
chalk.red(
"Language code is required. Please provide it as an argument or via the LANGUAGE environment variable."
)
);
process.exit(1);
}
if (!ISO6391.validate(lang)) {
console.error(
chalk.red(
`Unsupported language "${lang}". Please provide a valid ISO 639-1 language code.`
)
);
program.help();
}
let articles = [];
if (options.articles) {
articles = options.articles.split(",").map((a) => a.trim());
}
let AUDIO_DIR;
switch (os.platform()) {
case "darwin":
AUDIO_DIR = path.resolve(
os.homedir(),
`Library/Application Support/Anki2/${options.user}/collection.media/`
);
break;
case "win32":
AUDIO_DIR = path.resolve(
os.homedir(),
`AppData/Roaming/Anki2/${options.user}/collection.media/`
);
break;
case "linux":
AUDIO_DIR = path.resolve(
os.homedir(),
`.local/share/Anki2/${options.user}/collection.media/`
);
break;
default:
console.error(chalk.red(`Unsupported platform`));
process.exit(1);
}
// Check if the user directory exists
if (!fs.existsSync(AUDIO_DIR)) {
console.error(
chalk.red(
`The directory for user "${options.user}" does not exist: ${AUDIO_DIR}`
)
);
console.error(chalk.yellow("Use -u <user> to specify a different user."));
process.exit(1); // Exit if the directory does not exist
}
console.info(chalk.blue(`Dir for audio files: ${AUDIO_DIR}`));
async function fetchPronunciation(word, retryCount = 0) {
// Generate all possible versions of the word
const versions = new Set([word]);
// Add version without punctuation
const cleanWord = word.replace(/[.,!?;:]/g, "").trim();
versions.add(cleanWord);
// Add version without articles if present
if (articles.length) {
const articleRegex = new RegExp(`\\b${articles.join("|")}\\b`, "i");
const articleMatch = articleRegex.exec(cleanWord);
if (articleMatch) {
const article = articleMatch[0];
versions.add(cleanWord.replace(article, "").trim());
}
}
// Check for existing files for all versions
for (const version of versions) {
const baseFilename = `${version}_${lang}`;
const oggPath = path.resolve(AUDIO_DIR, `./${baseFilename}.ogg`);
const mp3Path = path.resolve(AUDIO_DIR, `./${baseFilename}.mp3`);
if (fs.existsSync(oggPath)) {
console.log(
chalk.green(`Using existing OGG file for "${word}": ${oggPath}`)
);
return `[sound:${baseFilename}.ogg]`;
}
if (fs.existsSync(mp3Path)) {
console.log(
chalk.green(`Using existing MP3 file for "${word}": ${mp3Path}`)
);
return `[sound:${baseFilename}.mp3]`;
}
}
// If no existing files found, try API requests for each version
for (const version of versions) {
try {
console.log(`\n---Trying version: ${version}---`);
const apiUrl = `${FORVO_API_BASE}/key/${
options.key
}/format/json/action/word-pronunciations/word/${encodeURIComponent(
version
)}/language/${lang}`;
console.log(`API request URL: ${apiUrl}`);
console.log("Making API request...");
const response = await axios.get(apiUrl);
console.log(`API response status: ${response.status}`);
const data = response.data;
if (data.items && data.items.length > 0) {
console.log(`Found ${data.items.length} pronunciations`);
// Get the best rated pronunciation
const bestPronunciation = data.items.sort(
(a, b) => b.rate - a.rate
)[0];
console.log(
`Best pronunciation: Rate=${bestPronunciation.rate}, User=${bestPronunciation.username}, Country=${bestPronunciation.country}`
);
const audioUrl = bestPronunciation.pathmp3;
if (audioUrl) {
console.log(`Found audio URL: ${audioUrl}`);
return await downloadAudio(audioUrl, version);
}
} else {
console.log(`No pronunciations found for version "${version}"`);
}
} catch (error) {
console.error(chalk.red(`Error with version "${version}":`));
if (error.response) {
console.error(chalk.red(`Status: ${error.response.status}`));
if (
error.response.status === 400 &&
Array.isArray(error.response.data) &&
error.response.data[0] === "Limit/day reached."
) {
console.error(
chalk.red.bold("\n========================================")
);
console.error(chalk.red.bold("🚫 Daily API limit reached!"));
console.error(
chalk.red.bold(
"Please try again tomorrow or use a different API key."
)
);
console.error(
chalk.red.bold(
"You can provide a different key using the --key option."
)
);
console.error(
chalk.red.bold("========================================\n")
);
process.exit(1);
}
}
if (retryCount < 5) {
const delay = 1000 * (retryCount + 1);
console.log(
chalk.yellow(
`Retrying in ${delay}ms (attempt ${retryCount + 1}/5)...`
)
);
await new Promise((resolve) => setTimeout(resolve, delay));
return fetchPronunciation(word, retryCount + 1);
}
}
}
console.info(
chalk.blue(`Can't find pronunciation for "${word}" in any form`)
);
return;
}
async function downloadAudio(url, word) {
const baseFilename = `${word}_${lang}`;
// Check for both .ogg and .mp3 files
const oggPath = path.resolve(AUDIO_DIR, `./${baseFilename}.ogg`);
const mp3Path = path.resolve(AUDIO_DIR, `./${baseFilename}.mp3`);
console.log("Checking for existing files:");
console.log(`- OGG path: ${oggPath}`);
console.log(`- MP3 path: ${mp3Path}`);
// If either file exists, use it
if (fs.existsSync(oggPath)) {
console.log(chalk.green(`Using existing OGG file: ${oggPath}`));
return `[sound:${baseFilename}.ogg]`;
}
if (fs.existsSync(mp3Path)) {
console.log(chalk.green(`Using existing MP3 file: ${mp3Path}`));
return `[sound:${baseFilename}.mp3]`;
}
console.log("Starting download...");
const response = await axios({
method: "get",
url: url,
responseType: "stream",
});
console.log(`Download response status: ${response.status}`);
// Determine file extension based on content type
const contentType = response.headers["content-type"];
let extension = ".mp3"; // default to mp3
if (contentType && contentType.includes("ogg")) {
extension = ".ogg";
}
const filename = `${baseFilename}${extension}`;
const audioPath = path.resolve(AUDIO_DIR, `./${filename}`);
console.log(`No existing files found. Will download to: ${audioPath}`);
console.log(`Writing audio file to ${audioPath}`);
await new Promise((resolve, reject) => {
const writer = fs.createWriteStream(audioPath);
response.data.pipe(writer);
writer.on("finish", () => {
console.log(chalk.green("File write completed successfully"));
resolve();
});
writer.on("error", (err) => {
console.error(chalk.red(`Error writing file: ${err.message}`));
reject(err);
});
});
console.log(chalk.green(`Successfully saved audio for "${word}"`));
return `[sound:${filename}]`;
}
function readTSV(filePath) {
const OUT_FILE_PATH = path.resolve(
path.dirname(filePath),
`${path.basename(filePath, ".tsv")}_pronunciations.tsv`
);
try {
if (fs.existsSync(OUT_FILE_PATH)) {
fs.unlinkSync(OUT_FILE_PATH);
}
} catch (err) {
console.error(err);
}
return new Promise((resolve, reject) => {
const results = [];
fs.createReadStream(filePath)
.pipe(fastcsv.parse({ headers: false, delimiter: "\t" }))
.on("data", ([guid, front, back]) => {
if (!guid || !front || !back) return; // Skip metadata lines
results.push([guid, front, back]);
})
.on("end", async () => {
try {
let records = [];
let missedWords = [];
for (const data of results.filter(Boolean)) {
const [guid, front, back] = data;
const audioEntry = await fetchPronunciation(front);
if (!audioEntry) missedWords.push(front);
records.push({
guid,
front: front + (audioEntry ?? ""),
back: back,
});
}
console.info(`Processed ${records.length} words.`);
// log missed words
missedWords.length &&
console.info(
`${missedWords.length} words could not be found in Forvo:\n`,
`${chalk.red(missedWords.join("\n"))}`
);
console.info(
chalk.blue(`Writing to ${chalk.bold(OUT_FILE_PATH)}`)
);
const writeStream = fs.createWriteStream(OUT_FILE_PATH, {
flags: "a",
});
writeStream.write("#separator:tab\n#html:true\n#guid column:1\n"); // Prepend the lines
fastcsv
.write(records, {
headers: false,
delimiter: "\t",
})
.pipe(writeStream)
.on("finish", () => {
console.log("...Done");
resolve();
})
.on("error", reject);
} catch (error) {
reject(error);
}
})
.on("error", reject);
});
}
readTSV(filePath);
});
program.configureOutput({
outputError: (str, write) => write(chalk.red(str)),
});
program.showHelpAfterError();
program.parse(process.argv);