Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -4,96 +4,194 @@ import { chromium } from 'playwright';
|
|
| 4 |
const app = express();
|
| 5 |
const PORT = 7860;
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
usage: '/search?q=YOUR_QUERY',
|
| 11 |
-
note: 'Search using Playwright (Zarebin & Bertina)'
|
| 12 |
-
});
|
| 13 |
-
});
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
}
|
| 20 |
|
| 21 |
-
let browser;
|
| 22 |
try {
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
});
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
await
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
});
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
});
|
|
|
|
| 65 |
}
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
res.json({
|
| 78 |
query,
|
| 79 |
count: results.length,
|
| 80 |
results,
|
| 81 |
-
source:
|
| 82 |
});
|
| 83 |
-
|
| 84 |
} catch (error) {
|
| 85 |
-
console.error(
|
| 86 |
res.status(500).json({
|
| 87 |
error: error.message,
|
| 88 |
query,
|
| 89 |
count: 0,
|
| 90 |
results: []
|
| 91 |
});
|
| 92 |
-
} finally {
|
| 93 |
-
if (browser) await browser.close();
|
| 94 |
}
|
| 95 |
});
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
app.listen(PORT, '0.0.0.0', () => {
|
| 98 |
console.log(`🚀 Server running on port ${PORT}`);
|
| 99 |
console.log(`📍 Test: http://localhost:${PORT}/search?q=SnappFood`);
|
|
|
|
| 4 |
const app = express();
|
| 5 |
const PORT = 7860;
|
| 6 |
|
| 7 |
+
// ===== کش =====
|
| 8 |
+
const CACHE_TTL = 3600;
|
| 9 |
+
const cache = new Map();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
// ===== روش اول: Fetch مستقیم (سبک و سریع) =====
|
| 12 |
+
async function searchBertinaDirect(query) {
|
| 13 |
+
const url = `https://search.bertina.ir/search?q=${encodeURIComponent(query)}`;
|
| 14 |
+
console.log(`🔍 Fetching Bertina: ${url}`);
|
|
|
|
| 15 |
|
|
|
|
| 16 |
try {
|
| 17 |
+
const response = await fetch(url, {
|
| 18 |
+
headers: {
|
| 19 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
|
| 20 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 21 |
+
'Accept-Language': 'en-US,en;q=0.9'
|
| 22 |
+
},
|
| 23 |
+
signal: AbortSignal.timeout(15000) // ۱۵ ثانیه
|
| 24 |
});
|
| 25 |
+
|
| 26 |
+
if (!response.ok) {
|
| 27 |
+
throw new Error(`HTTP ${response.status}`);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
const html = await response.text();
|
| 31 |
+
console.log(`✅ Bertina fetch succeeded (${html.length} bytes)`);
|
| 32 |
+
return html;
|
| 33 |
+
|
| 34 |
+
} catch (error) {
|
| 35 |
+
console.error(`❌ Bertina fetch failed:`, error.message);
|
| 36 |
+
return null;
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// ===== روش دوم: Playwright (فقط در صورت نیاز) =====
|
| 41 |
+
async function searchBertinaPlaywright(query) {
|
| 42 |
+
console.log(`🤖 Trying Playwright fallback on Bertina...`);
|
| 43 |
+
const browser = await chromium.launch({
|
| 44 |
+
headless: true,
|
| 45 |
+
args: [
|
| 46 |
+
'--no-sandbox',
|
| 47 |
+
'--disable-dev-shm-usage',
|
| 48 |
+
'--disable-blink-features=AutomationControlled'
|
| 49 |
+
]
|
| 50 |
+
});
|
| 51 |
+
const page = await browser.newPage();
|
| 52 |
+
|
| 53 |
+
try {
|
| 54 |
+
await page.goto(`https://search.bertina.ir/search?q=${encodeURIComponent(query)}`, {
|
| 55 |
+
waitUntil: 'domcontentloaded', // ✅ منتظر بارگذاری کامل نمیماند
|
| 56 |
+
timeout: 45000 // ✅ ۴۵ ثانیه فرصت
|
| 57 |
});
|
| 58 |
|
| 59 |
+
const html = await page.content();
|
| 60 |
+
console.log(`✅ Bertina Playwright succeeded`);
|
| 61 |
+
return html;
|
| 62 |
+
|
| 63 |
+
} catch (error) {
|
| 64 |
+
console.error(`❌ Bertina Playwright failed:`, error.message);
|
| 65 |
+
return null;
|
| 66 |
+
} finally {
|
| 67 |
+
await browser.close();
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// ===== استخراج نتایج از HTML =====
|
| 72 |
+
function extractBertinaResults(html) {
|
| 73 |
+
const results = [];
|
| 74 |
+
|
| 75 |
+
// الگوی رایج برای نتایج جستجو در برتینا
|
| 76 |
+
// معمولاً نتایج در داخل <h3><a href="...">...</a></h3> قرار دارند
|
| 77 |
+
const regex = /<h3[^>]*><a[^>]+href="([^"]+)"[^>]*>(.*?)<\/a><\/h3>/gs;
|
| 78 |
+
let match;
|
| 79 |
+
let count = 0;
|
| 80 |
+
|
| 81 |
+
while ((match = regex.exec(html)) !== null && count < 15) {
|
| 82 |
+
const url = match[1];
|
| 83 |
+
let title = match[2].replace(/<[^>]+>/g, '').trim();
|
| 84 |
+
|
| 85 |
+
// فیلتر کردن لینکهای اضافی
|
| 86 |
+
if (title && url && !url.includes('bertina.ir') && !url.includes('javascript:')) {
|
| 87 |
+
results.push({
|
| 88 |
+
title: title.slice(0, 200),
|
| 89 |
+
url: url,
|
| 90 |
+
snippet: '', // برتینا ممکن است توضیحات را در <p> یا <div> قرار دهد
|
| 91 |
+
site: new URL(url).hostname.replace('www.', '')
|
| 92 |
});
|
| 93 |
+
count++;
|
| 94 |
}
|
| 95 |
+
}
|
| 96 |
|
| 97 |
+
// اگر با الگوی بالا چیزی پیدا نشد، از الگوی جایگزین استفاده کن
|
| 98 |
+
if (results.length === 0) {
|
| 99 |
+
const altRegex = /<a[^>]+href="(https?:\/\/[^"]+)"[^>]*>([^<]+)<\/a>/gs;
|
| 100 |
+
while ((match = altRegex.exec(html)) !== null && count < 15) {
|
| 101 |
+
const url = match[1];
|
| 102 |
+
const title = match[2].trim();
|
| 103 |
+
if (title && url && !url.includes('bertina.ir') && !url.includes('javascript:')) {
|
| 104 |
+
results.push({
|
| 105 |
+
title: title.slice(0, 200),
|
| 106 |
+
url: url,
|
| 107 |
+
snippet: '',
|
| 108 |
+
site: new URL(url).hostname.replace('www.', '')
|
| 109 |
+
});
|
| 110 |
+
count++;
|
| 111 |
+
}
|
| 112 |
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
return results;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// ===== جستجوی اصلی =====
|
| 119 |
+
async function search(query) {
|
| 120 |
+
// ۱. کش
|
| 121 |
+
const cached = cache.get(query);
|
| 122 |
+
if (cached && Date.now() - cached.timestamp < CACHE_TTL * 1000) {
|
| 123 |
+
console.log(`✅ Cache hit for "${query}"`);
|
| 124 |
+
return cached.results;
|
| 125 |
+
}
|
| 126 |
|
| 127 |
+
console.log(`🔍 Searching Bertina for: "${query}"`);
|
| 128 |
+
|
| 129 |
+
let html = null;
|
| 130 |
+
let results = [];
|
| 131 |
+
|
| 132 |
+
// ۲. اول fetch مستقیم
|
| 133 |
+
html = await searchBertinaDirect(query);
|
| 134 |
+
|
| 135 |
+
// ۳. اگر fetch جواب نداد، Playwright
|
| 136 |
+
if (!html) {
|
| 137 |
+
html = await searchBertinaPlaywright(query);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// ۴. استخراج نتایج
|
| 141 |
+
if (html) {
|
| 142 |
+
results = extractBertinaResults(html);
|
| 143 |
+
console.log(`✅ Extracted ${results.length} results from Bertina`);
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
// ۵. اگر هیچ نتیجهای نبود
|
| 147 |
+
if (results.length === 0) {
|
| 148 |
+
results = [{
|
| 149 |
+
title: 'نتیجهای یافت نشد',
|
| 150 |
+
url: `https://search.bertina.ir/search?q=${encodeURIComponent(query)}`,
|
| 151 |
+
snippet: `برای "${query}" نتیجهای پیدا نشد. برای جستجوی مستقیم در برتینا کلیک کنید.`,
|
| 152 |
+
site: 'bertina.ir'
|
| 153 |
+
}];
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
cache.set(query, { results, timestamp: Date.now() });
|
| 157 |
+
return results;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
// ===== مسیر جستجو =====
|
| 161 |
+
app.get('/search', async (req, res) => {
|
| 162 |
+
const query = req.query.q;
|
| 163 |
+
if (!query) {
|
| 164 |
+
return res.status(400).json({ error: 'Missing query parameter' });
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
try {
|
| 168 |
+
const results = await search(query);
|
| 169 |
res.json({
|
| 170 |
query,
|
| 171 |
count: results.length,
|
| 172 |
results,
|
| 173 |
+
source: 'bertina'
|
| 174 |
});
|
|
|
|
| 175 |
} catch (error) {
|
| 176 |
+
console.error(`❌ Error:`, error.message);
|
| 177 |
res.status(500).json({
|
| 178 |
error: error.message,
|
| 179 |
query,
|
| 180 |
count: 0,
|
| 181 |
results: []
|
| 182 |
});
|
|
|
|
|
|
|
| 183 |
}
|
| 184 |
});
|
| 185 |
|
| 186 |
+
// ===== مسیر اصلی =====
|
| 187 |
+
app.get('/', (req, res) => {
|
| 188 |
+
res.json({
|
| 189 |
+
status: 'running',
|
| 190 |
+
usage: '/search?q=YOUR_QUERY',
|
| 191 |
+
note: 'Search powered by Bertina (with fetch + Playwright fallback)'
|
| 192 |
+
});
|
| 193 |
+
});
|
| 194 |
+
|
| 195 |
app.listen(PORT, '0.0.0.0', () => {
|
| 196 |
console.log(`🚀 Server running on port ${PORT}`);
|
| 197 |
console.log(`📍 Test: http://localhost:${PORT}/search?q=SnappFood`);
|