parse_preview.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. MIT License
  3. Copyright (c) present TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import asyncio
  21. from pyrogram import filters, enums
  22. from pyrogram.types import Message
  23. from wbb import SUDOERS, USERBOT_PREFIX, app2, eor
  24. from wbb.core.sections import section
  25. @app2.on_message(
  26. filters.command("parse_preview", prefixes=USERBOT_PREFIX)
  27. & SUDOERS
  28. )
  29. async def parse(_, message: Message):
  30. r = message.reply_to_message
  31. has_wpp = False
  32. if not r:
  33. return await eor(message, text="Reply to a message with a webpage")
  34. m_ = await eor(message, text="Parsing...")
  35. if not r.web_page:
  36. text = r.text or r.caption
  37. if text:
  38. m = await app2.send_message("me", text)
  39. await asyncio.sleep(1)
  40. await m.delete()
  41. if m.web_page:
  42. r = m
  43. has_wpp = True
  44. else:
  45. has_wpp = True
  46. if not has_wpp:
  47. return await m_.edit(
  48. "Replied message has no webpage preview.",
  49. )
  50. wpp = r.web_page
  51. body = {
  52. "Title": [wpp.title or "Null"],
  53. "Description": [
  54. (wpp.description[:50] + "...") if wpp.description else "Null"
  55. ],
  56. "URL": [wpp.display_url or "Null"],
  57. "Author": [wpp.author or "Null"],
  58. "Site Name": [wpp.site_name or "Null"],
  59. "Type": wpp.type or "Null",
  60. }
  61. text = section("Preview", body)
  62. t = wpp.type
  63. if t == enums.MessagesFilter.PHOTO:
  64. media = wpp.photo
  65. func = app2.send_photo
  66. elif t == enums.MessagesFilter.AUDIO:
  67. media = wpp.audio
  68. func = app2.send_audio
  69. elif t == enums.MessagesFilter.VIDEO:
  70. media = wpp.video
  71. func = app2.send_video
  72. elif t == enums.MessagesFilter.DOCUMENT:
  73. media = wpp.document
  74. func = app2.send_document
  75. else:
  76. media = None
  77. func = None
  78. if media and func:
  79. await m_.delete()
  80. return await func(
  81. m_.chat.id,
  82. media.file_id,
  83. caption=text,
  84. )
  85. await m_.edit(text, disable_web_page_preview=True)