Feat/parapharmacy api #38
@@ -107,10 +107,13 @@ async function scrapeSource(browser, source, query) {
|
|||||||
});
|
});
|
||||||
console.log(` 🔍 Product info:`, JSON.stringify(productInfo, null, 2));
|
console.log(` 🔍 Product info:`, JSON.stringify(productInfo, null, 2));
|
||||||
|
|
||||||
const products = await page.evaluate((selectors) => {
|
const products = await page.evaluate((selectors, searchQuery) => {
|
||||||
const results = [];
|
const results = [];
|
||||||
const cards = document.querySelectorAll(selectors.product);
|
const cards = document.querySelectorAll(selectors.product);
|
||||||
|
|
||||||
|
// Get the search query to filter relevant products
|
||||||
|
const query = searchQuery.toLowerCase();
|
||||||
|
|
||||||
cards.forEach(card => {
|
cards.forEach(card => {
|
||||||
// Try data attributes first (Promofarma style)
|
// Try data attributes first (Promofarma style)
|
||||||
let name = card.getAttribute('data-name');
|
let name = card.getAttribute('data-name');
|
||||||
@@ -132,7 +135,9 @@ async function scrapeSource(browser, source, query) {
|
|||||||
if (name && priceStr) {
|
if (name && priceStr) {
|
||||||
const price = parseFloat(priceStr.replace(/[^0-9.,]/g, '').replace(',', '.')) || 0;
|
const price = parseFloat(priceStr.replace(/[^0-9.,]/g, '').replace(',', '.')) || 0;
|
||||||
|
|
||||||
if (name && price > 0 && price < 1000) {
|
// Filter: only include products that contain the search query
|
||||||
|
const nameLower = name.toLowerCase();
|
||||||
|
if (name && price > 0 && price < 1000 && nameLower.includes(query)) {
|
||||||
results.push({
|
results.push({
|
||||||
name: name.substring(0, 200),
|
name: name.substring(0, 200),
|
||||||
price,
|
price,
|
||||||
@@ -144,7 +149,7 @@ async function scrapeSource(browser, source, query) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}, config.selectors);
|
}, config.selectors, query);
|
||||||
|
|
||||||
console.log(` 📦 Found ${products.length} products`);
|
console.log(` 📦 Found ${products.length} products`);
|
||||||
return products.slice(0, 10).map(p => ({
|
return products.slice(0, 10).map(p => ({
|
||||||
|
|||||||
Reference in New Issue
Block a user