Skip to content

Commit c5d1678

Browse files
authored
Update pyfunc.py
1 parent e651dc1 commit c5d1678

File tree

1 file changed

+40
-130
lines changed

1 file changed

+40
-130
lines changed

ethon/pyfunc.py

Lines changed: 40 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,40 @@
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
1+
"""This file is part of the ethon distribution.
2+
Copyright (c) 2021 vasusen-code
3+
This program is free software: you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation, version 3.
6+
This program is distributed in the hope that it will be useful, but
7+
WITHOUT ANY WARRANTY; without even the implied warranty of
8+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9+
General Public License for more details.
10+
License can be found in < https://github.com/vasusen-code/ethon/blob/main/LICENSE > ."""
11+
12+
#vasusen-code/thechariotoflight/dronebots
13+
#__TG:ChauhanMahesh__
14+
15+
import subprocess
16+
import cv2
17+
18+
#fastest way to get total number of frames in a video
19+
def total_frames(video_path):
20+
cap = cv2.VideoCapture(f"{video_path}")
21+
tf = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
22+
return tf
23+
24+
#makes a subprocess handy
25+
def bash(cmd):
26+
bashCommand = f"{cmd}"
27+
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
28+
output, error = process.communicate()
29+
return output, error
30+
31+
#to get width, height and duration(in sec) of a video
32+
def video_metadata(file):
33+
vcap = cv2.VideoCapture(f'{file}')
34+
width = round(vcap.get(cv2.CAP_PROP_FRAME_WIDTH ))
35+
height = round(vcap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
36+
fps = vcap.get(cv2.CAP_PROP_FPS)
37+
frame_count = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
38+
duration = round(frame_count / fps)
39+
data = {'width' : width, 'height' : height, 'duration' : duration }
40+
return

0 commit comments

Comments
 (0)