Преглед изворни кода

Ytdl cc | Paste webpagecurlfailed

TheHamkerCat пре 5 година
родитељ
комит
18d396a48b

+ 0 - 1
.dockerignore

@@ -7,7 +7,6 @@ cryptography
 dnspython
 future
 googletrans==4.0.0-rc1
-hurry.filesize
 motor
 pillow
 psutil

+ 12 - 6
runtime.txt

@@ -26,6 +26,7 @@ import asyncio
 import time
 from os import path
 
+from aiohttp import ClientSession
 from motor.motor_asyncio import AsyncIOMotorClient as MongoClient
 from pyrogram import Client
 from pyromod import listen
@@ -52,7 +53,7 @@ MOD_NOLOAD = []
 bot_start_time = time.time()
 
 # MongoDB client
-print("[INFO]: LOADING MONGODB")
+print("[INFO]: INITIALIZING DATABASE")
 mongo_client = MongoClient(MONGO_DB_URI)
 db = mongo_client.wbb
 
@@ -76,24 +77,28 @@ loop = asyncio.get_event_loop()
 loop.run_until_complete(load_sudoers())
 
 if not HEROKU:
-    print("[INFO]: LOADING USERBOT CLIENT")
+    print("[INFO]: INITIALIZING USERBOT CLIENT")
     app2 = Client(
         "userbot", phone_number=PHONE_NUMBER, api_id=API_ID, api_hash=API_HASH
     )
 else:
-    print("[INFO]: LOADING USERBOT CLIENT")
+    print("[INFO]: INITIALIZING USERBOT CLIENT")
     app2 = Client(SESSION_STRING, api_id=API_ID, api_hash=API_HASH)
 
 # Bot client
-print("[INFO]: LOADING BOT CLIENT")
+print("[INFO]: INITIALIZING BOT CLIENT")
 app = Client("wbb", bot_token=BOT_TOKEN, api_id=API_ID, api_hash=API_HASH)
 # ARQ client
-print("[INFO]: LOADING ARQ")
+print("[INFO]: INITIALIZING ARQ")
 arq = ARQ(ARQ_API_URL, ARQ_API_KEY)
 # Telegraph client
-print("[INFO]: LOADING TELEGRAPH")
+print("[INFO]: INITIALIZING TELEGRAPH")
 telegraph = Telegraph()
 telegraph.create_account(short_name="wbb")
+# Aiohttp Client
+print("[INFO]: INITIALZING AIOHTTP SESSION")
+aiohttpsession = ClientSession()
+
 
 BOT_ID = 0
 BOT_NAME = ""
@@ -135,6 +140,7 @@ print("[INFO]: STARTING BOT CLIENT")
 app.start()
 print("[INFO]: STARTING USERBOT CLIENT")
 app2.start()
+print("[INFO]: LOADING UB/BOT PROFILE INFO")
 get_info(app, app2)
 
 if USERBOT_ID not in SUDOERS:

+ 2 - 1
wbb/__main__.py

@@ -29,7 +29,7 @@ import uvloop
 from pyrogram import filters, idle
 from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
 
-from wbb import BOT_NAME, BOT_USERNAME, USERBOT_NAME, app
+from wbb import BOT_NAME, BOT_USERNAME, USERBOT_NAME, aiohttpsession, app
 from wbb.modules import ALL_MODULES
 from wbb.modules.sudoers import bot_sys_stats
 from wbb.utils import paginate_modules
@@ -70,6 +70,7 @@ async def start_bot():
     print(f"[INFO]: BOT STARTED AS {BOT_NAME}!")
     print(f"[INFO]: USERBOT STARTED AS {USERBOT_NAME}!")
     await idle()
+    await aiohttpsession.close()
 
 
 @app.on_message(filters.command(["help", "start"]))

+ 29 - 3
wbb/core/decorators/errors.py

