Skip to content

Commit e651dc1

Browse files
authored
Create telefunc.py
1 parent 279bf87 commit e651dc1

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

ethon/telefunc.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import math
2+
import time
3+
import asyncio
4+
5+
from .FasterTg import upload_file, download_file
6+
7+
from telethon import events
8+
from telethon.errors.rpcerrorlist import UserNotParticipantError
9+
from telethon.tl.functions.channels import GetParticipantRequest
10+
11+
12+
#Fast upload/download methods:
13+
14+
def time_formatter(milliseconds: int) -> str:
15+
"""Inputs time in milliseconds, to get beautified time,
16+
as string"""
17+
seconds, milliseconds = divmod(int(milliseconds), 1000)
18+
minutes, seconds = divmod(seconds, 60)
19+
hours, minutes = divmod(minutes, 60)
20+
days, hours = divmod(hours, 24)
21+
weeks, days = divmod(days, 7)
22+
tmp = (
23+
((str(weeks) + "w:") if weeks else "")
24+
+ ((str(days) + "d:") if days else "")
25+
+ ((str(hours) + "h:") if hours else "")
26+
+ ((str(minutes) + "m:") if minutes else "")
27+
+ ((str(seconds) + "s:") if seconds else "")
28+
)
29+
if tmp.endswith(":"):
30+
return tmp[:-1]
31+
else:
32+
return tmp
33+
34+
def hbs(size):
35+
if not size:
36+
return ""
37+
power = 2 ** 10
38+
raised_to_pow = 0
39+
dict_power_n = {0: "B", 1: "K", 2: "M", 3: "G", 4: "T", 5: "P"}
40+
while size > power:
41+
size /= power
42+
raised_to_pow += 1
43+
return str(round(size, 2)) + " " + dict_power_n[raised_to_pow] + "B"
44+
45+
async def progress(current, total, event, start, type_of_ps, file=None):
46+
now = time.time()
47+
diff = now - start
48+
if round(diff % 10.00) == 0 or current == total:
49+
percentage = current * 100 / total
50+
speed = current / diff
51+
time_to_completion = round((total - current) / speed) * 1000
52+
progress_str = "**[{0}{1}]** `| {2}%`\n\n".format(
53+
"".join(["█" for i in range(math.floor(percentage / 5))]),
54+
"".join(["" for i in range(20 - math.floor(percentage / 5))]),
55+
round(percentage, 2),
56+
)
57+
tmp = (
58+
progress_str
59+
+ "GROSS: {0} of {1}\n\nSpeed: {2}/s\n\nETA: {3}\n\n".format(
60+
hbs(current),
61+
hbs(total),
62+
hbs(speed),
63+
time_formatter(time_to_completion),
64+
)
65+
)
66+
if file:
67+
await event.edit(
68+
"{}\n\n`File Name: {}\n\n{}".format(type_of_ps, file, tmp)
69+
)
70+
else:
71+
await event.edit("{}\n\n{}".format(type_of_ps, tmp))
72+
73+
74+
#Why these methods? : Using progress of telethon makes upload/download slow due to callbacks
75+
#these method allows to upload/download in fastest way with progress bars.
76+
77+
async def fast_upload(file, name, time, bot, event, msg):
78+
with open(file, "rb") as f:
79+
result = await upload_file(
80+
client=bot,
81+
file=f,
82+
filename=name,
83+
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
84+
progress(
85+
d,
86+
t,
87+
event,
88+
time,
89+
msg,
90+
),
91+
),
92+
)
93+
return result
94+
95+
async def fast_download(filename, file, bot, event, time, msg):
96+
with open(filename, "wb") as fk:
97+
result = await download_file(
98+
client=bot,
99+
location=file,
100+
out=fk,
101+
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
102+
progress(
103+
d,
104+
t,
105+
event,
106+
time,
107+
msg,
108+
),
109+
),
110+
)
111+
return result
112+
"""
113+
---------------------------------------------------------------------------------
114+
"""
115+
116+
#Forcesub
117+
async def force_sub(client, channel, id, ft):
118+
s, r = False, None
119+
try:
120+
x = await client(GetParticipantRequest(channel=channel, participant=int(id)))
121+
left = x.stringify()
122+
if 'left' in left:
123+
s, r = True, f"{ft}\n\nAlso join @DroneBots"
124+
else:
125+
s, r = False, None
126+
except UserNotParticipantError:
127+
s, r = True, f"To use this bot you've to join @{channel}.\n\nAlso join @DroneBots"
128+
except Exception:
129+
s, r = True, "ERROR: Add in ForceSub channel, or check your channel id."
130+
return s, r

0 commit comments

Comments
 (0)