import dotenv from 'dotenv';
dotenv.config();
import { OpenAIVisionParser } from './build/scraper/openai_vision_parser.js';
import { existsSync, readdirSync } from 'fs';
import { resolve } from 'path';
async function testOpenAIParser() {
console.log('π§ͺ Testing OpenAI Vision Parser with 2021 PNG Images');
console.log('ββββββββββββββββββββββββββββββββββββββ\n');
const apiKey = process.env.OPENAI_API_KEY;
if (!apiKey) {
console.error('β OPENAI_API_KEY not found!');
process.exit(1);
}
const parser = new OpenAIVisionParser(apiKey);
// Path to the 2021 PNG images
const pngPath = '/Users/josuekongolo/Downloads/mcp/companyiq-mcp/data/pdfs/png_images/999059198/aarsregnskap_999059198-2021';
if (!existsSync(pngPath)) {
console.error(`β PNG folder not found: ${pngPath}`);
console.error(' Please ensure the PNG images exist at this location');
process.exit(1);
}
// List PNG files
const pngFiles = readdirSync(pngPath)
.filter(f => f.endsWith('.png'))
.sort((a, b) => {
const pageA = parseInt(a.match(/page_(\d+)/)?.[1] || '0');
const pageB = parseInt(b.match(/page_(\d+)/)?.[1] || '0');
return pageA - pageB;
});
console.log(`π Found ${pngFiles.length} PNG files in:`);
console.log(` ${pngPath}\n`);
if (pngFiles.length === 0) {
console.error('β No PNG files found!');
process.exit(1);
}
console.log('π‘ Norwegian Γ₯rsregnskap show FULL values in NOK');
console.log(' Example: "20 623" = 20,623 NOK (not 20.6M)');
console.log('\nπ Starting extraction from PNG images...\n');
try {
const startTime = Date.now();
// Since the PNG files already exist, we'll call parseFinancialPDF
// which should find and use the existing PNGs
// Create the expected PDF path (even if it doesn't exist, the PNGs do)
const pdfPath = '/Users/josuekongolo/Downloads/mcp/companyiq-mcp/data/pdfs/999059198/aarsregnskap_999059198-2021.pdf';
console.log('πΈ Using existing PNG images for analysis...');
console.log('π Expected PDF path (for reference):', pdfPath);
// The parser will look for PNGs in the expected location
// Since they already exist, it should use them
const result = await parser.parseFinancialPDF(pdfPath);
const duration = Math.round((Date.now() - startTime) / 1000);
console.log('\nββββββββββββββββββββββββββββββββββββββ');
console.log('π EXTRACTION RESULTS FOR 2021');
console.log('ββββββββββββββββββββββββββββββββββββββ\n');
console.log(`β±οΈ Time: ${duration} seconds\n`);
const formatValue = (name, value) => {
if (value === null) {
console.log(`${name}: Not found`);
return;
}
// Format based on size
if (Math.abs(value) < 1000) {
console.log(`${name}: ${value.toLocaleString('no-NO')} NOK`);
} else if (Math.abs(value) < 1000000) {
const thousands = value / 1000;
console.log(`${name}: ${value.toLocaleString('no-NO')} NOK (${thousands.toFixed(1)}K)`);
} else {
const millions = value / 1000000;
console.log(`${name}: ${value.toLocaleString('no-NO')} NOK (${millions.toFixed(1)}M)`);
}
};
formatValue('π° Revenue', result.revenue);
formatValue('π Profit', result.profit);
formatValue('π’ Assets', result.assets);
formatValue('π Equity', result.equity);
console.log('\nπ Value Analysis:');
// Check if values look reasonable (not multiplied by 1000)
const allValuesReasonable =
(!result.revenue || result.revenue < 10000000000) && // Less than 10 billion
(!result.profit || Math.abs(result.profit) < 1000000000) && // Less than 1 billion
(!result.assets || result.assets < 10000000000) &&
(!result.equity || result.equity < 10000000000);
if (allValuesReasonable) {
console.log('β
Values appear correct (no TNOK multiplication detected)');
} else {
console.log('β οΈ Some values seem too large (possible TNOK issue)');
}
// Save the extracted data
console.log('\nπΎ Extracted data for year 2021:');
console.log(JSON.stringify({
year: 2021,
revenue: result.revenue,
profit: result.profit,
assets: result.assets,
equity: result.equity,
source: 'openai_vision_extraction'
}, null, 2));
console.log('\nβ
Test completed successfully!');
// Show some sample PNG files that were analyzed
console.log('\nπ Sample PNG files analyzed:');
pngFiles.slice(0, 3).forEach(file => {
console.log(` - ${file}`);
});
if (pngFiles.length > 3) {
console.log(` ... and ${pngFiles.length - 3} more`);
}
} catch (error) {
console.error('β Extraction failed:', error.message);
console.error('\nError details:', error);
}
}
testOpenAIParser();