forked from abhinai2244/FileStore-Bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelper_func.py
More file actions
278 lines (236 loc) · 8.79 KB
/
helper_func.py
File metadata and controls
278 lines (236 loc) · 8.79 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
#(©)CodeFlix_Bots
#rohit_1888 on Tg #Dont remove this line
import base64
import re
import asyncio
import time
from pyrogram import filters
from pyrogram.enums import ChatMemberStatus
from config import *
from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
from shortzy import Shortzy
from pyrogram.errors import FloodWait
from database.database import *
# Don't Remove Credit @CodeFlix_Bots, @rohit_1888
# Ask Doubt on telegram @CodeflixSupport
#
# Copyright (C) 2025 by Codeflix-Bots@Github, < https://github.com/Codeflix-Bots >.
#
# This file is part of < https://github.com/Codeflix-Bots/FileStore > project,
# and is released under the MIT License.
# Please see < https://github.com/Codeflix-Bots/FileStore/blob/master/LICENSE >
#
# All rights reserved.
#
#used for cheking if a user is admin ~Owner also treated as admin level
async def check_admin(filter, client, update):
try:
user_id = update.from_user.id
return any([user_id == OWNER_ID, await db.admin_exist(user_id)])
except Exception as e:
print(f"! Exception in check_admin: {e}")
return False
# Don't Remove Credit @CodeFlix_Bots, @rohit_1888
# Ask Doubt on telegram @CodeflixSupport
#
# Copyright (C) 2025 by Codeflix-Bots@Github, < https://github.com/Codeflix-Bots >.
#
# This file is part of < https://github.com/Codeflix-Bots/FileStore > project,
# and is released under the MIT License.
# Please see < https://github.com/Codeflix-Bots/FileStore/blob/master/LICENSE >
#
# All rights reserved.
#
async def is_subscribed(client, user_id):
channel_ids = await db.show_channels()
if not channel_ids:
return True
if user_id == OWNER_ID:
return True
for cid in channel_ids:
if not await is_sub(client, user_id, cid):
# Retry once if join request might be processing
mode = await db.get_channel_mode(cid)
if mode == "on":
await asyncio.sleep(2) # give time for @on_chat_join_request to process
if await is_sub(client, user_id, cid):
continue
return False
return True
# Don't Remove Credit @CodeFlix_Bots, @rohit_1888
# Ask Doubt on telegram @CodeflixSupport
#
# Copyright (C) 2025 by Codeflix-Bots@Github, < https://github.com/Codeflix-Bots >.
#
# This file is part of < https://github.com/Codeflix-Bots/FileStore > project,
# and is released under the MIT License.
# Please see < https://github.com/Codeflix-Bots/FileStore/blob/master/LICENSE >
#
# All rights reserved.
#
async def is_sub(client, user_id, channel_id):
try:
member = await client.get_chat_member(channel_id, user_id)
status = member.status
#print(f"[SUB] User {user_id} in {channel_id} with status {status}")
return status in {
ChatMemberStatus.OWNER,
ChatMemberStatus.ADMINISTRATOR,
ChatMemberStatus.MEMBER
}
except UserNotParticipant:
mode = await db.get_channel_mode(channel_id)
if mode == "on":
exists = await db.req_user_exist(channel_id, user_id)
#print(f"[REQ] User {user_id} join request for {channel_id}: {exists}")
return exists
#print(f"[NOT SUB] User {user_id} not in {channel_id} and mode != on")
return False
except Exception as e:
print(f"[!] Error in is_sub(): {e}")
return False
# Don't Remove Credit @CodeFlix_Bots, @rohit_1888
# Ask Doubt on telegram @CodeflixSupport
#
# Copyright (C) 2025 by Codeflix-Bots@Github, < https://github.com/Codeflix-Bots >.
#
# This file is part of < https://github.com/Codeflix-Bots/FileStore > project,
# and is released under the MIT License.
# Please see < https://github.com/Codeflix-Bots/FileStore/blob/master/LICENSE >
#
# All rights reserved.
#
async def encode(string):
string_bytes = string.encode("ascii")
base64_bytes = base64.urlsafe_b64encode(string_bytes)
base64_string = (base64_bytes.decode("ascii")).strip("=")
return base64_string
async def decode(base64_string):
base64_string = base64_string.strip("=") # links generated before this commit will be having = sign, hence striping them to handle padding errors.
base64_bytes = (base64_string + "=" * (-len(base64_string) % 4)).encode("ascii")
string_bytes = base64.urlsafe_b64decode(base64_bytes)
string = string_bytes.decode("ascii")
return string
async def get_messages(client, message_ids):
messages = []
total_messages = 0
while total_messages != len(message_ids):
temb_ids = message_ids[total_messages:total_messages+200]
try:
msgs = await client.get_messages(
chat_id=client.db_channel.id,
message_ids=temb_ids
)
except FloodWait as e:
await asyncio.sleep(e.x)
msgs = await client.get_messages(
chat_id=client.db_channel.id,
message_ids=temb_ids
)
except:
pass
total_messages += len(temb_ids)
messages.extend(msgs)
return messages
async def get_message_id(client, message):
if message.forward_from_chat:
if message.forward_from_chat.id == client.db_channel.id:
return message.forward_from_message_id
else:
return 0
elif message.forward_sender_name:
return 0
elif message.text:
pattern = "https://t.me/(?:c/)?(.*)/(\d+)"
matches = re.match(pattern,message.text)
if not matches:
return 0
channel_id = matches.group(1)
msg_id = int(matches.group(2))
if channel_id.isdigit():
if f"-100{channel_id}" == str(client.db_channel.id):
return msg_id
else:
if channel_id == client.db_channel.username:
return msg_id
else:
return 0
def get_readable_time(seconds: int) -> str:
count = 0
up_time = ""
time_list = []
time_suffix_list = ["s", "m", "h", "days"]
while count < 4:
count += 1
remainder, result = divmod(seconds, 60) if count < 3 else divmod(seconds, 24)
if seconds == 0 and remainder == 0:
break
time_list.append(int(result))
seconds = int(remainder)
hmm = len(time_list)
for x in range(hmm):
time_list[x] = str(time_list[x]) + time_suffix_list[x]
if len(time_list) == 4:
up_time += f"{time_list.pop()}, "
time_list.reverse()
up_time += ":".join(time_list)
return up_time
def get_exp_time(seconds):
periods = [('days', 86400), ('hours', 3600), ('mins', 60), ('secs', 1)]
result = ''
for period_name, period_seconds in periods:
if seconds >= period_seconds:
period_value, seconds = divmod(seconds, period_seconds)
result += f'{int(period_value)} {period_name}'
return result
# Don't Remove Credit @CodeFlix_Bots, @rohit_1888
# Ask Doubt on telegram @CodeflixSupport
#
# Copyright (C) 2025 by Codeflix-Bots@Github, < https://github.com/Codeflix-Bots >.
#
# This file is part of < https://github.com/Codeflix-Bots/FileStore > project,
# and is released under the MIT License.
# Please see < https://github.com/Codeflix-Bots/FileStore/blob/master/LICENSE >
#
# All rights reserved.
#
async def get_shortlink(url, api, link):
shortzy = Shortzy(api_key=api, base_site=url)
link = await shortzy.convert(link)
return link
async def create_masked_link(target_url: str) -> str:
"""Generate a hashed masked link for the given target URL."""
from plugins.crypto_hash import generate_hash_id
from config import BASE_URL, LOGGER
try:
# Get the selected algorithm from DB
algorithm = await db.get_hash_algorithm()
# Generate the hash ID
hash_id = generate_hash_id(algorithm, target_url)
# Store the mapping in DB
await db.store_masked_link(hash_id, target_url, algorithm)
# Build the masked URL
base = BASE_URL.rstrip('/') if BASE_URL else ""
if not base:
LOGGER(__name__).warning("BASE_URL is not set! Masked link will not work.")
return target_url
if not base.startswith("http"):
base = f"https://{base}"
return f"{base}/r/{hash_id}"
except Exception as e:
LOGGER(__name__).error(f"Masking Error: {e}")
return target_url
subscribed = filters.create(is_subscribed)
admin = filters.create(check_admin)
#rohit_1888 on Tg :
# Don't Remove Credit @CodeFlix_Bots, @rohit_1888
# Ask Doubt on telegram @CodeflixSupport
#
# Copyright (C) 2025 by Codeflix-Bots@Github, < https://github.com/Codeflix-Bots >.
#
# This file is part of < https://github.com/Codeflix-Bots/FileStore > project,
# and is released under the MIT License.
# Please see < https://github.com/Codeflix-Bots/FileStore/blob/master/LICENSE >
#
# All rights reserved.
#