parse_preview.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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": [
  37. (wpp.description[:50] + "...") if wpp.description else "Null"
  38. ],
  39. "URL": [wpp.display_url or "Null"],
  40. "Author": [wpp.author or "Null"],
  41. "Site Name": [wpp.site_name or "Null"],
  42. "Type": wpp.type or "Null",
  43. }
  44. text = section("Preview", body)
  45. t = wpp.type
  46. if t == "photo":
  47. media = wpp.photo
  48. func = app2.send_photo
  49. elif t == "audio":
  50. media = wpp.audio
  51. func = app2.send_audio
  52. elif t == "video":
  53. media = wpp.video
  54. func = app2.send_video
  55. elif t == "document":
  56. media = wpp.document
  57. func = app2.send_document
  58. else:
  59. media = None
  60. func = None
  61. if media and func:
  62. await m_.delete()
  63. return await func(
  64. m_.chat.id,
  65. media.file_id,
  66. caption=text,
  67. )
  68. await m_.edit(text, disable_web_page_preview=True)