35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
// snappfood.js
|
|
const fetch = require("node-fetch");
|
|
|
|
async function searchSnappFood(query, limit = 12) {
|
|
if (!query) return [];
|
|
|
|
try {
|
|
// Use Tehran coordinates as default
|
|
const url = `https://api.snappfood.ir/search/v1/quick?lat=35.6892&lng=51.3890&search=${encodeURIComponent(query)}`;
|
|
|
|
const headers = {
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/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.status !== 200) return [];
|
|
|
|
const data = await response.json();
|
|
const restaurants = data.data?.restaurants || [];
|
|
|
|
return restaurants.slice(0, limit).map(restaurant => ({
|
|
title: restaurant.title || "بدون عنوان",
|
|
price: restaurant.delivery_time || "—",
|
|
image: restaurant.logo || "https://via.placeholder.com/150",
|
|
link: `https://snappfood.ir/restaurant/${restaurant.id}`
|
|
}));
|
|
} catch (err) {
|
|
console.error("SnappFood error:", err.message);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
module.exports = { searchSnappFood }; |