-
Notifications
You must be signed in to change notification settings - Fork 43
/
display_progress.py
134 lines (119 loc) · 4.58 KB
/
display_progress.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import math, time, re, asyncio, threading
from pyrogram.errors.exceptions import MessageNotModified, FloodWait
def parse_progress(line):
progress_pattern = re.compile(
r'(frame|fps|size|time|bitrate|speed)\s*\=\s*(\S+)'
)
items = {
key: value for key, value in progress_pattern.findall(line)
}
if not items:
return None
return items
async def readlines(stream):
pattern = re.compile(br'[\r\n]+')
data = bytearray()
while not stream.at_eof():
lines = pattern.split(data)
data[:] = lines.pop(-1)
for line in lines:
yield line
data.extend(await stream.read(1024))
async def read_stderr(start, msg, process):
async for line in readlines(process.stderr):
line = line.decode('utf-8')
progress = parse_progress(line)
if progress:
#Progress bar logic
now = time.time()
diff = start-now
text = 'Now converting mono audio..\n'
text += 'Size : {}\n'.format(progress['size'])
text += 'Time : {}\n'.format(progress['time'])
text += 'Speed : {}\n'.format(progress['speed'])
if round(diff % 5)==0:
try:
await msg.edit( text )
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: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'}
while size > power:
size /= power
n += 1
return str(round(size, 2)) + " " + Dic_powerN[n] + 'B'
def edit_msg(client, message, to_edit):
try:
client.loop.create_task(message.edit(to_edit))
except MessageNotModified:
pass
except FloodWait as e:
client.loop.create_task(asyncio.sleep(e.x))
except TypeError:
pass
def download_progress_hook(d, message, client):
if d['status'] == 'downloading':
current = d.get("_downloaded_bytes_str") or humanbytes(int(d.get("downloaded_bytes", 1)))
total = d.get("_total_bytes_str") or d.get("_total_bytes_estimate_str")
file_name = d.get("filename")
eta = d.get('_eta_str', "N/A")
percent = d.get("_percent_str", "N/A")
speed = d.get("_speed_str", "N/A")
to_edit = f"<b><u>Downloading File</b></u> \n<b>File Name :</b> <code>{file_name}</code> \n<b>File Size :</b> <code>{total}</code> \n<b>Speed :</b> <code>{speed}</code> \n<b>ETA :</b> <code>{eta}</code> \n<i>Downloaded {current} out of {total}</i> (__{percent}__)"
threading.Thread(target=edit_msg, args=(client, message, to_edit)).start()
async def progress_for_pyrogram(current, total, ud_type, message, start):
PROGRESS = """
Percentage : {0}%
Done: {1}
Total: {2}
Speed: {3}/s
ETA: {4}
"""
now = time.time()
diff = now - start
if round(diff % 10.00) == 0 or current == total:
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 = "[{0}{1}] \n".format(
''.join(["●" for i in range(math.floor(percentage / 5))]),
''.join(["○" for i in range(20 - math.floor(percentage / 5))])
)
tmp = progress + PROGRESS.format(
round(percentage, 2),
humanbytes(current),
humanbytes(total),
humanbytes(speed),
estimated_total_time if estimated_total_time != '' else "0 s"
)
try:
await message.edit(
text="**{}**\n\n {}".format(
ud_type,
tmp
),
parse_mode='markdown'
)
except:
pass
def TimeFormatter(milliseconds: int) -> str:
seconds, milliseconds = divmod(int(milliseconds), 1000)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
tmp = ((str(days) + "d, ") if days else "") + \
((str(hours) + "h, ") if hours else "") + \
((str(minutes) + "m, ") if minutes else "") + \
((str(seconds) + "s, ") if seconds else "") + \
((str(milliseconds) + "ms, ") if milliseconds else "")
return tmp[:-2]