41 lines
1.4 KiB
JavaScript
41 lines
1.4 KiB
JavaScript
// lioncomputer.js
|
|
const puppeteer = require("puppeteer");
|
|
|
|
async function searchLionComputer(query, limit = 12) {
|
|
if (!query) return [];
|
|
|
|
let browser;
|
|
try {
|
|
browser = await puppeteer.launch({
|
|
headless: true,
|
|
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
|
});
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto(`https://lioncomputer.com/search?q=${encodeURIComponent(query)}`, {
|
|
waitUntil: 'networkidle2',
|
|
timeout: 30000
|
|
});
|
|
|
|
const products = await page.evaluate((limit) => {
|
|
const items = Array.from(document.querySelectorAll('.product-item, .product-card'));
|
|
return items.slice(0, limit).map(item => {
|
|
const title = item.querySelector('h3, h4, .title')?.textContent || "بدون عنوان";
|
|
const price = item.querySelector('.price, .cost')?.textContent || "—";
|
|
const image = item.querySelector('img')?.src || "https://via.placeholder.com/150";
|
|
const link = item.querySelector('a')?.href || "#";
|
|
|
|
return { title, price, image, link };
|
|
});
|
|
}, limit);
|
|
|
|
return products;
|
|
} catch (err) {
|
|
console.error("Lion Computer scraping error:", err.message);
|
|
return [];
|
|
} finally {
|
|
if (browser) await browser.close();
|
|
}
|
|
}
|
|
|
|
module.exports = { searchLionComputer }; |