debug-book-formats.jsā¢4.61 kB
#!/usr/bin/env node
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
// Configuration
const CALIBRE_LIBRARY = 'D:\\e-library';
async function debugBookFormats() {
console.log('š Debug: Checking book formats and availability...');
try {
// Get book info for first few books
const bookIds = [1, 2, 3, 4, 5];
for (const bookId of bookIds) {
console.log(`\\nš Checking Book ID: ${bookId}`);
// Get detailed book info
const command = 'calibredb';
const args = ['--library-path', CALIBRE_LIBRARY, 'list', '--fields', 'id,title,authors,formats', '--for-machine', '--search', `id:${bookId}`];
console.log(`Running: ${command} ${args.join(' ')}`);
const result = await new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: 'pipe', windowsHide: true });
let stdout = '';
let stderr = '';
child.stdout.on('data', (data) => stdout += data.toString());
child.stderr.on('data', (data) => stderr += data.toString());
child.on('close', (code) => {
if (code === 0) {
resolve(stdout);
} else {
reject(new Error(`Command failed: ${stderr}`));
}
});
child.on('error', reject);
});
if (result.trim()) {
const books = JSON.parse(result);
if (books.length > 0) {
const book = books[0];
console.log(` Title: ${book.title}`);
console.log(` Authors: ${book.authors}`);
console.log(` Formats available: ${book.formats ? book.formats.length : 0}`);
if (book.formats && book.formats.length > 0) {
book.formats.forEach((format, idx) => {
console.log(` ${idx + 1}. ${format}`);
console.log(` File exists: ${fs.existsSync(format)}`);
if (fs.existsSync(format)) {
const ext = path.extname(format).toLowerCase();
console.log(` Extension: ${ext}`);
console.log(` Size: ${Math.round(fs.statSync(format).size / 1024)} KB`);
}
});
// Check for text format
const txtFormat = book.formats.find(f => f.endsWith('.txt'));
if (txtFormat) {
console.log(` ā
Has text format: ${txtFormat}`);
} else {
console.log(` ā ļø No text format available`);
// Check for OCR-compatible formats
const pdfFormat = book.formats.find(f => f.toLowerCase().endsWith('.pdf'));
const imgFormat = book.formats.find(f => {
const ext = path.extname(f).toLowerCase();
return ['.jpg', '.jpeg', '.png', '.tiff', '.bmp'].includes(ext);
});
if (pdfFormat) {
console.log(` š Has PDF format: ${pdfFormat}`);
}
if (imgFormat) {
console.log(` š¼ļø Has image format: ${imgFormat}`);
}
if (!pdfFormat && !imgFormat) {
console.log(` ā No OCR-compatible formats available`);
}
}
} else {
console.log(` ā No formats available`);
}
} else {
console.log(` ā Book not found`);
}
} else {
console.log(` ā No result from calibredb`);
}
}
} catch (error) {
console.error('ā Debug failed:', error.message);
}
}
debugBookFormats();