paste.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. MIT License
  3. Copyright (c) 2021 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. import os
  22. import re
  23. import aiofiles
  24. from pykeyboard import InlineKeyboard
  25. from pyrogram import filters
  26. from pyrogram.types import InlineKeyboardButton
  27. from wbb import aiohttpsession as session
  28. from wbb import app
  29. from wbb.core.decorators.errors import capture_err
  30. from wbb.utils.pastebin import paste
  31. __MODULE__ = "Paste"
  32. __HELP__ = "/paste - To Paste Replied Text Or Document To Nekobin"
  33. pattern = re.compile(
  34. r"^text/|json$|yaml$|xml$|toml$|x-sh$|x-shellscript$"
  35. )
  36. async def isPreviewUp(preview: str) -> bool:
  37. for _ in range(7):
  38. try:
  39. async with session.head(preview, timeout=2) as resp:
  40. status = resp.status
  41. size = resp.content_length
  42. except asyncio.exceptions.TimeoutError:
  43. return False
  44. if status == 404 or (status == 200 and size == 0):
  45. await asyncio.sleep(0.4)
  46. else:
  47. return True if status == 200 else False
  48. return False
  49. @app.on_message(filters.command("paste") & ~filters.edited)
  50. @capture_err
  51. async def paste_func(_, message):
  52. if not message.reply_to_message:
  53. return await message.reply_text(
  54. "Reply To A Message With /paste"
  55. )
  56. m = await message.reply_text("Pasting...")
  57. if message.reply_to_message.text:
  58. content = str(message.reply_to_message.text)
  59. elif message.reply_to_message.document:
  60. document = message.reply_to_message.document
  61. if document.file_size > 1048576:
  62. return await m.edit(
  63. "You can only paste files smaller than 1MB."
  64. )
  65. if not pattern.search(document.mime_type):
  66. return await m.edit("Only text files can be pasted.")
  67. doc = await message.reply_to_message.download()
  68. async with aiofiles.open(doc, mode="r") as f:
  69. content = await f.read()
  70. os.remove(doc)
  71. else:
  72. return await message.reply("Reply to a text message or DOC.")
  73. link = await paste(content)
  74. preview = link + "/preview.png"
  75. button = InlineKeyboard(row_width=1)
  76. button.add(InlineKeyboardButton(text="Paste Link", url=link))
  77. if await isPreviewUp(preview):
  78. try:
  79. await message.reply_photo(
  80. photo=preview, quote=False, reply_markup=button
  81. )
  82. return await m.delete()
  83. except Exception:
  84. pass
  85. return await m.edit(link)