@@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 """
 import asyncio
+import traceback
 
 from pyrogram import filters
 from pyrogram.types import ChatPermissions
@@ -160,8 +161,9 @@ async def purge(client, message):
             await message.reply_text("Your Don't Have Enough Permissions!")
         await message.delete()
     except Exception as e:
+        await message.reply_text(str(e))
+        e = traceback.format_exc()
         print(e)
-        await message.reply_text(e)
 
 
 # Kick members
@@ -197,8 +199,9 @@ async def kick(_, message):
             await message.reply_to_message.chat.unban_member(user_id)
             await message.reply_text("Kicked!")
     except Exception as e:
+        await message.reply_text(str(e))
+        e = traceback.format_exc()
         print(e)
-        await message.reply_text(e)
 
 
 # Ban members
@@ -233,6 +236,8 @@ async def ban(_, message):
             await message.reply_text("Banned!")
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Unban members
@@ -264,6 +269,8 @@ async def unban(_, message):
         await message.reply_text("Unbanned!")
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Delete messages
@@ -292,6 +299,8 @@ async def delete(_, message):
         await message.delete()
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Promote Members
@@ -336,6 +345,8 @@ async def promote(_, message):
 
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Demote Member
@@ -379,6 +390,8 @@ async def demote(_, message):
 
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Pin Messages
@@ -401,6 +414,8 @@ async def pin(_, message):
             return
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Mute members
@@ -437,6 +452,8 @@ async def mute(_, message):
         await message.reply_text("Muted!")
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Unmute members
@@ -468,6 +485,8 @@ async def unmute(_, message):
         await message.reply_text("Unmuted!")
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Ban deleted accounts
@@ -503,7 +522,8 @@ async def ban_deleted_accounts(_, message):
             await message.reply_text("You Don't Have Enough Permissions")
     except Exception as e:
         await message.reply_text(str(e))
-        print(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 @app.on_message(filters.command("warn") & ~filters.edited)
@@ -565,6 +585,8 @@ async def warn_user(_, message):
             await message.reply_text("You don't have enough permissions.")
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Rmwarns
@@ -597,6 +619,8 @@ async def remove_warnings(_, message):
             await message.reply_text("You don't have enough permissions")
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Warns
@@ -637,6 +661,8 @@ async def check_warns(_, message):
         )
     except Exception as e:
         await message.reply_text(str(e))
+        e = traceback.format_exc()
+        print(e)
 
 
 # Report

+ 2 - 2
wbb/modules/admin_misc.py

@@ -131,7 +131,7 @@ async def downvote(_, message):
 async def karma(_, message):
     chat_id = message.chat.id
     if not message.reply_to_message:
-        m = await message.reply_text("Analyzing Karma...Will Take 20 Seconds")
+        m = await message.reply_text("Analyzing Karma...Will Take 10 Seconds")
         karma = await get_karmas(chat_id)
         msg = f"**Karma list of {message.chat.title}:- **\n"
         limit = 0
@@ -153,7 +153,7 @@ async def karma(_, message):
                 break
             try:
                 user = await app.get_users(int(user_idd))
-                await asyncio.sleep(2)
+                await asyncio.sleep(0.8)
             except Exception:
                 continue
             first_name = user.first_name

+ 29 - 22
wbb/modules/locks.py

@@ -25,8 +25,9 @@ import asyncio
 import os
 
 from pyrogram import filters
+from pyrogram.errors.exceptions.bad_request_400 import WebpageCurlFailed
 
-from wbb import app
+from wbb import aiohttpsession, app
 from wbb.core.decorators.errors import capture_err
 from wbb.utils.functions import get_http_status_code
 from wbb.utils.pastebin import paste
@@ -35,26 +36,36 @@ __MODULE__ = "Paste"
 __HELP__ = "/paste - To Paste Replied Text Or Document To Nekobin"
 
 
+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)
+        elif status == 200 and size > 0:
+            return True
+    return False
+
+
 @app.on_message(filters.command("paste") & ~filters.edited)
 @capture_err
 async def paste_func(_, message):
     if message.reply_to_message:
-        app.set_parse_mode("markdown")
         if message.reply_to_message.text:
             m = await message.reply_text("Pasting...")
             content = str(message.reply_to_message.text)
             link = await paste(content)
             preview = link + "/preview.png"
-            status_code = await get_http_status_code(preview)
-            i = 0
-            while status_code != 200:
-                if i == 5:
-                    break
-                status_code = await get_http_status_code(preview)
-                await asyncio.sleep(0.2)
-                i += 1
-            await m.delete()
-            await app.send_photo(message.chat.id, photo=preview, caption=link)
+            if await isPreviewUp(preview):
+                await message.reply_photo(
+                    photo=preview, caption=link, quote=false
+                )
+                return
+            await m.edit(link)
 
         elif message.reply_to_message.document:
             if message.reply_to_message.document.file_size > 1048576:
@@ -69,16 +80,12 @@ async def paste_func(_, message):
             i = open(doc_file, "r")
             link = await paste(i.read())
             preview = link + "/preview.png"
-            status_code = await get_http_status_code(preview)
-            i = 0
-            while status_code != 200:
-                if i == 5:
-                    break
-                status_code = await get_http_status_code(preview)
-                await asyncio.sleep(0.2)
-                i += 1
-            await m.delete()
-            await app.send_photo(message.chat.id, photo=preview, caption=link)
+            if await isPreviewUp(preview):
+                await message.reply_photo(
+                    photo=preview, caption=link, quote=false
+                )
+                return
+            await m.edit(link)
             os.remove(doc_file)
     else:
         await message.reply_text("Reply To A Message With /paste")

+ 3 - 2
wbb/modules/pipes.py

@@ -1,3 +1,4 @@
+import traceback
 from asyncio import gather
 from os import remove
 from random import randint
@@ -5,7 +6,7 @@ from random import randint
 from pykeyboard import InlineKeyboard
 from pyrogram import filters
 from pyrogram.types import InlineKeyboardButton
-import traceback
+
 from wbb import app, arq
 from wbb.core.decorators.errors import capture_err
 from wbb.utils.formatter import convert_seconds_to_minutes as timeFormat
@@ -78,7 +79,7 @@ async def ytdlCallback(_, cq):
         format = data["format"]
         size = data["size"]
         thumbnail = data["thumbnail"]
-        cc = data['cc']
+        cc = data["cc"]
         caption = f"""
 **Title:** {title}
 **Size:** {size}

