-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
156 lines (138 loc) · 4.72 KB
/
helpers.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import time
import math
import asyncio
import requests
if bool(os.environ.get("WEBHOOK", False)):
from sample_config import Config
else:
from config import Config
from script import script
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; rv:80.0) Gecko/20100101 Firefox/80.0",
"Referer":"https://www.zee5.com",
"Accept":"*/*",
"Accept-Encoding":"gzip, deflate, br",
"Connection":"keep-alive",
"Accept-Language":"en-US,en;q=0.9",
"Origin":"https://www.zee5.com",
"sec-fetch-dest":"empty",
"sec-fetch-mode":"cors",
"sec-fetch-site":"same-site"
}
async def progress_for_pyrogram(
current,
total,
ud_type,
message,
start
):
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 = "\n[{0}{1}] \n\n⭕️Progress: {2}%\n".format(
''.join(["▣" for i in range(math.floor(percentage / 5))]),
''.join(["▢" for i in range(20 - math.floor(percentage / 5))]),
round(percentage, 2))
tmp = progress + "{0} of {1}\n\n⭕️Speed: {2}/s\n\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 message.edit(
text="{}\n {}".format(
ud_type,
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: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'}
while size > power:
size /= power
n += 1
return str(round(size, 2)) + " " + Dic_powerN[n] + 'B'
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]
async def take_screen_shot(video_file, output_directory, ttl):
out_put_file_name = output_directory + \
"/" + str(time.time()) + ".jpg"
file_genertor_command = [
"ffmpeg",
"-ss",
str(ttl),
"-i",
video_file,
"-vframes",
"1",
out_put_file_name
]
# width = "90"
process = await asyncio.create_subprocess_exec(
*file_genertor_command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await process.communicate()
e_response = stderr.decode().strip()
t_response = stdout.decode().strip()
if os.path.lexists(out_put_file_name):
return out_put_file_name
else:
return None
def DownLoadFile(url, file_name, chunk_size, client, ud_type, message_id, chat_id):
if os.path.exists(file_name):
os.remove(file_name)
if not url:
return file_name
r = requests.get(url, allow_redirects=True, stream=True)
total_size = int(r.headers.get("content-length", 0))
downloaded_size = 0
with open(file_name, 'wb') as fd:
for chunk in r.iter_content(chunk_size=chunk_size):
if chunk:
fd.write(chunk)
downloaded_size += chunk_size
if client is not None:
if ((total_size // downloaded_size) % 5) == 0:
time.sleep(0.3)
try:
client.edit_message_text(
chat_id,
message_id,
text="{}: {} of {}".format(
ud_type,
humanbytes(downloaded_size),
humanbytes(total_size)
)
)
except:
pass
return file_name