TheHamkerCat 5 лет назад
Родитель
Сommit
c1c3fd5bae
2 измененных файлов с 32 добавлено и 65 удалено
  1. 25 44
      wbb/modules/paste.py
  2. 7 21
      wbb/utils/pastebin.py

+ 25 - 44
wbb/modules/paste.py

@@ -26,72 +26,53 @@ import os
 import re
 import re
 
 
 import aiofiles
 import aiofiles
-from pykeyboard import InlineKeyboard
 from pyrogram import filters
 from pyrogram import filters
-from pyrogram.types import InlineKeyboardButton
 
 
-from wbb import aiohttpsession as session
 from wbb import app
 from wbb import app
 from wbb.core.decorators.errors import capture_err
 from wbb.core.decorators.errors import capture_err
+from wbb.core.keyboard import ikb
 from wbb.utils.pastebin import paste
 from wbb.utils.pastebin import paste
 
 
 __MODULE__ = "Paste"
 __MODULE__ = "Paste"
-__HELP__ = "/paste - To Paste Replied Text Or Document To Nekobin"
+__HELP__ = "/paste - To Paste Replied Text Or Document To A Pastebin"
 pattern = re.compile(
 pattern = re.compile(
     r"^text/|json$|yaml$|xml$|toml$|x-sh$|x-shellscript$"
     r"^text/|json$|yaml$|xml$|toml$|x-sh$|x-shellscript$"
 )
 )
 
 
 
 
-async def isPreviewUp(preview: str) -> bool:
-    for _ in range(7):
-        try:
-            async with session.head(preview, timeout=2) as resp:
-                status = resp.status
-                size = resp.content_length
-        except asyncio.exceptions.TimeoutError:
-            return False
-        if status == 404 or (status == 200 and size == 0):
-            await asyncio.sleep(0.4)
-        else:
-            return True if status == 200 else False
-    return False
-
-
 @app.on_message(filters.command("paste") & ~filters.edited)
 @app.on_message(filters.command("paste") & ~filters.edited)
 @capture_err
 @capture_err
 async def paste_func(_, message):
 async def paste_func(_, message):
     if not message.reply_to_message:
     if not message.reply_to_message:
-        return await message.reply_text(
-            "Reply To A Message With /paste"
+        return await message.reply("Reply To A Message With /paste")
+    r = message.reply_to_message
+
+    if not r.text and not r.document:
+        return await message.reply(
+            "Only text and documents are supported."
         )
         )
-    m = await message.reply_text("Pasting...")
-    if message.reply_to_message.text:
-        content = str(message.reply_to_message.text)
-    elif message.reply_to_message.document:
-        document = message.reply_to_message.document
-        if document.file_size > 1048576:
+
+    m = await message.reply("Pasting...")
+
+    if r.text:
+        content = str(r.text)
+    elif r.document:
+        if r.document.file_size > 40000:
             return await m.edit(
             return await m.edit(
-                "You can only paste files smaller than 1MB."
+                "You can only paste files smaller than 40KB."
             )
             )
-        if not pattern.search(document.mime_type):
+        if not pattern.search(r.document.mime_type):
             return await m.edit("Only text files can be pasted.")
             return await m.edit("Only text files can be pasted.")
         doc = await message.reply_to_message.download()
         doc = await message.reply_to_message.download()
         async with aiofiles.open(doc, mode="r") as f:
         async with aiofiles.open(doc, mode="r") as f:
             content = await f.read()
             content = await f.read()
         os.remove(doc)
         os.remove(doc)
-    else:
-        return await message.reply("Reply to a text message or DOC.")
-    link = await paste(content)
-    preview = link + "/preview.png"
-    button = InlineKeyboard(row_width=1)
-    button.add(InlineKeyboardButton(text="Paste Link", url=link))
 
 
-    if await isPreviewUp(preview):
-        try:
-            await message.reply_photo(
-                photo=preview, quote=False, reply_markup=button
-            )
-            return await m.delete()
-        except Exception:
-            pass
-    return await m.edit(link)
+    link = await paste(content)
+    kb = ikb({"Paste Link": link})
+    try:
+        await message.reply_photo(
+            photo=link, quote=False, reply_markup=kb
+        )
+    except Exception:
+        await message.reply("Here's your paste", reply_markup=kb)

+ 7 - 21
wbb/utils/pastebin.py

@@ -21,27 +21,13 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 SOFTWARE.
 """
 """
-import socket
-from asyncio import get_running_loop
-from functools import partial
+from wbb.utils.http import post
 
 
+BASE = "https://batbin.me/"
 
 
-def _netcat(host, port, content):
-    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    s.connect((host, port))
-    s.sendall(content.encode())
-    s.shutdown(socket.SHUT_WR)
-    while True:
-        data = s.recv(4096).decode("utf-8").strip("\n\x00")
-        if not data:
-            break
-        return data
-    s.close()
 
 
-
-async def paste(content):
-    loop = get_running_loop()
-    link = await loop.run_in_executor(
-        None, partial(_netcat, "ezup.dev", 9999, content)
-    )
-    return link
+async def paste(content: str):
+    resp = await post(f"{BASE}api/paste", data={"content": content})
+    if not resp["status"]:
+        return
+    return BASE + resp["message"]