Kaynağa Gözat

Removed buggy progress bar from .(up|down)load, Stable now

TheHamkerCat 5 yıl önce
ebeveyn
işleme
d3dd5904e3

+ 1 - 1
wbb/core/sections.py

@@ -36,7 +36,7 @@ def section(
     title: str,
     title: str,
     body: dict,
     body: dict,
     indent: int = 2,
     indent: int = 2,
-    underline: bool = True,
+    underline: bool = False,
 ) -> str:
 ) -> str:
 
 
     text = (bold_ul(title) + n) if underline else bold(title) + n
     text = (bold_ul(title) + n) if underline else bold(title) + n

+ 33 - 35
wbb/modules/download_upload.py

@@ -9,7 +9,6 @@ from wbb import SUDOERS, USERBOT_PREFIX, app2
 from wbb.core.sections import section
 from wbb.core.sections import section
 from wbb.modules.userbot import add_task, eor, rm_task
 from wbb.modules.userbot import add_task, eor, rm_task
 from wbb.utils.downloader import download
 from wbb.utils.downloader import download
-from wbb.utils.functions import progress
 
 
 
 
 @app2.on_message(
 @app2.on_message(
@@ -20,23 +19,28 @@ async def download_func(_, message: Message):
     reply = message.reply_to_message
     reply = message.reply_to_message
     start = time()
     start = time()
     task_id = int(start)
     task_id = int(start)
-    if reply:
-        m = await eor(message, text="Downloading...")
 
 
+    body = {
+        "Started": ctime(start),
+        "Task ID": task_id,
+    }
+    m = await eor(
+        message,
+        text=section("Downloading", body),
+    )
+
+    if reply:
         task = await add_task(
         task = await add_task(
             reply.download,
             reply.download,
             task_id=task_id,
             task_id=task_id,
-            progress=progress,
-            progress_args=(start, task_id, m),
+            task_name="Downloader",
         )
         )
         await task
         await task
         await rm_task(task_id)
         await rm_task(task_id)
 
 
         elapsed = int(time() - start)
         elapsed = int(time() - start)
-        body = {
-            "Started": ctime(start),
-            "Time": f"{elapsed}s",
-        }
+        body["Took"] = f"{elapsed}s"
+
         return await eor(m, text=section("Downloaded", body))
         return await eor(m, text=section("Downloaded", body))
 
 
     text = message.text
     text = message.text
@@ -44,35 +48,19 @@ async def download_func(_, message: Message):
         return await eor(message, text="Invalid Arguments")
         return await eor(message, text="Invalid Arguments")
 
 
     url = text.split(None, 1)[1]
     url = text.split(None, 1)[1]
-    task_id = int(time())
-
-    body = {
-        "Started": ctime(start),
-        "Task ID": task_id,
-        "URL": url,
-    }
-    m = await eor(
-        message,
-        text=section("Downloading", body, underline=False),
-        disable_web_page_preview=True,
-    )
 
 
     try:
     try:
         await download(
         await download(
             url,
             url,
-            progress_func=(progress, [start, task_id, m]),
             task_id=task_id,
             task_id=task_id,
         )
         )
     except Exception as e:
     except Exception as e:
         return await eor(m, text=f"**Error:** `{str(e)}`")
         return await eor(m, text=f"**Error:** `{str(e)}`")
 
 
     elapsed = int(time() - start)
     elapsed = int(time() - start)
-    body = {
-        "Started": ctime(start),
-        "Took": f"{elapsed}s",
-        "Task ID": task_id,
-    }
-    await eor(m, text=section("Downloaded", body, underline=False))
+    body["Took"] = f"{elapsed}s"
+
+    await eor(m, text=section("Downloaded", body))
 
 
 
 
 @app2.on_message(
 @app2.on_message(
@@ -85,31 +73,41 @@ async def upload_func(_, message: Message):
 
 
     url_or_path = message.text.split(None, 1)[1]
     url_or_path = message.text.split(None, 1)[1]
 
 
-    m = await eor(message, text="Uploading..")
     start = time()
     start = time()
     task_id = int(start)
     task_id = int(start)
-    
-    async def upload_file(path):
+
+    body = {
+        "Started": ctime(start),
+        "Task ID": task_id,
+    }
+
+    m = await eor(message, text=section("Uploading", body))
+
+    async def upload_file(path: str):
         task = await add_task(
         task = await add_task(
             message.reply_document,
             message.reply_document,
             task_id,
             task_id,
+            "Uploader",
             path,
             path,
-            progress=progress,
-            progress_args=(start, task_id, m),
         )
         )
+
         await task
         await task
         await rm_task(task_id)
         await rm_task(task_id)
+
         elapsed = int(time() - start)
         elapsed = int(time() - start)
-        return await eor(m, text=f"Uploaded in {elapsed}s")
+        body["Took"] = f"{elapsed}s"
+
+        return await eor(m, text=section("Uploaded", body))
 
 
     try:
     try:
         if isfile(url_or_path):
         if isfile(url_or_path):
             return await upload_file(url_or_path)
             return await upload_file(url_or_path)
+
         path = await download(
         path = await download(
             url_or_path,
             url_or_path,
             task_id=task_id,
             task_id=task_id,
-            progress_func=(progress, [start, task_id, m]),
         )
         )
+
         return await upload_file(path)
         return await upload_file(path)
     except Exception as e:
     except Exception as e:
         return await eor(m, text=f"**Error:** `{str(e)}`")
         return await eor(m, text=f"**Error:** `{str(e)}`")

+ 14 - 6
wbb/modules/userbot.py

@@ -21,6 +21,7 @@ from pyrogram.types import Message, ReplyKeyboardMarkup
 
 
 from wbb import app  # don't remove
 from wbb import app  # don't remove
 from wbb import SUDOERS, USERBOT_PREFIX, app2, arq
 from wbb import SUDOERS, USERBOT_PREFIX, app2, arq
+from wbb.core.sections import section
 
 
 # Eval and Sh module from nana-remix
 # Eval and Sh module from nana-remix
 
 
@@ -49,9 +50,12 @@ async def eor(msg: Message, **kwargs):
     )
     )
 
 
 
 
-async def add_task(taskFunc, task_id, *args, **kwargs):
+async def add_task(taskFunc, task_id, task_name, *args, **kwargs):
     global tasks
     global tasks
-    task = create_task(taskFunc(*args, **kwargs))
+    task = create_task(
+        taskFunc(*args, **kwargs),
+        name=task_name,
+    )
     tasks[task_id] = task
     tasks[task_id] = task
     return task
     return task
 
 
@@ -77,7 +81,6 @@ async def rm_task(task_id=None):
     & filters.command("cancelTask", prefixes=USERBOT_PREFIX)
     & filters.command("cancelTask", prefixes=USERBOT_PREFIX)
 )
 )
 async def task_cancel(_, message: Message):
 async def task_cancel(_, message: Message):
