NextAITeam commited on
Commit
5d3b7d0
·
verified ·
1 Parent(s): 3ca805b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +35 -161
server.js CHANGED
@@ -1,170 +1,44 @@
1
- import express from "express";
2
 
3
- const app = express();
4
- const PORT = 7860;
 
5
 
6
- // ===== کش =====
7
- const CACHE_TTL = 3600;
8
- const cache = new Map();
9
-
10
- // ===== لیست نمونه‌های عمومی Whoogle =====
11
- const WHOOGLE_INSTANCES = [
12
- "https://whoogle.uesugi.eu",
13
- "https://whoogle.pussthecat.org",
14
- "https://whoogle.foss.wtf",
15
- "https://search.ngn.tf",
16
- "https://whoogle.priv.pw",
17
- "https://whoogle.suds.one"
18
- ];
19
-
20
- // ===== جستجو با Whoogle =====
21
- async function searchWhoogle(query) {
22
- for (const instance of WHOOGLE_INSTANCES) {
23
- try {
24
- const url = `${instance}/search?q=${encodeURIComponent(query)}&format=json`;
25
- console.log(`🔍 Trying Whoogle: ${instance}`);
26
-
27
- const response = await fetch(url, {
28
- signal: AbortSignal.timeout(10000),
29
- headers: {
30
- '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'
31
- }
32
- });
33
-
34
- if (!response.ok) {
35
- console.log(`⚠️ ${instance} returned ${response.status}`);
36
- continue;
37
- }
38
-
39
- const data = await response.json();
40
-
41
- if (data.results && data.results.length > 0) {
42
- console.log(`✅ Whoogle (${instance}) found ${data.results.length} results`);
43
- return data.results.slice(0, 15).map(item => ({
44
- title: item.title || "No title",
45
- url: item.url || "#",
46
- snippet: item.description || item.snippet || "",
47
- site: new URL(item.url || "#").hostname.replace("www.", "") || "unknown"
48
- }));
49
- }
50
- } catch (e) {
51
- console.log(`❌ Whoogle ${instance} failed:`, e.message);
52
- }
53
- }
54
- return [];
55
- }
56
-
57
- // ===== جستجوی اصلی =====
58
- async function search(query) {
59
- // ۱. کش
60
- const cached = cache.get(query);
61
- if (cached && Date.now() - cached.timestamp < CACHE_TTL * 1000) {
62
- console.log(`✅ Cache hit for "${query}"`);
63
- return cached.results;
64
- }
65
-
66
- console.log(`🔍 Searching for: "${query}"`);
67
-
68
- let results = [];
69
-
70
- // ۲. Whoogle (منبع اصلی)
71
  try {
72
- results = await searchWhoogle(query);
73
- } catch (e) {
74
- console.log(`❌ Whoogle error:`, e.message);
75
- }
76
-
77
- // ۳. اگر Whoogle نتیجه‌ای نداشت، Bing Scraping (پشتیبان)
78
- if (results.length === 0) {
79
- console.log(`🔄 Whoogle no results, trying Bing...`);
80
- try {
81
- // کد Bing Scraping (همان قبلی)
82
- const bingUrl = `https://www.bing.com/search?q=${encodeURIComponent(query)}&count=10`;
83
- const bingResponse = await fetch(bingUrl, {
84
- headers: {
85
- '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',
86
- 'Accept': 'text/html'
87
- }
 
 
 
 
 
 
88
  });
89
- if (bingResponse.ok) {
90
- const html = await bingResponse.text();
91
- const regex = /<li class="b_algo"[^>]*>.*?<h2><a href="([^"]+)"[^>]*>(.*?)<\/a><\/h2>.*?<p>(.*?)<\/p>/gs;
92
- let match;
93
- const bingResults = [];
94
- while ((match = regex.exec(html)) !== null && bingResults.length < 10) {
95
- const url = match[1];
96
- const title = match[2].replace(/<[^>]+>/g, '').trim();
97
- const snippet = match[3].replace(/<[^>]+>/g, '').trim();
98
- if (title && url && !url.includes('bing.com') && !url.includes('microsoft.com')) {
99
- bingResults.push({
100
- title: title,
101
- url: url,
102
- snippet: snippet || 'No description available',
103
- site: new URL(url).hostname.replace('www.', '')
104
- });
105
- }
106
- }
107
- if (bingResults.length > 0) {
108
- results = bingResults;
109
- console.log(`✅ Bing found ${results.length} results`);
110
- }
111
- }
112
- } catch (e) {
113
- console.log(`❌ Bing fallback failed:`, e.message);
114
- }
115
- }
116
-
117
- // ۴. اگر هیچ نتیجه‌ای نبود، یک پیام مفید برگردان
118
- if (results.length === 0) {
119
- results = [{
120
- title: "No results found",
121
- url: "#",
122
- snippet: `No results found for "${query}". Try searching on Whoogle: https://whoogle.uesugi.eu/search?q=${encodeURIComponent(query)}`,
123
- site: "system"
124
- }];
125
- }
126
-
127
- cache.set(query, { results, timestamp: Date.now() });
128
- return results;
129
- }
130
 
