Spaces:
Paused
Paused
CBV01 commited on
Commit ·
e64eb90
1
Parent(s): e02925c
Restrict deep SearchGlobalRequest to base query to save API calls
Browse files- backend/main.py +27 -19
backend/main.py
CHANGED
|
@@ -1273,7 +1273,8 @@ async def _scrape_telegram_search(base_query: str, rows: list, queue: Optional[a
|
|
| 1273 |
await queue.put({"type": "result", "layer": 1, "data": item}) # type: ignore
|
| 1274 |
|
| 1275 |
# Phrase-based Global Message Search (Discovery Hack)
|
| 1276 |
-
|
|
|
|
| 1277 |
from telethon.tl.functions.messages import SearchGlobalRequest # type: ignore
|
| 1278 |
from telethon.tl.types import InputMessagesFilterEmpty # type: ignore
|
| 1279 |
try:
|
|
@@ -1284,32 +1285,39 @@ async def _scrape_telegram_search(base_query: str, rows: list, queue: Optional[a
|
|
| 1284 |
max_date=None,
|
| 1285 |
offset_id=0,
|
| 1286 |
offset_peer=InputPeerEmpty(),
|
| 1287 |
-
limit=
|
| 1288 |
))
|
| 1289 |
for msg in msg_results.messages:
|
| 1290 |
try:
|
| 1291 |
-
|
|
|
|
|
|
|
|
|
|
| 1292 |
if not chat: continue
|
| 1293 |
cid = str(getattr(chat, 'id', '0'))
|
|
|
|
| 1294 |
if cid not in unique_groups:
|
| 1295 |
msg_username = getattr(chat, 'username', None)
|
| 1296 |
is_member = not getattr(chat, 'left', True)
|
| 1297 |
-
|
| 1298 |
-
|
| 1299 |
-
|
| 1300 |
-
|
| 1301 |
-
|
| 1302 |
-
|
| 1303 |
-
|
| 1304 |
-
|
| 1305 |
-
|
| 1306 |
-
|
| 1307 |
-
|
| 1308 |
-
|
| 1309 |
-
|
| 1310 |
-
|
| 1311 |
-
|
| 1312 |
-
|
|
|
|
|
|
|
|
|
|
| 1313 |
except: continue
|
| 1314 |
except: pass
|
| 1315 |
|
|
|
|
| 1273 |
await queue.put({"type": "result", "layer": 1, "data": item}) # type: ignore
|
| 1274 |
|
| 1275 |
# Phrase-based Global Message Search (Discovery Hack)
|
| 1276 |
+
# Only run this expensive message search on the INITIAL base query, not on every variation suffix!
|
| 1277 |
+
if q == base_query and (len(q.split()) > 1 or any(x in q.lower() for x in ["need", "who", "build", "looking"])):
|
| 1278 |
from telethon.tl.functions.messages import SearchGlobalRequest # type: ignore
|
| 1279 |
from telethon.tl.types import InputMessagesFilterEmpty # type: ignore
|
| 1280 |
try:
|
|
|
|
| 1285 |
max_date=None,
|
| 1286 |
offset_id=0,
|
| 1287 |
offset_peer=InputPeerEmpty(),
|
| 1288 |
+
limit=15
|
| 1289 |
))
|
| 1290 |
for msg in msg_results.messages:
|
| 1291 |
try:
|
| 1292 |
+
# Use cached .chat if available, fallback to get_chat()
|
| 1293 |
+
chat = getattr(msg, 'chat', None)
|
| 1294 |
+
if chat is None and hasattr(msg, 'get_chat'):
|
| 1295 |
+
chat = await msg.get_chat()
|
| 1296 |
if not chat: continue
|
| 1297 |
cid = str(getattr(chat, 'id', '0'))
|
| 1298 |
+
|
| 1299 |
if cid not in unique_groups:
|
| 1300 |
msg_username = getattr(chat, 'username', None)
|
| 1301 |
is_member = not getattr(chat, 'left', True)
|
| 1302 |
+
# Only include if it's a valid public or private group/channel
|
| 1303 |
+
if getattr(chat, 'broadcast', False) or getattr(chat, 'megagroup', False) or msg_username:
|
| 1304 |
+
item = {
|
| 1305 |
+
"id": cid,
|
| 1306 |
+
"title": getattr(chat, 'title', 'Unknown'),
|
| 1307 |
+
"username": msg_username,
|
| 1308 |
+
"participants_count": getattr(chat, 'participants_count', 0),
|
| 1309 |
+
"type": 'channel' if getattr(chat, 'broadcast', False) else 'group',
|
| 1310 |
+
"is_private": msg_username is None,
|
| 1311 |
+
"country": "Global",
|
| 1312 |
+
"global_shows": 1,
|
| 1313 |
+
"user_shows": 1,
|
| 1314 |
+
"source": "telegram_msg",
|
| 1315 |
+
"is_member": is_member
|
| 1316 |
+
}
|
| 1317 |
+
unique_groups[cid] = item
|
| 1318 |
+
if queue:
|
| 1319 |
+
await queue.put({"type": "result", "layer": 1, "data": item}) # type: ignore
|
| 1320 |
+
await asyncio.sleep(0.1) # Micro-sleep between chats
|
| 1321 |
except: continue
|
| 1322 |
except: pass
|
| 1323 |
|