forked from 1Danish-00/CompressorBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncn.py
169 lines (145 loc) · 4.82 KB
/
funcn.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
157
158
159
160
161
162
163
164
165
166
167
168
169
# This file is part of the CompressorBot distribution.
# Copyright (c) 2021 Danish_00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# License can be found in < https://github.com/1Danish-00/CompressorBot/blob/main/License> .
from .config import *
COUNT = []
OK = {}
uptime = dt.now()
os.system("wget https://telegra.ph/file/75ee20ec8d8c8bba84f02.jpg -O thumb.jpg")
if not os.path.isdir("downloads/"):
os.mkdir("downloads/")
if not os.path.isdir("encode/"):
os.mkdir("encode/")
if not os.path.isdir("thumb/"):
os.mkdir("thumb/")
def stdr(seconds: int) -> str:
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
if len(str(minutes)) == 1:
minutes = "0" + str(minutes)
if len(str(hours)) == 1:
hours = "0" + str(hours)
if len(str(seconds)) == 1:
seconds = "0" + str(seconds)
dur = (
((str(hours) + ":") if hours else "00:")
+ ((str(minutes) + ":") if minutes else "00:")
+ ((str(seconds)) if seconds else "")
)
return dur
def ts(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]
def hbs(size):
if not size:
return ""
power = 2 ** 10
raised_to_pow = 0
dict_power_n = {0: "B", 1: "K", 2: "M", 3: "G", 4: "T", 5: "P"}
while size > power:
size /= power
raised_to_pow += 1
return str(round(size, 2)) + " " + dict_power_n[raised_to_pow] + "B"
async def progress(current, total, event, start, type_of_ps, file=None):
now = time.time()
diff = now - start
if round(diff % 10.00) == 0 or current == total:
percentage = current * 100 / total
speed = current / diff
time_to_completion = round((total - current) / speed) * 1000
progress_str = "`[{0}{1}] {2}%`\n\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_str
+ "`{0} of {1}`\n\n`✦ Speed: {2}/s`\n\n`✦ ETA: {3}`\n\n".format(
hbs(current),
hbs(total),
hbs(speed),
ts(time_to_completion),
)
)
if file:
await event.edit(
"`✦ {}`\n\n`File Name: {}`\n\n{}".format(type_of_ps, file, tmp)
)
else:
await event.edit("`✦ {}`\n\n{}".format(type_of_ps, tmp))
async def genss(file):
process = subprocess.Popen(
["mediainfo", file, "--Output=JSON"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
stdout, stderr = process.communicate()
out = stdout.decode().strip()
z = json.loads(out)
p = z["media"]["track"][0]["Duration"]
return int(p.split(".")[-2])
async def duration_s(file):
tsec = await genss(file)
x = round(tsec / 5)
y = round(tsec / 5 + 30)
pin = stdr(x)
if y < tsec:
pon = stdr(y)
else:
pon = stdr(tsec)
return pin, pon
async def info(file, event):
process = subprocess.Popen(
["mediainfo", file, "--Output=HTML"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
stdout, stderr = process.communicate()
out = stdout.decode()
client = TelegraphPoster(use_api=True)
client.create_api_token("Mediainfo")
page = client.post(
title="Mediainfo",
author=((await event.client.get_me()).first_name),
author_url=f"https://t.me/{((await event.client.get_me()).username)}",
text=out,
)
return page["url"]
def code(data):
OK.update({len(OK): data})
return str(len(OK)-1)
def decode(key):
if OK.get(int(key)):
return OK[int(key)]
return
async def skip(e):
wah = e.pattern_match.group(1).decode("UTF-8")
wh = decode(wah)
out, dl, thum, dtime = wh.split(";")
try:
COUNT.remove(e.sender_id)
os.remove(dl)
os.remove(out)
except:
pass
return await e.delete()