parse_preview.py 1.9 KB

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