42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
// torob.js
|
|
const fetch = require("node-fetch");
|
|
|
|
async function searchTorob(query, size = 24) {
|
|
if (!query) return [];
|
|
|
|
try {
|
|
const encodedQuery = encodeURIComponent(query);
|
|
// Use the exact working endpoint from your HTML
|
|
const url = `https://api.torob.com/v4/base-product/search/?page=0&sort=popularity&size=${size}&q=${encodedQuery}`;
|
|
|
|
// Minimal headers that actually work
|
|
const headers = {
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
|
"Accept": "application/json",
|
|
"Referer": "https://www.torob.com/"
|
|
};
|
|
|
|
const response = await fetch(url, { headers });
|
|
|
|
// Don't throw on non-200, just return empty array
|
|
if (response.status !== 200) {
|
|
console.log(`Torob returned status: ${response.status}`);
|
|
return [];
|
|
}
|
|
|
|
const data = await response.json();
|
|
const results = data.results || [];
|
|
|
|
return results.map(item => ({
|
|
title: item.name1 || "بدون عنوان",
|
|
price: item.price_text || "—",
|
|
image: item.image_url || "https://via.placeholder.com/150",
|
|
link: `https://torob.com${item.web_client_absolute_url || ''}`
|
|
}));
|
|
} catch (err) {
|
|
console.error("Torob error:", err.message);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
module.exports = { searchTorob }; |