paste.py 3.4 KB

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