-    global tasks
     m = message
     m = message
     r = m.reply_to_message
     r = m.reply_to_message
 
 
@@ -110,8 +113,12 @@ async def task_list(_, message: Message):
             text=f"{arrow(message)} No tasks pending",
             text=f"{arrow(message)} No tasks pending",
         )
         )
 
 
-    ls = "\n    ".join([str(i) for i in tasks.keys()])
-    await eor(message, text=f"{arrow(message)}  {ls}")
+    body = {
+        str(key): value.get_name()
+        for key, value in list(tasks.items())
+    }
+
+    await eor(message, text=section("Pending Tasks", body))
 
 
 
 
 @app2.on_message(
 @app2.on_message(
@@ -121,7 +128,7 @@ async def task_list(_, message: Message):
     & filters.command("eval", prefixes=USERBOT_PREFIX)
     & filters.command("eval", prefixes=USERBOT_PREFIX)
 )
 )
 async def executor(client, message: Message):
 async def executor(client, message: Message):
-    global m, p, r, tasks
+    global m, p, r
     try:
     try:
         cmd = message.text.split(" ", maxsplit=1)[1]
         cmd = message.text.split(" ", maxsplit=1)[1]
     except IndexError:
     except IndexError:
@@ -148,6 +155,7 @@ async def executor(client, message: Message):
         task = await add_task(
         task = await add_task(
             aexec,
             aexec,
             m.message_id,
             m.message_id,
+            "Eval",
             cmd,
             cmd,
             client,
             client,
             message,
             message,

+ 2 - 34
wbb/utils/downloader.py

@@ -17,27 +17,9 @@ async def download_url(
     url,
     url,
     file_path,
     file_path,
     chunk_size,
     chunk_size,
-    progress_func,
 ):
 ):
-    global tasks
-
     file_path = file_path or url.split("/")[-1][:20]
     file_path = file_path or url.split("/")[-1][:20]
 
 
-    progress = 0
-    total_size = 0
-
-    # Check if the provided progress function is
-    # async for sync in order to call it correctly.
-    if progress_func:
-        p_f_args = progress_func[1]
-        progress_func = progress_func[0]
-        is_async = iscoroutinefunction(progress_func)
-
-    # Check if the server responds
-    async with session.get(url) as resp:
-        ensure_status(resp.status)
-        total_size = int(resp.headers["Content-Length"])
-
     async with session.get(url) as response:
     async with session.get(url) as response:
         ensure_status(response.status)
         ensure_status(response.status)
 
 
@@ -49,25 +31,13 @@ async def download_url(
             ):
             ):
                 await f.write(chunk)
                 await f.write(chunk)
 
 
