CBV01 commited on
Commit
049cd25
·
1 Parent(s): 866c437

Enhance Telegram channel/group preview in scraper modal with MTProto live feed fallback, media thumbnail resolving, and polished UI

Browse files
Files changed (3) hide show
  1. .gitignore +4 -0
  2. backend/main.py +180 -12
  3. frontend/src/app/scraper/page.tsx +35 -14
.gitignore CHANGED
@@ -33,3 +33,7 @@ Thumbs.db
33
  .idea/
34
  .gemini/
35
  .claude/
 
 
 
 
 
33
  .idea/
34
  .gemini/
35
  .claude/
36
+
37
+ # Static downloads & caches
38
+ static/
39
+ backend/static/
backend/main.py CHANGED
@@ -35,7 +35,7 @@ except ImportError:
35
  # Telethon Imports
36
  from telethon.sync import TelegramClient # type: ignore
37
  from telethon.sessions import StringSession # type: ignore
38
- from telethon.tl.functions.channels import JoinChannelRequest, GetParticipantsRequest # type: ignore
39
  from telethon.tl.functions.messages import SearchGlobalRequest, CheckChatInviteRequest, ImportChatInviteRequest # type: ignore
40
  from telethon.tl.functions.contacts import SearchRequest # type: ignore
41
  from telethon.tl.types import ChannelParticipantsSearch, InputMessagesFilterEmpty, InputPeerEmpty, InputPeerChannel, InputPeerChat, User # type: ignore
@@ -349,27 +349,195 @@ app.add_middleware(
349
  allow_headers=["*"],
350
  )
351
 
352
- @app.get("/api/telegram/preview")
353
- async def preview_telegram_channel(target: str, user_id: str = Depends(get_current_user_id)):
354
- """Fetches in-app preview info (avatar, title, members, description) for any Telegram channel/group."""
355
- clean_target = target.strip().replace('https://t.me/', '').replace('t.me/', '').replace('@', '').split('/')[0].split('?')[0]
356
- if not clean_target:
357
- raise HTTPException(status_code=400, detail="Target username or link required")
358
-
359
- info = await verify_telegram_info(clean_target)
360
- return {"status": "success", "data": info}
361
-
362
- # Static files for profile photos - resolve both local and root static paths safely
363
  _BASE_DIR = os.path.dirname(os.path.abspath(__file__))
364
  _STATIC_DIR = os.path.join(_BASE_DIR, "static")
365
  _AVATARS_DIR = os.path.join(_STATIC_DIR, "avatars")
 
366
  os.makedirs(_AVATARS_DIR, exist_ok=True)
 
367
  _ROOT_STATIC_DIR = os.path.abspath("static")
368
  if os.path.abspath(_STATIC_DIR) != _ROOT_STATIC_DIR:
369
  os.makedirs(os.path.join(_ROOT_STATIC_DIR, "avatars"), exist_ok=True)
 
370
 
371
  app.mount("/static", StaticFiles(directory=_STATIC_DIR), name="static")
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  # Moved here so 'app' is defined before this route is registered (was crashing import).
374
  @app.get("/api/debug/logs")
375
  async def get_logs(user_id: str = Depends(get_current_admin)):
 
35
  # Telethon Imports
36
  from telethon.sync import TelegramClient # type: ignore
37
  from telethon.sessions import StringSession # type: ignore
38
+ from telethon.tl.functions.channels import JoinChannelRequest, GetParticipantsRequest, GetFullChannelRequest # type: ignore
39
  from telethon.tl.functions.messages import SearchGlobalRequest, CheckChatInviteRequest, ImportChatInviteRequest # type: ignore
40
  from telethon.tl.functions.contacts import SearchRequest # type: ignore
41
  from telethon.tl.types import ChannelParticipantsSearch, InputMessagesFilterEmpty, InputPeerEmpty, InputPeerChannel, InputPeerChat, User # type: ignore
 
349
  allow_headers=["*"],
350
  )
351
 
352
+ # Static files for profile photos and preview post media - resolve both local and root static paths safely
 
 
 
 
 
 
 
 
 
 
353
  _BASE_DIR = os.path.dirname(os.path.abspath(__file__))
354
  _STATIC_DIR = os.path.join(_BASE_DIR, "static")
355
  _AVATARS_DIR = os.path.join(_STATIC_DIR, "avatars")