131
- // ===== مسیر جستجو =====
132
- app.get("/search", async (req, res) => {
133
- const query = req.query.q;
134
- if (!query) {
135
- return res.status(400).json({ error: "Missing parameter q" });
136
- }
137
 
138
- try {
139
- const results = await search(query);
140
- res.json({
141
- query,
142
- count: results.length,
143
- results,
144
- source: results[0]?.site === "system" ? "none" : "whoogle"
145
- });
146
  } catch (error) {
147
- console.error(`❌ Error:`, error.message);
148
- res.status(500).json({
149
- error: error.message,
150
- query,
151
- count: 0,
152
- results: []
153
- });
154
  }
155
- });
156
-
157
- // ===== مسیر اصلی =====
158
- app.get("/", (req, res) => {
159
- res.json({
160
- status: "running",
161
- usage: "/search?q=YOUR_QUERY",
162
- note: "Search powered by Whoogle (Google results) + Bing fallback"
163
- });
164
- });
165
 
166
- // ===== راه‌اندازی سرور =====
167
- app.listen(PORT, "0.0.0.0", () => {
168
- console.log(`🚀 Server running on port ${PORT}`);
169
- console.log(`📍 Test: http://localhost:${PORT}/search?q=SnappFood`);
170
- });
 
1
+ const { chromium } = require('playwright');
2
 
3
+ async function searchZarebin(query) {
4
+ const browser = await chromium.launch({ headless: false }); // برای مشاهده مرورگر، headless: false
5
+ const page = await browser.newPage();
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  try {
8
+ // 1. رفتن به صفحه اصلی موتور جستجو
9
+ await page.goto('https://zarebin.ir');
10
+
11
+ // 2. پیدا کردن باکس جستجو و وارد کردن عبارت (سلکتورها باید دقیق باشند)
12
+ // این سلکتور یک فرض است و باید با سلکتور واقعی در وب‌سایت جایگزین شود
13
+ await page.fill('input[type="text"]', query);
14
+ await page.click('button[type="submit"]'); // یا فشار دادن کلید Enter
15
+
16
+ // 3. منتظر ماندن برای بارگذاری نتایج
17
+ await page.waitForSelector('.result-item'); // یک سلکتور برای نتایج
18
+
19
+ // 4. استخراج داده‌های مورد نظر (عنوان، لینک، توضیحات)
20
+ const results = await page.$$eval('.result-item', items => {
21
+ return items.map(item => {
22
+ const titleElement = item.querySelector('h3 a');
23
+ const linkElement = item.querySelector('a');
24
+ const descElement = item.querySelector('p');
25
+ return {
26
+ title: titleElement ? titleElement.innerText : null,
27
+ url: linkElement ? linkElement.href : null,
28
+ description: descElement ? descElement.innerText : null,
29
+ };
30
  });
31
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ console.log(results);
34
+ return results;
 
 
 
 
35
 
 
 
 
 
 
 
 
 
36
  } catch (error) {
37
+ console.error('Error during search:', error);
38
+ } finally {
39
+ await browser.close();
 
 
 
 
40
  }
41
+ }
 
 
 
 
 
 
 
 
 
42
 
43
+ // اجرای تابع
44
+ searchZarebin('پایتون');