-                # Call the progress func on each chunk downloaded
-                progress += chunk_size
-                if progress_func:
-
-                    if is_async:
-                        await progress_func(
-                            progress, total_size, *p_f_args
-                        )
-                    else:
-                        progress_func(progress, total_size, *p_f_args)
-
     return absolute_path(file_path)
     return absolute_path(file_path)
 
 
 
 
 async def download(
 async def download(
     url: str,
     url: str,
     file_path: str = None,
     file_path: str = None,
-    chunk_size: int = 1024,
-    progress_func=None,
+    chunk_size: int = 1000000,  # 1MB chunk
     task_id: int = int(time()),
     task_id: int = int(time()),
 ):
 ):
     """
     """
@@ -75,18 +45,16 @@ async def download(
     :file_path: path/to/file
     :file_path: path/to/file
     :chunk_size: size of a single chunk
     :chunk_size: size of a single chunk
     """
     """
-    global tasks
-
     # Create a task and add it to main tasks dict
     # Create a task and add it to main tasks dict
     # So we can cancel it using .cancelTask
     # So we can cancel it using .cancelTask
 
 
     task = await add_task(
     task = await add_task(
         download_url,
         download_url,
         task_id,
         task_id,
+        "Downloader",
         url=url,
         url=url,
         file_path=file_path,
         file_path=file_path,
         chunk_size=chunk_size,
         chunk_size=chunk_size,
-        progress_func=progress_func,
     )
     )
     await task
     await task
     await rm_task(task_id)
     await rm_task(task_id)

+ 2 - 35
wbb/utils/functions.py

@@ -24,7 +24,7 @@ SOFTWARE.
 from asyncio import gather
 from asyncio import gather
 from datetime import datetime, timedelta
 from datetime import datetime, timedelta
 from io import BytesIO
 from io import BytesIO
-from math import atan2, cos, floor, radians, sin, sqrt
+from math import atan2, cos, radians, sin, sqrt
 from os import execvp
 from os import execvp
 from random import randint
 from random import randint
 from re import findall
 from re import findall
@@ -36,8 +36,8 @@ import aiofiles
 import aiohttp
 import aiohttp
 import speedtest
 import speedtest
 from PIL import Image, ImageDraw, ImageFilter, ImageFont
 from PIL import Image, ImageDraw, ImageFilter, ImageFont
-from pyrogram.types import Message
 from pyrogram.errors import MessageNotModified
 from pyrogram.errors import MessageNotModified
+from pyrogram.types import Message
 
 
 from wbb import aiohttpsession as aiosession
 from wbb import aiohttpsession as aiosession
 from wbb.modules.userbot import eor
 from wbb.modules.userbot import eor
@@ -45,39 +45,6 @@ from wbb.utils.dbfunctions import start_restart_stage
 from wbb.utils.http import get
 from wbb.utils.http import get
 
 
 
 
-async def progress(
-    current: int,
-    total: int,
-    start: int,
-    task_id: int,
-    message: Message,
-):
-    percentage = current / total * 100
-    elapsed = time() - start
-    speed = (current / 1000000) / elapsed  # In MB/s
-    eta = (100 / percentage * elapsed) / 60  # In Minutes
-
-    if round(elapsed % 10.00) != 0 and current != total:
-        return
-
-    round_pct = floor(percentage / 10)
-
-    bar = ("▰" * round_pct) + ("▱" * (10 - round_pct))
-
-    text = f"""
-{bar}
-**Progress:** {int(percentage)}%
-**Speed:** {round(speed, 2)}MB/s
-**ETA:** {round(eta, 2)}m
-**Task ID:** {task_id}
-"""
-    try:
-        await eor(message, text=text)
-    except MessageNotModified:
-        print("MessageNotModified: progress func")
-        pass
-
-
 async def restart(m: Message):
 async def restart(m: Message):
     if m:
     if m:
         await start_restart_stage(m.chat.id, m.message_id)
         await start_restart_stage(m.chat.id, m.message_id)