356
+ _PREVIEWS_DIR = os.path.join(_STATIC_DIR, "previews")
357
  os.makedirs(_AVATARS_DIR, exist_ok=True)
358
+ os.makedirs(_PREVIEWS_DIR, exist_ok=True)
359
  _ROOT_STATIC_DIR = os.path.abspath("static")
360
  if os.path.abspath(_STATIC_DIR) != _ROOT_STATIC_DIR:
361
  os.makedirs(os.path.join(_ROOT_STATIC_DIR, "avatars"), exist_ok=True)
362
+ os.makedirs(os.path.join(_ROOT_STATIC_DIR, "previews"), exist_ok=True)
363
 
364
  app.mount("/static", StaticFiles(directory=_STATIC_DIR), name="static")
365
 
366
+ @app.get("/api/telegram/preview")
367
+ async def preview_telegram_channel(target: str, user_id: str = Depends(get_current_user_id)):
368
+ """Fetches in-app preview info (avatar, title, members, description, live feed posts) for any Telegram channel/group."""
369
+ raw_target = (target or '').strip()
370
+ if not raw_target:
371
+ raise HTTPException(status_code=400, detail="Target username or link required")
372
+
373
+ clean_target = raw_target
374
+ for prefix in ['https://t.me/s/', 'http://t.me/s/', 'https://t.me/', 'http://t.me/', 't.me/s/', 't.me/', '@']:
375
+ if clean_target.lower().startswith(prefix.lower()):
376
+ clean_target = clean_target[len(prefix):]
377
+ clean_target = clean_target.split('?')[0].strip()
378
+ if '/' in clean_target and not ('joinchat' in raw_target or '+' in raw_target):
379
+ clean_target = clean_target.split('/')[0].strip()
380
+
381
+ if not clean_target:
382
+ raise HTTPException(status_code=400, detail="Target username or link required")
383
+
384
+ # 1. First attempt anonymous public web scraping
385
+ info = await verify_telegram_info(clean_target)
386
+
387
+ # 2. If web posts are empty (e.g. groups or channels where t.me/s/ redirects or is disabled), fallback to connected MTProto client
388
+ if not info.get("posts") or len(info.get("posts", [])) == 0:
389
+ try:
390
+ conn = get_db_connection()
391
+ acc_row = None
392
+ try:
393
+ acc_row = conn.execute(
394
+ "SELECT phone_number, user_id FROM accounts WHERE (user_id = ? OR user_id = 'admin_virtual_id') AND status = 'active' LIMIT 1",
395
+ (user_id,)
396
+ ).fetchone()
397
+ if not acc_row:
398
+ acc_row = conn.execute(
399
+ "SELECT phone_number, user_id FROM accounts WHERE status = 'active' LIMIT 1"
400
+ ).fetchone()
401
+ finally:
402
+ if hasattr(conn, "close"): conn.close()
403
+
404
+ if acc_row:
405
+ phone_num = acc_row[0]
406
+ owner_uid = acc_row[1]
407
+ client = await POOL.get_client(owner_uid, phone_num)
408
+ if client and client.is_connected():
409
+ # Handle invite link hashes or standard usernames
410
+ is_invite = '+' in raw_target or 'joinchat' in raw_target
411
+ if is_invite:
412
+ inv_hash = ""
413
+ if '+' in raw_target:
414
+ inv_hash = raw_target.split('+')[-1].split('/')[0].split('?')[0].strip()
415
+ elif 'joinchat/' in raw_target:
416
+ inv_hash = raw_target.split('joinchat/')[-1].split('/')[0].split('?')[0].strip()
417
+ if inv_hash:
418
+ try:
419
+ check_res = await client(CheckChatInviteRequest(inv_hash))
420
+ chat_obj = getattr(check_res, 'chat', None)
421
+ if chat_obj:
422
+ info["title"] = getattr(chat_obj, 'title', info.get("title"))
423
+ info["participants_count"] = getattr(chat_obj, 'participants_count', info.get("participants_count"))
424
+ info["type"] = "channel" if getattr(chat_obj, 'broadcast', False) else "group"
425
+ info["exists"] = True
426
+ except Exception as inv_e:
427
+ log_debug(f"PREVIEW: Invite link check error: {inv_e}")
428
+ else:
429
+ entity = None
430
+ try:
431
+ target_arg = int(clean_target) if clean_target.lstrip('-').isdigit() else clean_target
432
+ entity = await client.get_entity(target_arg)
433
+ except Exception as ent_err:
434
+ log_debug(f"PREVIEW: get_entity failed for {clean_target}: {ent_err}")
435
+
436
+ if entity:
437
+ info["exists"] = True
438
+ if not info.get("title") or info.get("title") == clean_target:
439
+ info["title"] = getattr(entity, 'title', getattr(entity, 'first_name', clean_target))
440
+
441
+ if getattr(entity, 'broadcast', False):
442
+ info["type"] = "channel"
443
+ elif getattr(entity, 'megagroup', False) or getattr(entity, 'gigagroup', False):
444
+ info["type"] = "supergroup"
445
+ else:
446
+ info["type"] = "group"
447
+
448
+ if not info.get("participants_count") or info.get("participants_count") == 0:
449
+ p_cnt = getattr(entity, 'participants_count', 0)
450
+ if not p_cnt:
451
+ try:
452
+ full = await client(GetFullChannelRequest(entity))
453
+ p_cnt = getattr(full.full_chat, 'participants_count', 0)
454
+ except Exception:
455
+ pass
456
+ if p_cnt:
457
+ info["participants_count"] = p_cnt
458
+
459
+ if not info.get("photo_url"):
460
+ try:
461
+ avatar_filename = f"avatar_{clean_target}.jpg"
462
+ avatar_path = os.path.join(_AVATARS_DIR, avatar_filename)
463
+ if not os.path.exists(avatar_path):
464
+ await client.download_profile_photo(entity, file=avatar_path)
465
+ if os.path.exists(avatar_path):
466
+ info["photo_url"] = f"/static/avatars/{avatar_filename}"
467
+ if os.path.abspath(_STATIC_DIR) != _ROOT_STATIC_DIR:
468
+ try:
469
+ import shutil
470
+ shutil.copy2(avatar_path, os.path.join(_ROOT_STATIC_DIR, "avatars", avatar_filename))
471
+ except Exception:
472
+ pass
473
+ except Exception:
474
+ pass
475
+
476
+ # Fetch recent messages directly via MTProto
477
+ try:
478
+ msgs = await client.get_messages(entity, limit=20)
479
+ telethon_posts = []
480
+ media_download_count = 0
481
+ for m in msgs:
482
+ text = (m.message or '').strip()
483
+ date_str = m.date.strftime("%b %d, %Y, %H:%M") if getattr(m, 'date', None) else "Recent"
484
+ views_str = str(m.views) if getattr(m, 'views', None) is not None else ""
485
+
486
+ photo_url = None
487
+ has_media = bool(getattr(m, 'photo', None) or getattr(m, 'video', None) or getattr(m, 'document', None))
488
+ if has_media and media_download_count < 8:
489
+ p_filename = f"post_{clean_target}_{m.id}.jpg"
490
+ p_path = os.path.join(_PREVIEWS_DIR, p_filename)
491
+ try:
492
+ if not os.path.exists(p_path):
493
+ await client.download_media(m, file=p_path, thumb=-1)
494
+ if os.path.exists(p_path):
495
+ photo_url = f"/static/previews/{p_filename}"
496
+ media_download_count += 1
497
+ if os.path.abspath(_STATIC_DIR) != _ROOT_STATIC_DIR:
498
+ try:
499
+ import shutil
500
+ shutil.copy2(p_path, os.path.join(_ROOT_STATIC_DIR, "previews", p_filename))
501
+ except Exception:
502
+ pass
503
+ except Exception as dl_e:
504
+ log_debug(f"PREVIEW: Media download error for {m.id}: {dl_e}")
505
+
506
+ # Enrich text if blank
507
+ if not text:
508
+ if getattr(m, 'file', None) and getattr(m.file, 'name', None):
509
+ text = f"📎 {m.file.name}"
510
+ elif getattr(m, 'poll', None):
511
+ try:
512
+ text = f"📊 Poll: {m.poll.poll.question.text}"
513
+ except Exception:
514
+ text = "📊 Poll"
515
+ elif getattr(m, 'voice', None):
516
+ text = "🎤 Voice message"
517
+ elif getattr(m, 'video_note', None):
518
+ text = "📹 Video note"
519
+ elif getattr(m, 'sticker', None):
520
+ text = "✨ Telegram Sticker"
521
+
522
+ if text or photo_url:
523
+ telethon_posts.append({
524
+ "id": str(m.id),
525
+ "text": text,
526
+ "date": date_str,
527
+ "views": views_str,
528
+ "photo_url": photo_url
529
+ })
530
+
531
+ if telethon_posts:
532
+ info["posts"] = telethon_posts
533
+ log_debug(f"PREVIEW: Telethon loaded {len(telethon_posts)} live messages for {clean_target}")
534
+ except Exception as msgs_e:
535
+ log_debug(f"PREVIEW: Telethon get_messages error for {clean_target}: {msgs_e}")
536
+ except Exception as e:
537
+ log_debug(f"PREVIEW: Telethon fallback error for {clean_target}: {e}")
538
+
539
+ return {"status": "success", "data": info}
540
+
541
  # Moved here so 'app' is defined before this route is registered (was crashing import).
542
  @app.get("/api/debug/logs")
543
  async def get_logs(user_id: str = Depends(get_current_admin)):
frontend/src/app/scraper/page.tsx CHANGED
@@ -135,6 +135,18 @@ export default function ScraperPage() {
135
  const [previewData, setPreviewData] = useState<any | null>(null);
136
  const [isPreviewLoading, setIsPreviewLoading] = useState(false);
137
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  const handleOpenPreview = async (item: ScrapeResult) => {
139
  setPreviewItem(item);
140
  setPreviewData(null);
@@ -1407,8 +1419,8 @@ export default function ScraperPage() {
1407
  <div className="flex items-center justify-between p-4 px-5 border-b border-border/70 bg-gradient-to-r from-indigo-500/10 via-purple-500/5 to-transparent">
1408
  <div className="flex items-center gap-3 min-w-0">
1409
  <div className="w-11 h-11 rounded-xl bg-gradient-to-br from-indigo-500/20 to-purple-500/20 border border-indigo-500/30 flex items-center justify-center text-indigo-400 shrink-0 overflow-hidden shadow-inner">
1410
- {previewData?.photo_url ? (
1411
- <img src={previewData.photo_url} alt="" className="w-full h-full object-cover" />
1412
  ) : (
1413
  <Eye size={20} />
1414
  )}
@@ -1486,12 +1498,12 @@ export default function ScraperPage() {
1486
  )}
1487
  </span>
1488
  <a
1489
- href={`https://t.me/s/${(previewData?.username || previewItem.username || '').replace('@', '')}`}
1490
  target="_blank"
1491
  rel="noopener noreferrer"
1492
  className="text-[10px] text-indigo-400 hover:text-indigo-300 font-mono flex items-center gap-1"
1493
  >
1494
- <span>t.me/s/{(previewData?.username || previewItem.username || '').replace('@', '')}</span>
1495
  <ExternalLink size={10} />
1496
  </a>
1497
  </div>
@@ -1506,12 +1518,13 @@ export default function ScraperPage() {
1506
  previewData.posts.map((post: any, pIdx: number) => (
1507
  <div key={pIdx} className="p-3 rounded-xl bg-card border border-border/60 hover:border-indigo-500/30 transition-all space-y-2 shadow-xs">
1508
  {post.photo_url && (
1509
- <div className="w-full rounded-lg overflow-hidden max-h-56 bg-black/20 flex items-center justify-center">
1510
  <img
1511
- src={post.photo_url}
1512
  alt="Post media"
1513
- className="w-full h-auto max-h-56 object-cover rounded-lg"
1514
  onError={(e) => { (e.target as HTMLElement).style.display = 'none'; }}
 
1515
  />
1516
  </div>
1517
  )}
@@ -1531,17 +1544,25 @@ export default function ScraperPage() {
1531
  </div>
1532
  ))
1533
  ) : (
1534
- <div className="p-8 text-center space-y-2">
1535
- <p className="text-xs font-medium text-foreground/60">
1536
- No public feed posts accessible or this is a closed group with restricted history.
1537
- </p>
 
 
 
 
 
 
 
 
1538
  <a
1539
- href={`https://t.me/${(previewData?.username || previewItem.username || '').replace('@', '')}`}
1540
  target="_blank"
1541
  rel="noopener noreferrer"
1542
- className="inline-flex items-center gap-1 text-xs text-indigo-400 hover:text-indigo-300 font-semibold"
1543
  >
1544
- <span>View Community Directly on Telegram</span>
1545
  <ExternalLink size={12} />
1546
  </a>
1547
  </div>
 
135
  const [previewData, setPreviewData] = useState<any | null>(null);
136
  const [isPreviewLoading, setIsPreviewLoading] = useState(false);
137
 
138
+ const resolvePreviewMedia = (url?: string | null) => {
139
+ if (!url) return null;
140
+ if (url.startsWith('http://') || url.startsWith('https://')) return url;
141
+ try {
142
+ const base = getApiBase();
143
+ const clean = url.startsWith('/') ? url : `/${url}`;
144
+ return `${base}${clean}`;
145
+ } catch {
146
+ return url;
147
+ }
148
+ };
149
+
150
  const handleOpenPreview = async (item: ScrapeResult) => {
151
  setPreviewItem(item);
152
  setPreviewData(null);
 
1419
  <div className="flex items-center justify-between p-4 px-5 border-b border-border/70 bg-gradient-to-r from-indigo-500/10 via-purple-500/5 to-transparent">
1420
  <div className="flex items-center gap-3 min-w-0">
1421
  <div className="w-11 h-11 rounded-xl bg-gradient-to-br from-indigo-500/20 to-purple-500/20 border border-indigo-500/30 flex items-center justify-center text-indigo-400 shrink-0 overflow-hidden shadow-inner">
1422
+ {resolvePreviewMedia(previewData?.photo_url) ? (
1423
+ <img src={resolvePreviewMedia(previewData.photo_url)!} alt="" className="w-full h-full object-cover" />
1424
  ) : (
1425
  <Eye size={20} />
1426
  )}
 
1498
  )}
1499
  </span>
1500
  <a
1501
+ href={`https://t.me/${(previewData?.username || previewItem.username || previewItem.id || '').replace(/^@/, '')}`}
1502
  target="_blank"
1503
  rel="noopener noreferrer"
1504
  className="text-[10px] text-indigo-400 hover:text-indigo-300 font-mono flex items-center gap-1"
1505
  >
1506
+ <span>t.me/{(previewData?.username || previewItem.username || previewItem.id || '').replace(/^@/, '')}</span>
1507
  <ExternalLink size={10} />
1508
  </a>
1509
  </div>
 
1518
  previewData.posts.map((post: any, pIdx: number) => (
1519
  <div key={pIdx} className="p-3 rounded-xl bg-card border border-border/60 hover:border-indigo-500/30 transition-all space-y-2 shadow-xs">
1520
  {post.photo_url && (
1521
+ <div className="w-full rounded-lg overflow-hidden max-h-60 bg-black/20 flex items-center justify-center">
1522
  <img
1523
+ src={resolvePreviewMedia(post.photo_url) || post.photo_url}
1524
  alt="Post media"
1525
+ className="w-full h-auto max-h-60 object-cover rounded-lg cursor-pointer hover:opacity-95 transition-opacity"
1526
  onError={(e) => { (e.target as HTMLElement).style.display = 'none'; }}
1527
+ onClick={() => window.open(resolvePreviewMedia(post.photo_url) || post.photo_url, '_blank')}
1528
  />
1529
  </div>
1530
  )}
 
1544
  </div>
1545
  ))
1546
  ) : (
1547
+ <div className="p-8 text-center space-y-3">
1548
+ <div className="w-10 h-10 rounded-full bg-amber-500/10 text-amber-400 flex items-center justify-center mx-auto border border-amber-500/20">
1549
+ <EyeOff size={18} />
1550
+ </div>
1551
+ <div className="space-y-1">
1552
+ <p className="text-xs font-bold text-foreground">
1553
+ Restricted Community Post History
1554
+ </p>
1555
+ <p className="text-[11px] font-medium text-foreground/50 max-w-sm mx-auto leading-relaxed">
1556
+ This group or channel has restricted past message history from non-members. You can view the community directly on Telegram or extract its members using the button below.
1557
+ </p>
1558
+ </div>
1559
  <a
1560
+ href={`https://t.me/${(previewData?.username || previewItem.username || previewItem.id || '').replace('@', '')}`}
1561
  target="_blank"
1562
  rel="noopener noreferrer"
1563
+ className="inline-flex items-center gap-1.5 px-3.5 py-1.5 rounded-xl bg-indigo-500/10 hover:bg-indigo-500/20 text-xs text-indigo-400 hover:text-indigo-300 font-bold border border-indigo-500/25 transition-all"
1564
  >
1565
+ <span>Open Community on Telegram</span>
1566
  <ExternalLink size={12} />
1567
  </a>
1568
  </div>