paste.py 3.2 KB

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