@codebam/cf-workers-telegram-bot 12.6.17 → 12.6.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils.js +44 -1
- package/package.json +1 -1
package/dist/utils.js
CHANGED
|
@@ -165,7 +165,50 @@ export const searchTool = {
|
|
|
165
165
|
// Continue to next fallback
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
|
-
// Fallback to
|
|
168
|
+
// Fallback to DuckDuckGo Lite Search
|
|
169
|
+
try {
|
|
170
|
+
const ddgUrl = 'https://lite.duckduckgo.com/lite/';
|
|
171
|
+
const ddgRes = await fetch(ddgUrl, {
|
|
172
|
+
method: 'POST',
|
|
173
|
+
headers: {
|
|
174
|
+
'User-Agent': userAgent,
|
|
175
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
176
|
+
Accept: 'text/html',
|
|
177
|
+
},
|
|
178
|
+
body: `q=${encodeURIComponent(query)}`,
|
|
179
|
+
});
|
|
180
|
+
if (ddgRes.status === 200 || ddgRes.status === 202) {
|
|
181
|
+
const html = await ddgRes.text();
|
|
182
|
+
const cleanHtml = (str) => str
|
|
183
|
+
.replace(/<[^>]*>/g, '')
|
|
184
|
+
.replace(/ /g, ' ')
|
|
185
|
+
.replace(/\s+/g, ' ')
|
|
186
|
+
.trim();
|
|
187
|
+
const links = [];
|
|
188
|
+
const linkRegex = /<a[^>]*class='result-link'[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/g;
|
|
189
|
+
let match;
|
|
190
|
+
while ((match = linkRegex.exec(html)) !== null) {
|
|
191
|
+
links.push({ url: match[1], title: cleanHtml(match[2]) });
|
|
192
|
+
}
|
|
193
|
+
const snippets = [];
|
|
194
|
+
const snippetRegex = /<td[^>]*class='result-snippet'[^>]*>([\s\S]*?)<\/td>/g;
|
|
195
|
+
while ((match = snippetRegex.exec(html)) !== null) {
|
|
196
|
+
snippets.push(cleanHtml(match[1]));
|
|
197
|
+
}
|
|
198
|
+
if (links.length > 0) {
|
|
199
|
+
const results = links.map((link, i) => ({
|
|
200
|
+
title: link.title,
|
|
201
|
+
url: link.url,
|
|
202
|
+
snippet: snippets[i] || '',
|
|
203
|
+
}));
|
|
204
|
+
return JSON.stringify({ results });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
// Continue to next fallback
|
|
210
|
+
}
|
|
211
|
+
// Fallback to Wikipedia Search if DuckDuckGo Lite is blocked/rate-limited
|
|
169
212
|
try {
|
|
170
213
|
const wikiUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(query)}&utf8=&format=json`;
|
|
171
214
|
const res = await fetch(wikiUrl, {
|