-
Notifications
You must be signed in to change notification settings - Fork 946
Expand file tree
/
Copy pathfunc.py
More file actions
289 lines (257 loc) · 9.63 KB
/
func.py
File metadata and controls
289 lines (257 loc) · 9.63 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# ---------------------------------------------------
# File Name: func.py
# Description: A Pyrogram bot for downloading files from Telegram channels or groups
# and uploading them back to Telegram.
# Author: Gagan
# GitHub: https://github.com/devgaganin/
# Telegram: https://t.me/team_spy_pro
# YouTube: https://youtube.com/@dev_gagan
# Created: 2025-01-11
# Last Modified: 2025-01-11
# Version: 2.0.5
# License: MIT License
# ---------------------------------------------------
import math
import time , re
from pyrogram import enums
from config import CHANNEL_ID, OWNER_ID
from devgagan.core.mongo.plans_db import premium_users
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
import cv2
from pyrogram.errors import FloodWait, InviteHashInvalid, InviteHashExpired, UserAlreadyParticipant, UserNotParticipant
from datetime import datetime as dt
import asyncio, subprocess, re, os, time
async def chk_user(message, user_id):
user = await premium_users()
if user_id in user or user_id in OWNER_ID:
return 0
else:
return 1
async def gen_link(app,chat_id):
link = await app.export_chat_invite_link(chat_id)
return link
async def subscribe(app, message):
update_channel = CHANNEL_ID
url = await gen_link(app, update_channel)
if update_channel:
try:
user = await app.get_chat_member(update_channel, message.from_user.id)
if user.status == "kicked":
await message.reply_text("You are Banned. Contact -- @devgaganin")
return 1
except UserNotParticipant:
caption = f"Join our channel to use the bot"
await message.reply_photo(photo="https://graph.org/file/d44f024a08ded19452152.jpg",caption=caption, reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Join Now...", url=f"{url}")]]))
return 1
except Exception:
await message.reply_text("Something Went Wrong. Contact us @devgaganin...")
return 1
async def get_seconds(time_string):
def extract_value_and_unit(ts):
value = ""
unit = ""
index = 0
while index < len(ts) and ts[index].isdigit():
value += ts[index]
index += 1
unit = ts[index:].lstrip()
if value:
value = int(value)
return value, unit
value, unit = extract_value_and_unit(time_string)
if unit == 's':
return value
elif unit == 'min':
return value * 60
elif unit == 'hour':
return value * 3600
elif unit == 'day':
return value * 86400
elif unit == 'month':
return value * 86400 * 30
elif unit == 'year':
return value * 86400 * 365
else:
return 0
PROGRESS_BAR = """\n
│ **__Completed:__** {1}/{2}
│ **__Bytes:__** {0}%
│ **__Speed:__** {3}/s
│ **__ETA:__** {4}
╰─────────────────────╯
"""
async def progress_bar(current, total, ud_type, message, start):
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}".format(
''.join(["♦" for i in range(math.floor(percentage / 10))]),
''.join(["◇" for i in range(10 - math.floor(percentage / 10))]))
tmp = progress + PROGRESS_BAR.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│ {}".format(ud_type, tmp),)
except:
pass
def humanbytes(size):
if not size:
return ""
power = 2**10
n = 0
Dic_powerN = {0: ' ', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
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]
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
async def userbot_join(userbot, invite_link):
try:
await userbot.join_chat(invite_link)
return "Successfully joined the Channel"
except UserAlreadyParticipant:
return "User is already a participant."
except (InviteHashInvalid, InviteHashExpired):
return "Could not join. Maybe your link is expired or Invalid."
except FloodWait:
return "Too many requests, try again later."
except Exception as e:
print(e)
return "Could not join, try joining manually."
def get_link(string):
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex,string)
try:
link = [x[0] for x in url][0]
if link:
return link
else:
return False
except Exception:
return False
def video_metadata(file):
default_values = {'width': 1, 'height': 1, 'duration': 1}
try:
vcap = cv2.VideoCapture(file)
if not vcap.isOpened():
return default_values
width = round(vcap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = round(vcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = vcap.get(cv2.CAP_PROP_FPS)
frame_count = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
if fps <= 0:
return default_values
duration = round(frame_count / fps)
if duration <= 0:
return default_values
vcap.release()
return {'width': width, 'height': height, 'duration': duration}
except Exception as e:
print(f"Error in video_metadata: {e}")
return default_values
def hhmmss(seconds):
return time.strftime('%H:%M:%S',time.gmtime(seconds))
async def screenshot(video, duration, sender):
if os.path.exists(f'{sender}.jpg'):
return f'{sender}.jpg'
time_stamp = hhmmss(int(duration)/2)
out = dt.now().isoformat("_", "seconds") + ".jpg"
cmd = ["ffmpeg",
"-ss",
f"{time_stamp}",
"-i",
f"{video}",
"-frames:v",
"1",
f"{out}",
"-y"
]
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
x = stderr.decode().strip()
y = stdout.decode().strip()
if os.path.isfile(out):
return out
else:
None
last_update_time = time.time()
async def progress_callback(current, total, progress_message):
percent = (current / total) * 100
global last_update_time
current_time = time.time()
if current_time - last_update_time >= 10 or percent % 10 == 0:
completed_blocks = int(percent // 10)
remaining_blocks = 10 - completed_blocks
progress_bar = "♦" * completed_blocks + "◇" * remaining_blocks
current_mb = current / (1024 * 1024)
total_mb = total / (1024 * 1024)
await progress_message.edit(
f"╭──────────────────╮\n"
f"│ **__Uploading...__** \n"
f"├──────────\n"
f"│ {progress_bar}\n\n"
f"│ **__Progress:__** {percent:.2f}%\n"
f"│ **__Uploaded:__** {current_mb:.2f} MB / {total_mb:.2f} MB\n"
f"╰──────────────────╯\n\n"
f"**__Powered by SmartKit Bots__**"
)
last_update_time = current_time
async def prog_bar(current, total, ud_type, message, start):
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}".format(
''.join(["♦" for i in range(math.floor(percentage / 10))]),
''.join(["◇" for i in range(10 - math.floor(percentage / 10))]))
tmp = progress + PROGRESS_BAR.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(
text="{}\n│ {}".format(ud_type, tmp),)
except:
pass