CBV01 commited on
Commit
e64eb90
·
1 Parent(s): e02925c

Restrict deep SearchGlobalRequest to base query to save API calls

Browse files
Files changed (1) hide show
  1. 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
- if len(q.split()) > 1 or any(x in q.lower() for x in ["need", "who", "build", "looking"]):
 
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=20
1288
  ))
1289
  for msg in msg_results.messages:
1290
  try:
1291
- chat = await msg.get_chat()
 
 
 
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
- item = {
1298
- "id": cid,
1299
- "title": getattr(chat, 'title', 'Unknown'),
1300
- "username": msg_username,
1301
- "participants_count": getattr(chat, 'participants_count', 0),
1302
- "type": 'channel' if getattr(chat, 'broadcast', False) else 'group',
1303
- "is_private": msg_username is None,
1304
- "country": "Global",
1305
- "global_shows": 1,
1306
- "user_shows": 1,
1307
- "source": "telegram_msg",
1308
- "is_member": is_member
1309
- }
1310
- unique_groups[cid] = item
1311
- if queue:
1312
- await queue.put({"type": "result", "layer": 1, "data": item}) # type: ignore
 
 
 
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