const express = require('express'); const cors = require('cors'); const path = require('path'); const { scrapePBTechCPUs } = require('./scrapers/pbtech'); const { fetchBenchmarkData, findBenchmarkScore } = require('./scrapers/cpubenchmark'); const app = express(); const PORT = process.env.PORT || 3000; app.use(cors()); app.use(express.static('public')); // Server-Sent Events endpoint for real-time progress app.get('/api/scrape', async (req, res) => { // Set headers for SSE res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Connection', 'keep-alive'); // Helper function to send progress updates const sendProgress = (message) => { res.write(`data: ${JSON.stringify({ type: 'progress', message })}\n\n`); }; try { sendProgress('Starting CPU comparison...'); // Fetch CPUs from PB Tech sendProgress('Step 1/3: Fetching CPUs from PB Tech...'); const cpus = await scrapePBTechCPUs(sendProgress); if (cpus.length === 0) { sendProgress('No CPUs found on PB Tech. Please check the website.'); res.write(`data: ${JSON.stringify({ type: 'error', message: 'No CPUs found' })}\n\n`); res.end(); return; } // Fetch benchmark data sendProgress('Step 2/3: Fetching CPU benchmarks...'); const benchmarkData = await fetchBenchmarkData(sendProgress); // Match CPUs with benchmarks and calculate performance per dollar sendProgress('Step 3/3: Calculating performance per dollar...'); const results = []; for (let i = 0; i < cpus.length; i++) { const cpu = cpus[i]; const benchmark = findBenchmarkScore(cpu.name, benchmarkData); if (benchmark) { const performancePerDollar = benchmark / cpu.price; results.push({ name: cpu.name, price: cpu.price, benchmark: benchmark, performancePerDollar: performancePerDollar, inStock: cpu.inStock }); } if ((i + 1) % 5 === 0) { sendProgress(`Processed ${i + 1}/${cpus.length} CPUs...`); } } // Sort by performance per dollar (descending) results.sort((a, b) => b.performancePerDollar - a.performancePerDollar); sendProgress(`Found ${results.length} CPUs with benchmark data!`); sendProgress('Analysis complete!'); // Send final results res.write(`data: ${JSON.stringify({ type: 'complete', data: results })}\n\n`); res.end(); } catch (error) { console.error('Scraping error:', error); sendProgress(`Error: ${error.message}`); res.write(`data: ${JSON.stringify({ type: 'error', message: error.message })}\n\n`); res.end(); } }); app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });