parse_preview.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from asyncio import sleep
  2. from pyrogram import filters
  3. from pyrogram.types import Message
  4. from wbb import SUDOERS, USERBOT_PREFIX, app2, eor
  5. from wbb.core.sections import section
  6. @app2.on_message(
  7. filters.command("parse_preview", prefixes=USERBOT_PREFIX)
  8. & ~filters.forwarded
  9. & ~filters.via_bot
  10. & SUDOERS
  11. )
  12. async def parse(_, message: Message):
  13. r = message.reply_to_message
  14. has_wpp = False
  15. if not r:
  16. return await eor(message, text="Reply to a message with a webpage")
  17. m_ = await eor(message, text="Parsing...")
  18. if not r.web_page:
  19. text = r.text or r.caption
  20. if text:
  21. m = await app2.send_message("me", text)
  22. await sleep(1)
  23. await m.delete()
  24. if m.web_page:
  25. r = m
  26. has_wpp = True
  27. else:
  28. has_wpp = True
  29. if not has_wpp:
  30. return await m_.edit(
  31. "Replied message has no webpage preview.",
  32. )
  33. wpp = r.web_page
  34. body = {
  35. "Title": [wpp.title or "Null"],
  36. "Description": [(wpp.description[:50] + "...") if wpp.description else "Null"],
  37. "URL": [wpp.display_url or "Null"],
  38. "Author": [wpp.author or "Null"],
  39. "Site Name": [wpp.site_name or "Null"],
  40. "Type": wpp.type or "Null",
  41. }
  42. text = section("Preview", body)
  43. t = wpp.type
  44. if t == "photo":
  45. media = wpp.photo
  46. func = app2.send_photo
  47. elif t == "audio":
  48. media = wpp.audio
  49. func = app2.send_audio
  50. elif t == "video":
  51. media = wpp.video
  52. func = app2.send_video
  53. elif t == "document":
  54. media = wpp.document
  55. func = app2.send_document
  56. else:
  57. media = None
  58. func = None
  59. if media and func:
  60. await m_.delete()
  61. return await func(
  62. m_.chat.id,
  63. media.file_id,
  64. caption=text,
  65. )
  66. await m_.edit(text, disable_web_page_preview=True)