paste.py 3.4 KB

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