File size: 3,432 Bytes
78b07ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import time

from pyrogram.types import Message
from pySmartDL import SmartDL

from Hellbot.core import LOGS, Symbols
from Hellbot.functions.formatter import readable_time
from Hellbot.functions.tools import progress

from . import Config, HelpMenu, hellbot, on_message


@on_message("download", allow_stan=True)
async def download(_, message: Message):
    hell = await hellbot.edit(message, "Starting to download...")
    if message.reply_to_message and message.reply_to_message.media:
        start_time = time.time()
        try:
            dwl_path = await message.reply_to_message.download(
                Config.DWL_DIR,
                progress=progress,
                progress_args=(
                    hell,
                    start_time,
                    "⬇️ Downloading",
                ),
            )
        except Exception as e:
            return await hellbot.error(hell, f"`{e}`", 10)

        await hell.edit(
            f"**Downloaded to** `{dwl_path}` **in** {readable_time(round(time.time() - start_time))} seconds.**"
        )

    elif len(message.command) > 2:
        dwl_url = (await hellbot.input(message)).split(" ")
        start_time = time.time()
        try:
            dl = SmartDL(dwl_url, Config.DWL_DIR, progress_bar=False)
            dl.start(blocking=False)
            while not dl.isFinished():
                display_msg = ""
                downloaded_size = dl.get_dl_size(human=True)
                file_size = dl.filesize or "Unknown"
                diff = time.time() - start_time
                speed = dl.get_speed(human=True)
                dl_percentage = round((dl.get_progress() * 100), 2)
                eta = dl.get_eta(human=True)
                try:
                    current_msg = (
                        f"**⬇️ Downloading...**\n\n"
                        f"**{Symbols.anchor} URL:** `{dwl_url}`\n"
                        f"**{Symbols.anchor} Downloaded:** `{downloaded_size}` of `{file_size}`\n"
                        f"**{Symbols.anchor} Speed:** `{speed}`\n"
                        f"**{Symbols.anchor} ETA:** `{eta}`\n"
                        f"**{Symbols.anchor} Progress:** `{dl_percentage}%`"
                    )
                    if round(diff % 10.00) == 0 and current_msg != display_msg:
                        await hell.edit(current_msg)
                        display_msg = current_msg
                except Exception as e:
                    LOGS.warning(f"PySmartDl: {str(e)}")

            end_time = readable_time(round(time.time() - start_time))
            if dl.isSuccessful():
                await hell.edit(
                    f"**Downloaded to** `{dl.get_dest()}` **in** `{end_time}` **seconds.**"
                )
            else:
                await hellbot.error(hell, f"**Failed to download** `{len(dwl_url)} url(s)`")
        except Exception as e:
            return await hellbot.error(hell, f"`{e}`", 10)
    else:
        return await hellbot.delete(
            message, "Reply to a media message or pass direct urls to download it."
        )


HelpMenu("downloads").add(
    "download",
    "<reply to media> or <direct link>",
    "Download media to server.",
    "download https://example.com/file.mp4",
    "You can pass multiple urls to download it on my server.",
).info("Downloader").done()