849763896d
Run Tests on Branches / Detect Changes (push) Successful in 12s
Run Tests on Branches / Frontend Tests (push) Successful in 2m12s
Run Tests on Branches / Frontend Mobile Tests (push) Has been skipped
Run Tests on Branches / Parapharmacy API Tests (push) Successful in 2m2s
Run Tests on Branches / PIP Platform Tests (push) Has been skipped
Run Tests on Branches / Backend Tests (push) Successful in 2m8s
38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
import { Router } from 'express';
|
|
import { scrapeAll } from '../scraper.js';
|
|
import rateLimit from 'express-rate-limit';
|
|
import { requireServiceKey } from '../middleware/service-auth.js';
|
|
|
|
const router = Router();
|
|
const scrapeLimiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000,
|
|
max: 5,
|
|
standardHeaders: true,
|
|
legacyHeaders: false,
|
|
});
|
|
|
|
// Trigger scraping
|
|
router.post('/scrape', scrapeLimiter, requireServiceKey('INGEST_API_KEY'), async (req, res) => {
|
|
try {
|
|
const { queries = ['crema hidratante'], sources = ['promofarma'] } = req.body;
|
|
if (!Array.isArray(queries) || !Array.isArray(sources) || queries.length > 20 || sources.length > 10) {
|
|
return res.status(400).json({ error: 'queries and sources must be bounded arrays' });
|
|
}
|
|
|
|
console.log('[Scraper] Starting scrape...');
|
|
console.log(`[Scraper] Queries: ${queries.join(', ')}`);
|
|
console.log(`[Scraper] Sources: ${sources.join(', ')}`);
|
|
|
|
const result = await scrapeAll(queries, sources);
|
|
|
|
console.log(`[Scraper] Completed. Total: ${result.total} products`);
|
|
|
|
res.json(result);
|
|
} catch (error) {
|
|
console.error('[Scraper] Error:', error.message);
|
|
res.status(500).json({ error: 'Scraping failed' });
|
|
}
|
|
});
|
|
|
|
export default router;
|