+ 3 - 1
wbb/utils/__init__.py

@@ -247,7 +247,9 @@ async def get_karmas_count() -> dict:
     karmas_count = 0
     for chat in await chats.to_list(length=1000000):
         for i in chat["karma"]:
-            karmas_count += chat["karma"][i]["karma"]
+            karma_ = chat["karma"][i]["karma"]
+            if karma_ > 0:
+                karmas_count += karma_
         chats_count += 1
     return {"chats_count": chats_count, "karmas_count": karmas_count}
 

+ 7 - 8
wbb/utils/fetch.py

@@ -21,7 +21,7 @@ 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
 SOFTWARE.
 """
-import aiohttp
+from wbb import aiohttpsession
 
 headers = {
     "Accept": "application/json",
@@ -29,11 +29,10 @@ headers = {
 }
 
 
-async def fetch(url):
-    async with aiohttp.ClientSession() as session:
-        async with session.get(url, headers=headers) as resp:
-            try:
-                data = await resp.json()
-            except Exception:
-                data = await resp.text()
+async def fetch(url: str):
+    async with aiohttpsession.get(url, headers=headers) as resp:
+        try:
+            data = await resp.json()
+        except Exception:
+            data = await resp.text()
     return data

+ 1 - 1
wbb/utils/files.py

@@ -32,9 +32,9 @@ import aiohttp
 import speedtest
 from carbonnow import Carbon
 from PIL import Image, ImageDraw, ImageFilter, ImageFont
+from wget import download
 
 from wbb.utils import aiodownloader
-from wget import download
 from wbb.utils.fetch import fetch
 
 """

+ 0 - 0
wbb/utils/inlinefuncs.py