128 lines
4.2 KiB
JavaScript
128 lines
4.2 KiB
JavaScript
// digikala.js
|
|
const fetch = require("node-fetch");
|
|
|
|
/**
|
|
* Search products on Digikala by query
|
|
*/
|
|
async function searchDigikala(query, limit = 12) {
|
|
if (!query) return [];
|
|
|
|
try {
|
|
const encodedQuery = encodeURIComponent(query);
|
|
const url = `https://api.digikala.com/v3/search/?q=${encodedQuery}`;
|
|
|
|
const headers = {
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
"Accept": "application/json",
|
|
"Accept-Language": "en-US,en;q=0.9,fa;q=0.8"
|
|
};
|
|
|
|
const response = await fetch(url, { headers });
|
|
|
|
if (!response.ok) {
|
|
console.error(`Digikala API error: ${response.status} ${response.statusText}`);
|
|
return [];
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!data.data || !data.data.products || !Array.isArray(data.data.products)) {
|
|
return [];
|
|
}
|
|
|
|
return data.data.products.slice(0, limit).map(item => {
|
|
const title = decodeURIComponent(escape(item.title_fa || item.title || "بدون عنوان"));
|
|
const price = item.default_variant?.price?.selling_price ||
|
|
item.default_variant?.price?.rrp ||
|
|
"—";
|
|
const formattedPrice = price !== "—" ?
|
|
new Intl.NumberFormat('fa-IR').format(price) + " تومان" :
|
|
"—";
|
|
const image = item.images?.main?.url ||
|
|
item.images?.gallery?.[0]?.url ||
|
|
"https://via.placeholder.com/150";
|
|
const link = `https://www.digikala.com${item.url?.uri || ''}`;
|
|
const category = item.category?.title_fa || item.category?.title || "—";
|
|
const description = item.description || item.short_description || "";
|
|
|
|
return {
|
|
title,
|
|
category,
|
|
price: formattedPrice,
|
|
image,
|
|
description,
|
|
link
|
|
};
|
|
});
|
|
} catch (err) {
|
|
console.error("Digikala API error:", err.message);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Fetch a single product by its numeric ID
|
|
*/
|
|
async function getProductById(productId) {
|
|
if (!productId) return null;
|
|
|
|
try {
|
|
const url = `https://api.digikala.com/v2/product/${productId}/`;
|
|
|
|
const headers = {
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
"Accept": "application/json",
|
|
"Accept-Language": "en-US,en;q=0.9,fa;q=0.8"
|
|
};
|
|
|
|
const response = await fetch(url, { headers });
|
|
|
|
if (!response.ok) {
|
|
console.error(`Digikala product API error: ${response.status} ${response.statusText}`);
|
|
return null;
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!data.data?.product) {
|
|
console.warn("Product data not found in response");
|
|
return null;
|
|
}
|
|
|
|
const product = data.data.product;
|
|
|
|
// Title
|
|
const title = product.title_fa || product.title || "بدون عنوان";
|
|
|
|
// Image
|
|
const image = product.images?.main?.url || product.images?.gallery?.[0]?.url || "https://via.placeholder.com/150";
|
|
|
|
// Description
|
|
const description = product.description || product.short_description || "توضیحاتی موجود نیست.";
|
|
|
|
// Price
|
|
let price = "—";
|
|
if (product?.default_variant?.price?.selling_price) {
|
|
price = product.default_variant.price.selling_price;
|
|
} else if (product?.variants?.length > 0 && product.variants[0]?.price?.selling_price) {
|
|
price = product.variants[0].price.selling_price;
|
|
}
|
|
|
|
const formattedPrice = price !== "—"
|
|
? new Intl.NumberFormat('fa-IR').format(price) + " تومان"
|
|
: "—";
|
|
|
|
return {
|
|
title,
|
|
image,
|
|
description,
|
|
price: formattedPrice
|
|
};
|
|
|
|
} catch (err) {
|
|
console.error("Error fetching product by ID:", err.message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
module.exports = { searchDigikala, getProductById }; |