This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprogress_bar.py
66 lines (57 loc) · 2.19 KB
/
progress_bar.py
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
import time, math
async def progress(current,total,a,start, title):
now = time.time()
diff = now - start
if round(diff % 10.00) == 0 or current == total:
# if round(current / total * 100, 0) % 5 == 0:
percentage = current * 100 / total
speed = current / diff
elapsed_time = round(diff) * 1000
time_to_completion = round((total - current) / speed) * 1000
estimated_total_time = elapsed_time + time_to_completion
elapsed_time = TimeFormatter(milliseconds=elapsed_time)
estimated_total_time = TimeFormatter(milliseconds=estimated_total_time)
progress = "__**Progress :**__ `[{0}{1}] {2}%`\n".format(
''.join(["●" for _ in range(math.floor(percentage / 5))]),
''.join([" " for _ in range(20 - math.floor(percentage / 5))]),
round(percentage, 2),
)
tmp = progress + "__**Uploaded :**__ {0} of {1}\n__**Speed :**__ {2}/s\n__**ETA :**__ {3}\n".format(
humanbytes(current),
humanbytes(total),
humanbytes(speed),
# elapsed_time if elapsed_time != '' else "0 s",
estimated_total_time if estimated_total_time != '' else "0 s"
)
try:
await a.edit(
text=f"__**File :**__ __{title}__\n"
f"{tmp}"
)
except:
pass
def humanbytes(size):
# https://stackoverflow.com/a/49361727/4723940
# 2**10 = 1024
if not size:
return ""
power = 2**10
n = 0
Dic_powerN = {0: ' ', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
while size > power:
size /= power
n += 1
return f"{str(round(size, 2))} {Dic_powerN[n]}B"
def TimeFormatter(milliseconds: int) -> str:
seconds, milliseconds = divmod(milliseconds, 1000)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
tmp = (
(f"{str(days)}d, " if days else "")
+ (f"{str(hours)}h, " if hours else "")
+ (f"{str(minutes)}m, " if minutes else "")
+ (f"{str(seconds)}s, " if seconds else "")
+ (f"{str(milliseconds)}ms, " if milliseconds else "")
)
return tmp[:-2]