Upload 9 files
Browse files- Systumm/utils/bot_utils.py +216 -0
- Systumm/utils/broadcast_helper.py +19 -0
- Systumm/utils/custom_dl.py +216 -0
- Systumm/utils/database.py +126 -0
- Systumm/utils/file_properties.py +143 -0
- Systumm/utils/human_readable.py +10 -0
- Systumm/utils/render_template.py +34 -0
- Systumm/utils/time_format.py +22 -0
- Systumm/utils/translation.py +71 -0
Systumm/utils/bot_utils.py
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from typing import Union
|
| 3 |
+
from pyrogram.errors import UserNotParticipant, FloodWait
|
| 4 |
+
from pyrogram.enums.parse_mode import ParseMode
|
| 5 |
+
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message
|
| 6 |
+
|
| 7 |
+
# --- SYSTUMM IMPORTS ---
|
| 8 |
+
from Systumm.utils.translation import LANG
|
| 9 |
+
from Systumm.utils.database import Database
|
| 10 |
+
from Systumm.utils.human_readable import humanbytes
|
| 11 |
+
from Systumm.config import Telegram, Server
|
| 12 |
+
from Systumm.bot import SystummBot
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME)
|
| 16 |
+
|
| 17 |
+
async def get_invite_link(bot, chat_id: Union[str, int]):
|
| 18 |
+
try:
|
| 19 |
+
invite_link = await bot.create_chat_invite_link(chat_id=chat_id)
|
| 20 |
+
return invite_link
|
| 21 |
+
except FloodWait as e:
|
| 22 |
+
print(f"Sleep of {e.value}s caused by FloodWait ...")
|
| 23 |
+
await asyncio.sleep(e.value)
|
| 24 |
+
return await get_invite_link(bot, chat_id)
|
| 25 |
+
|
| 26 |
+
async def is_user_joined(bot, message: Message):
|
| 27 |
+
if Telegram.FORCE_SUB_ID and Telegram.FORCE_SUB_ID.startswith("-100"):
|
| 28 |
+
channel_chat_id = int(Telegram.FORCE_SUB_ID) # When id startswith with -100
|
| 29 |
+
elif Telegram.FORCE_SUB_ID and (not Telegram.FORCE_SUB_ID.startswith("-100")):
|
| 30 |
+
channel_chat_id = Telegram.FORCE_SUB_ID # When id not startswith -100
|
| 31 |
+
else:
|
| 32 |
+
return 200
|
| 33 |
+
try:
|
| 34 |
+
user = await bot.get_chat_member(chat_id=channel_chat_id, user_id=message.from_user.id)
|
| 35 |
+
if user.status == "BANNED":
|
| 36 |
+
await message.reply_text(
|
| 37 |
+
text=LANG.BAN_TEXT.format(Telegram.OWNER_ID),
|
| 38 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 39 |
+
disable_web_page_preview=True
|
| 40 |
+
)
|
| 41 |
+
return False
|
| 42 |
+
except UserNotParticipant:
|
| 43 |
+
invite_link = await get_invite_link(bot, chat_id=channel_chat_id)
|
| 44 |
+
if Telegram.VERIFY_PIC:
|
| 45 |
+
ver = await message.reply_photo(
|
| 46 |
+
photo=Telegram.VERIFY_PIC,
|
| 47 |
+
caption="<i>Jᴏɪɴ ᴍʏ ᴜᴘᴅᴀᴛᴇ ᴄʜᴀɴɴᴇʟ ᴛᴏ ᴜsᴇ ᴍᴇ 🔐</i>",
|
| 48 |
+
parse_mode=ParseMode.HTML,
|
| 49 |
+
reply_markup=InlineKeyboardMarkup(
|
| 50 |
+
[[
|
| 51 |
+
InlineKeyboardButton("❆ Jᴏɪɴ Oᴜʀ Cʜᴀɴɴᴇʟ ❆", url=invite_link.invite_link)
|
| 52 |
+
]]
|
| 53 |
+
)
|
| 54 |
+
)
|
| 55 |
+
else:
|
| 56 |
+
ver = await message.reply_text(
|
| 57 |
+
text = "<i>Jᴏɪɴ ᴍʏ ᴜᴘᴅᴀᴛᴇ ᴄʜᴀɴɴᴇʟ ᴛᴏ ᴜsᴇ ᴍᴇ 🔐</i>",
|
| 58 |
+
reply_markup=InlineKeyboardMarkup(
|
| 59 |
+
[[
|
| 60 |
+
InlineKeyboardButton("❆ Jᴏɪɴ Oᴜʀ Cʜᴀɴɴᴇʟ ❆", url=invite_link.invite_link)
|
| 61 |
+
]]
|
| 62 |
+
),
|
| 63 |
+
parse_mode=ParseMode.HTML
|
| 64 |
+
)
|
| 65 |
+
await asyncio.sleep(30)
|
| 66 |
+
try:
|
| 67 |
+
await ver.delete()
|
| 68 |
+
await message.delete()
|
| 69 |
+
except Exception:
|
| 70 |
+
pass
|
| 71 |
+
return False
|
| 72 |
+
except Exception:
|
| 73 |
+
await message.reply_text(
|
| 74 |
+
text = f"<i>Sᴏᴍᴇᴛʜɪɴɢ ᴡʀᴏɴɢ ᴄᴏɴᴛᴀᴄᴛ ᴍʏ ᴅᴇᴠᴇʟᴏᴘᴇʀ</i> <b><a href='https://t.me/{Telegram.UPDATES_CHANNEL}'>[ ᴄʟɪᴄᴋ ʜᴇʀᴇ ]</a></b>",
|
| 75 |
+
parse_mode=ParseMode.HTML,
|
| 76 |
+
disable_web_page_preview=True)
|
| 77 |
+
return False
|
| 78 |
+
return True
|
| 79 |
+
|
| 80 |
+
#---------------------[ PRIVATE GEN LINK + CALLBACK ]---------------------#
|
| 81 |
+
|
| 82 |
+
async def gen_link(_id):
|
| 83 |
+
file_info = await db.get_file(_id)
|
| 84 |
+
file_name = file_info['file_name']
|
| 85 |
+
file_size = humanbytes(file_info['file_size'])
|
| 86 |
+
mime_type = file_info['mime_type']
|
| 87 |
+
|
| 88 |
+
page_link = f"{Server.URL}watch/{_id}"
|
| 89 |
+
stream_link = f"{Server.URL}dl/{_id}"
|
| 90 |
+
file_link = f"https://t.me/{SystummBot.username}?start=file_{_id}"
|
| 91 |
+
|
| 92 |
+
if "video" in mime_type:
|
| 93 |
+
stream_text = LANG.STREAM_TEXT.format(file_name, file_size, stream_link, page_link, file_link)
|
| 94 |
+
reply_markup = InlineKeyboardMarkup(
|
| 95 |
+
[
|
| 96 |
+
[InlineKeyboardButton("sᴛʀᴇᴀᴍ", url=page_link), InlineKeyboardButton("ᴅᴏᴡɴʟᴏᴀᴅ", url=stream_link)],
|
| 97 |
+
[InlineKeyboardButton("ɢᴇᴛ ғɪʟᴇ", url=file_link), InlineKeyboardButton("ʀᴇᴠᴏᴋᴇ ғɪʟᴇ", callback_data=f"msgdelpvt_{_id}")],
|
| 98 |
+
[InlineKeyboardButton("ᴄʟᴏsᴇ", callback_data="close")]
|
| 99 |
+
]
|
| 100 |
+
)
|
| 101 |
+
else:
|
| 102 |
+
stream_text = LANG.STREAM_TEXT_X.format(file_name, file_size, stream_link, file_link)
|
| 103 |
+
reply_markup = InlineKeyboardMarkup(
|
| 104 |
+
[
|
| 105 |
+
[InlineKeyboardButton("ᴅᴏᴡɴʟᴏᴀᴅ", url=stream_link)],
|
| 106 |
+
[InlineKeyboardButton("ɢᴇᴛ ғɪʟᴇ", url=file_link), InlineKeyboardButton("ʀᴇᴠᴏᴋᴇ ғɪʟᴇ", callback_data=f"msgdelpvt_{_id}")],
|
| 107 |
+
[InlineKeyboardButton("ᴄʟᴏsᴇ", callback_data="close")]
|
| 108 |
+
]
|
| 109 |
+
)
|
| 110 |
+
return reply_markup, stream_text
|
| 111 |
+
|
| 112 |
+
#---------------------[ GEN STREAM LINKS FOR CHANNEL ]---------------------#
|
| 113 |
+
|
| 114 |
+
async def gen_linkx(m:Message , _id, name: list):
|
| 115 |
+
file_info = await db.get_file(_id)
|
| 116 |
+
file_name = file_info['file_name']
|
| 117 |
+
mime_type = file_info['mime_type']
|
| 118 |
+
file_size = humanbytes(file_info['file_size'])
|
| 119 |
+
|
| 120 |
+
page_link = f"{Server.URL}watch/{_id}"
|
| 121 |
+
stream_link = f"{Server.URL}dl/{_id}"
|
| 122 |
+
file_link = f"https://t.me/{SystummBot.username}?start=file_{_id}"
|
| 123 |
+
|
| 124 |
+
if "video" in mime_type:
|
| 125 |
+
stream_text= LANG.STREAM_TEXT_X.format(file_name, file_size, stream_link, page_link)
|
| 126 |
+
reply_markup = InlineKeyboardMarkup(
|
| 127 |
+
[
|
| 128 |
+
[InlineKeyboardButton("sᴛʀᴇᴀᴍ", url=page_link), InlineKeyboardButton("ᴅᴏᴡɴʟᴏᴀᴅ", url=stream_link)]
|
| 129 |
+
]
|
| 130 |
+
)
|
| 131 |
+
else:
|
| 132 |
+
stream_text= LANG.STREAM_TEXT_X.format(file_name, file_size, stream_link, file_link)
|
| 133 |
+
reply_markup = InlineKeyboardMarkup(
|
| 134 |
+
[
|
| 135 |
+
[InlineKeyboardButton("ᴅᴏᴡɴʟᴏᴀᴅ", url=stream_link)]
|
| 136 |
+
]
|
| 137 |
+
)
|
| 138 |
+
return reply_markup, stream_text
|
| 139 |
+
|
| 140 |
+
#---------------------[ USER BANNED ]---------------------#
|
| 141 |
+
|
| 142 |
+
async def is_user_banned(message):
|
| 143 |
+
if await db.is_user_banned(message.from_user.id):
|
| 144 |
+
await message.reply_text(
|
| 145 |
+
text=LANG.BAN_TEXT.format(Telegram.OWNER_ID),
|
| 146 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 147 |
+
disable_web_page_preview=True
|
| 148 |
+
)
|
| 149 |
+
return True
|
| 150 |
+
return False
|
| 151 |
+
|
| 152 |
+
#---------------------[ CHANNEL BANNED ]---------------------#
|
| 153 |
+
|
| 154 |
+
async def is_channel_banned(bot, message):
|
| 155 |
+
if await db.is_user_banned(message.chat.id):
|
| 156 |
+
await bot.edit_message_reply_markup(
|
| 157 |
+
chat_id=message.chat.id,
|
| 158 |
+
message_id=message.id,
|
| 159 |
+
reply_markup=InlineKeyboardMarkup([[
|
| 160 |
+
InlineKeyboardButton(f"ᴄʜᴀɴɴᴇʟ ɪs ʙᴀɴɴᴇᴅ", callback_data="N/A")]])
|
| 161 |
+
)
|
| 162 |
+
return True
|
| 163 |
+
return False
|
| 164 |
+
|
| 165 |
+
#---------------------[ USER AUTH ]---------------------#
|
| 166 |
+
|
| 167 |
+
async def is_user_authorized(message):
|
| 168 |
+
if hasattr(Telegram, 'AUTH_USERS') and Telegram.AUTH_USERS:
|
| 169 |
+
user_id = message.from_user.id
|
| 170 |
+
|
| 171 |
+
if user_id == Telegram.OWNER_ID:
|
| 172 |
+
return True
|
| 173 |
+
|
| 174 |
+
if not (user_id in Telegram.AUTH_USERS):
|
| 175 |
+
await message.reply_text(
|
| 176 |
+
text="Yᴏᴜ ᴀʀᴇ ɴᴏᴛ ᴀᴜᴛʜᴏʀɪᴢᴇᴅ ᴛᴏ ᴜsᴇ ᴛʜɪs ʙᴏᴛ.\n\n_Made with ♥️ by Ajay Yadav_",
|
| 177 |
+
parse_mode=ParseMode.MARKDOWN,
|
| 178 |
+
disable_web_page_preview=True
|
| 179 |
+
)
|
| 180 |
+
return False
|
| 181 |
+
|
| 182 |
+
return True
|
| 183 |
+
|
| 184 |
+
#---------------------[ USER EXIST ]---------------------#
|
| 185 |
+
|
| 186 |
+
async def is_user_exist(bot, message):
|
| 187 |
+
if not bool(await db.get_user(message.from_user.id)):
|
| 188 |
+
await db.add_user(message.from_user.id)
|
| 189 |
+
await bot.send_message(
|
| 190 |
+
Telegram.ULOG_CHANNEL,
|
| 191 |
+
f"**#NᴇᴡUsᴇʀ**\n**⬩ ᴜsᴇʀ ɴᴀᴍᴇ :** [{message.from_user.first_name}](tg://user?id={message.from_user.id})\n**⬩ ᴜsᴇʀ ɪᴅ :** `{message.from_user.id}`"
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
async def is_channel_exist(bot, message):
|
| 195 |
+
if not bool(await db.get_user(message.chat.id)):
|
| 196 |
+
await db.add_user(message.chat.id)
|
| 197 |
+
members = await bot.get_chat_members_count(message.chat.id)
|
| 198 |
+
await bot.send_message(
|
| 199 |
+
Telegram.ULOG_CHANNEL,
|
| 200 |
+
f"**#NᴇᴡCʜᴀɴɴᴇʟ** \n**⬩ ᴄʜᴀᴛ ɴᴀᴍᴇ :** `{message.chat.title}`\n**⬩ ᴄʜᴀᴛ ɪᴅ :** `{message.chat.id}`\n**⬩ ᴛᴏᴛᴀʟ ᴍᴇᴍʙᴇʀs :** `{members}`"
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
async def verify_user(bot, message):
|
| 204 |
+
if not await is_user_authorized(message):
|
| 205 |
+
return False
|
| 206 |
+
|
| 207 |
+
if await is_user_banned(message):
|
| 208 |
+
return False
|
| 209 |
+
|
| 210 |
+
await is_user_exist(bot, message)
|
| 211 |
+
|
| 212 |
+
if Telegram.FORCE_SUB:
|
| 213 |
+
if not await is_user_joined(bot, message):
|
| 214 |
+
return False
|
| 215 |
+
|
| 216 |
+
return True
|
Systumm/utils/broadcast_helper.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import traceback
|
| 3 |
+
from pyrogram.errors import FloodWait, InputUserDeactivated, UserIsBlocked, PeerIdInvalid
|
| 4 |
+
|
| 5 |
+
async def send_msg(user_id, message):
|
| 6 |
+
try:
|
| 7 |
+
await message.copy(chat_id=user_id)
|
| 8 |
+
return 200, None
|
| 9 |
+
except FloodWait as e:
|
| 10 |
+
await asyncio.sleep(e.value)
|
| 11 |
+
return send_msg(user_id, message)
|
| 12 |
+
except InputUserDeactivated:
|
| 13 |
+
return 400, f"{user_id} : deactivated\n"
|
| 14 |
+
except UserIsBlocked:
|
| 15 |
+
return 400, f"{user_id} : blocked the bot\n"
|
| 16 |
+
except PeerIdInvalid:
|
| 17 |
+
return 400, f"{user_id} : user id invalid\n"
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return 500, f"{user_id} : {traceback.format_exc()}\n"
|
Systumm/utils/custom_dl.py
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import logging
|
| 3 |
+
from typing import Dict, Union
|
| 4 |
+
|
| 5 |
+
# --- SYSTUMM IMPORTS ---
|
| 6 |
+
from Systumm.bot import work_loads
|
| 7 |
+
from pyrogram import Client, utils, raw
|
| 8 |
+
from .file_properties import get_file_ids
|
| 9 |
+
from pyrogram.session import Session, Auth
|
| 10 |
+
from pyrogram.errors import AuthBytesInvalid
|
| 11 |
+
from pyrogram.file_id import FileId, FileType, ThumbnailSource
|
| 12 |
+
from pyrogram.types import Message
|
| 13 |
+
|
| 14 |
+
class ByteStreamer:
|
| 15 |
+
def __init__(self, client: Client):
|
| 16 |
+
self.clean_timer = 30 * 60
|
| 17 |
+
self.client: Client = client
|
| 18 |
+
self.cached_file_ids: Dict[str, FileId] = {}
|
| 19 |
+
asyncio.create_task(self.clean_cache())
|
| 20 |
+
|
| 21 |
+
async def get_file_properties(self, db_id: str, multi_clients) -> FileId:
|
| 22 |
+
"""
|
| 23 |
+
Returns the properties of a media of a specific message in a FIleId class.
|
| 24 |
+
if the properties are cached, then it'll return the cached results.
|
| 25 |
+
or it'll generate the properties from the Message ID and cache them.
|
| 26 |
+
"""
|
| 27 |
+
if not db_id in self.cached_file_ids:
|
| 28 |
+
logging.debug("Before Calling generate_file_properties")
|
| 29 |
+
await self.generate_file_properties(db_id, multi_clients)
|
| 30 |
+
logging.debug(f"Cached file properties for file with ID {db_id}")
|
| 31 |
+
return self.cached_file_ids[db_id]
|
| 32 |
+
|
| 33 |
+
async def generate_file_properties(self, db_id: str, multi_clients) -> FileId:
|
| 34 |
+
"""
|
| 35 |
+
Generates the properties of a media file on a specific message.
|
| 36 |
+
returns ths properties in a FIleId class.
|
| 37 |
+
"""
|
| 38 |
+
logging.debug("Before calling get_file_ids")
|
| 39 |
+
file_id = await get_file_ids(self.client, db_id, multi_clients, Message)
|
| 40 |
+
logging.debug(f"Generated file ID and Unique ID for file with ID {db_id}")
|
| 41 |
+
self.cached_file_ids[db_id] = file_id
|
| 42 |
+
logging.debug(f"Cached media file with ID {db_id}")
|
| 43 |
+
return self.cached_file_ids[db_id]
|
| 44 |
+
|
| 45 |
+
async def generate_media_session(self, client: Client, file_id: FileId) -> Session:
|
| 46 |
+
"""
|
| 47 |
+
Generates the media session for the DC that contains the media file.
|
| 48 |
+
This is required for getting the bytes from Telegram servers.
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
media_session = client.media_sessions.get(file_id.dc_id, None)
|
| 52 |
+
|
| 53 |
+
if media_session is None:
|
| 54 |
+
if file_id.dc_id != await client.storage.dc_id():
|
| 55 |
+
media_session = Session(
|
| 56 |
+
client,
|
| 57 |
+
file_id.dc_id,
|
| 58 |
+
await Auth(
|
| 59 |
+
client, file_id.dc_id, await client.storage.test_mode()
|
| 60 |
+
).create(),
|
| 61 |
+
await client.storage.test_mode(),
|
| 62 |
+
is_media=True,
|
| 63 |
+
)
|
| 64 |
+
await media_session.start()
|
| 65 |
+
|
| 66 |
+
for _ in range(6):
|
| 67 |
+
exported_auth = await client.invoke(
|
| 68 |
+
raw.functions.auth.ExportAuthorization(dc_id=file_id.dc_id)
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
await media_session.invoke(
|
| 73 |
+
raw.functions.auth.ImportAuthorization(
|
| 74 |
+
id=exported_auth.id, bytes=exported_auth.bytes
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
break
|
| 78 |
+
except AuthBytesInvalid:
|
| 79 |
+
logging.debug(
|
| 80 |
+
f"Invalid authorization bytes for DC {file_id.dc_id}"
|
| 81 |
+
)
|
| 82 |
+
continue
|
| 83 |
+
else:
|
| 84 |
+
await media_session.stop()
|
| 85 |
+
raise AuthBytesInvalid
|
| 86 |
+
else:
|
| 87 |
+
media_session = Session(
|
| 88 |
+
client,
|
| 89 |
+
file_id.dc_id,
|
| 90 |
+
await client.storage.auth_key(),
|
| 91 |
+
await client.storage.test_mode(),
|
| 92 |
+
is_media=True,
|
| 93 |
+
)
|
| 94 |
+
await media_session.start()
|
| 95 |
+
logging.debug(f"Created media session for DC {file_id.dc_id}")
|
| 96 |
+
client.media_sessions[file_id.dc_id] = media_session
|
| 97 |
+
else:
|
| 98 |
+
logging.debug(f"Using cached media session for DC {file_id.dc_id}")
|
| 99 |
+
return media_session
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
@staticmethod
|
| 103 |
+
async def get_location(file_id: FileId) -> Union[raw.types.InputPhotoFileLocation,
|
| 104 |
+
raw.types.InputDocumentFileLocation,
|
| 105 |
+
raw.types.InputPeerPhotoFileLocation,]:
|
| 106 |
+
"""
|
| 107 |
+
Returns the file location for the media file.
|
| 108 |
+
"""
|
| 109 |
+
file_type = file_id.file_type
|
| 110 |
+
|
| 111 |
+
if file_type == FileType.CHAT_PHOTO:
|
| 112 |
+
if file_id.chat_id > 0:
|
| 113 |
+
peer = raw.types.InputPeerUser(
|
| 114 |
+
user_id=file_id.chat_id, access_hash=file_id.chat_access_hash
|
| 115 |
+
)
|
| 116 |
+
else:
|
| 117 |
+
if file_id.chat_access_hash == 0:
|
| 118 |
+
peer = raw.types.InputPeerChat(chat_id=-file_id.chat_id)
|
| 119 |
+
else:
|
| 120 |
+
peer = raw.types.InputPeerChannel(
|
| 121 |
+
channel_id=utils.get_channel_id(file_id.chat_id),
|
| 122 |
+
access_hash=file_id.chat_access_hash,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
location = raw.types.InputPeerPhotoFileLocation(
|
| 126 |
+
peer=peer,
|
| 127 |
+
volume_id=file_id.volume_id,
|
| 128 |
+
local_id=file_id.local_id,
|
| 129 |
+
big=file_id.thumbnail_source == ThumbnailSource.CHAT_PHOTO_BIG,
|
| 130 |
+
)
|
| 131 |
+
elif file_type == FileType.PHOTO:
|
| 132 |
+
location = raw.types.InputPhotoFileLocation(
|
| 133 |
+
id=file_id.media_id,
|
| 134 |
+
access_hash=file_id.access_hash,
|
| 135 |
+
file_reference=file_id.file_reference,
|
| 136 |
+
thumb_size=file_id.thumbnail_size,
|
| 137 |
+
)
|
| 138 |
+
else:
|
| 139 |
+
location = raw.types.InputDocumentFileLocation(
|
| 140 |
+
id=file_id.media_id,
|
| 141 |
+
access_hash=file_id.access_hash,
|
| 142 |
+
file_reference=file_id.file_reference,
|
| 143 |
+
thumb_size=file_id.thumbnail_size,
|
| 144 |
+
)
|
| 145 |
+
return location
|
| 146 |
+
|
| 147 |
+
async def yield_file(
|
| 148 |
+
self,
|
| 149 |
+
file_id: FileId,
|
| 150 |
+
index: int,
|
| 151 |
+
offset: int,
|
| 152 |
+
first_part_cut: int,
|
| 153 |
+
last_part_cut: int,
|
| 154 |
+
part_count: int,
|
| 155 |
+
chunk_size: int,
|
| 156 |
+
) -> Union[str, None]:
|
| 157 |
+
"""
|
| 158 |
+
Custom generator that yields the bytes of the media file.
|
| 159 |
+
Modded from <https://github.com/eyaadh/megadlbot_oss/blob/master/mega/telegram/utils/custom_download.py#L20>
|
| 160 |
+
Thanks to Eyaadh <https://github.com/eyaadh>
|
| 161 |
+
"""
|
| 162 |
+
client = self.client
|
| 163 |
+
work_loads[index] += 1
|
| 164 |
+
logging.debug(f"Starting to yielding file with client {index}.")
|
| 165 |
+
media_session = await self.generate_media_session(client, file_id)
|
| 166 |
+
|
| 167 |
+
current_part = 1
|
| 168 |
+
|
| 169 |
+
location = await self.get_location(file_id)
|
| 170 |
+
|
| 171 |
+
try:
|
| 172 |
+
r = await media_session.invoke(
|
| 173 |
+
raw.functions.upload.GetFile(
|
| 174 |
+
location=location, offset=offset, limit=chunk_size
|
| 175 |
+
),
|
| 176 |
+
)
|
| 177 |
+
if isinstance(r, raw.types.upload.File):
|
| 178 |
+
while True:
|
| 179 |
+
chunk = r.bytes
|
| 180 |
+
if not chunk:
|
| 181 |
+
break
|
| 182 |
+
elif part_count == 1:
|
| 183 |
+
yield chunk[first_part_cut:last_part_cut]
|
| 184 |
+
elif current_part == 1:
|
| 185 |
+
yield chunk[first_part_cut:]
|
| 186 |
+
elif current_part == part_count:
|
| 187 |
+
yield chunk[:last_part_cut]
|
| 188 |
+
else:
|
| 189 |
+
yield chunk
|
| 190 |
+
|
| 191 |
+
current_part += 1
|
| 192 |
+
offset += chunk_size
|
| 193 |
+
|
| 194 |
+
if current_part > part_count:
|
| 195 |
+
break
|
| 196 |
+
|
| 197 |
+
r = await media_session.invoke(
|
| 198 |
+
raw.functions.upload.GetFile(
|
| 199 |
+
location=location, offset=offset, limit=chunk_size
|
| 200 |
+
),
|
| 201 |
+
)
|
| 202 |
+
except (TimeoutError, AttributeError):
|
| 203 |
+
pass
|
| 204 |
+
finally:
|
| 205 |
+
logging.debug(f"Finished yielding file with {current_part} parts.")
|
| 206 |
+
work_loads[index] -= 1
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
async def clean_cache(self) -> None:
|
| 210 |
+
"""
|
| 211 |
+
function to clean the cache to reduce memory usage
|
| 212 |
+
"""
|
| 213 |
+
while True:
|
| 214 |
+
await asyncio.sleep(self.clean_timer)
|
| 215 |
+
self.cached_file_ids.clear()
|
| 216 |
+
logging.debug("Cleaned the cache")
|
Systumm/utils/database.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pymongo
|
| 2 |
+
import time
|
| 3 |
+
import motor.motor_asyncio
|
| 4 |
+
from bson.objectid import ObjectId
|
| 5 |
+
from bson.errors import InvalidId
|
| 6 |
+
|
| 7 |
+
# --- SYSTUMM IMPORTS ---
|
| 8 |
+
from Systumm.server.exceptions import FIleNotFound
|
| 9 |
+
|
| 10 |
+
class Database:
|
| 11 |
+
def __init__(self, uri, database_name):
|
| 12 |
+
self._client = motor.motor_asyncio.AsyncIOMotorClient(uri)
|
| 13 |
+
self.db = self._client[database_name]
|
| 14 |
+
self.col = self.db.users
|
| 15 |
+
self.black = self.db.blacklist
|
| 16 |
+
self.file = self.db.file
|
| 17 |
+
|
| 18 |
+
#---------------------[ NEW USER ]---------------------#
|
| 19 |
+
def new_user(self, id):
|
| 20 |
+
return dict(
|
| 21 |
+
id=id,
|
| 22 |
+
join_date=time.time(),
|
| 23 |
+
Links=0
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# ---------------------[ ADD USER ]---------------------#
|
| 27 |
+
async def add_user(self, id):
|
| 28 |
+
user = self.new_user(id)
|
| 29 |
+
await self.col.insert_one(user)
|
| 30 |
+
|
| 31 |
+
# ---------------------[ GET USER ]---------------------#
|
| 32 |
+
async def get_user(self, id):
|
| 33 |
+
user = await self.col.find_one({'id': int(id)})
|
| 34 |
+
return user
|
| 35 |
+
|
| 36 |
+
# ---------------------[ CHECK USER ]---------------------#
|
| 37 |
+
async def total_users_count(self):
|
| 38 |
+
count = await self.col.count_documents({})
|
| 39 |
+
return count
|
| 40 |
+
|
| 41 |
+
async def get_all_users(self):
|
| 42 |
+
all_users = self.col.find({})
|
| 43 |
+
return all_users
|
| 44 |
+
|
| 45 |
+
# ---------------------[ REMOVE USER ]---------------------#
|
| 46 |
+
async def delete_user(self, user_id):
|
| 47 |
+
await self.col.delete_many({'id': int(user_id)})
|
| 48 |
+
|
| 49 |
+
# ---------------------[ BAN, UNBAN USER ]---------------------#
|
| 50 |
+
def black_user(self, id):
|
| 51 |
+
return dict(
|
| 52 |
+
id=id,
|
| 53 |
+
ban_date=time.time()
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
async def ban_user(self, id):
|
| 57 |
+
user = self.black_user(id)
|
| 58 |
+
await self.black.insert_one(user)
|
| 59 |
+
|
| 60 |
+
async def unban_user(self, id):
|
| 61 |
+
await self.black.delete_one({'id': int(id)})
|
| 62 |
+
|
| 63 |
+
async def is_user_banned(self, id):
|
| 64 |
+
user = await self.black.find_one({'id': int(id)})
|
| 65 |
+
return True if user else False
|
| 66 |
+
|
| 67 |
+
async def total_banned_users_count(self):
|
| 68 |
+
count = await self.black.count_documents({})
|
| 69 |
+
return count
|
| 70 |
+
|
| 71 |
+
# ---------------------[ ADD FILE TO DB ]---------------------#
|
| 72 |
+
async def add_file(self, file_info):
|
| 73 |
+
file_info["time"] = time.time()
|
| 74 |
+
fetch_old = await self.get_file_by_fileuniqueid(file_info["user_id"], file_info["file_unique_id"])
|
| 75 |
+
if fetch_old:
|
| 76 |
+
return fetch_old["_id"]
|
| 77 |
+
await self.count_links(file_info["user_id"], "+")
|
| 78 |
+
return (await self.file.insert_one(file_info)).inserted_id
|
| 79 |
+
|
| 80 |
+
# ---------------------[ FIND FILE IN DB ]---------------------#
|
| 81 |
+
async def find_files(self, user_id, range):
|
| 82 |
+
user_files=self.file.find({"user_id": user_id})
|
| 83 |
+
user_files.skip(range[0] - 1)
|
| 84 |
+
user_files.limit(range[1] - range[0] + 1)
|
| 85 |
+
user_files.sort('_id', pymongo.DESCENDING)
|
| 86 |
+
total_files = await self.file.count_documents({"user_id": user_id})
|
| 87 |
+
return user_files, total_files
|
| 88 |
+
|
| 89 |
+
async def get_file(self, _id):
|
| 90 |
+
try:
|
| 91 |
+
file_info=await self.file.find_one({"_id": ObjectId(_id)})
|
| 92 |
+
if not file_info:
|
| 93 |
+
raise FIleNotFound
|
| 94 |
+
return file_info
|
| 95 |
+
except InvalidId:
|
| 96 |
+
raise FIleNotFound
|
| 97 |
+
|
| 98 |
+
async def get_file_by_fileuniqueid(self, id, file_unique_id, many=False):
|
| 99 |
+
if many:
|
| 100 |
+
return self.file.find({"file_unique_id": file_unique_id})
|
| 101 |
+
else:
|
| 102 |
+
file_info=await self.file.find_one({"user_id": id, "file_unique_id": file_unique_id})
|
| 103 |
+
if file_info:
|
| 104 |
+
return file_info
|
| 105 |
+
return False
|
| 106 |
+
|
| 107 |
+
# ---------------------[ TOTAL FILES ]---------------------#
|
| 108 |
+
async def total_files(self, id=None):
|
| 109 |
+
if id:
|
| 110 |
+
return await self.file.count_documents({"user_id": id})
|
| 111 |
+
return await self.file.count_documents({})
|
| 112 |
+
|
| 113 |
+
# ---------------------[ DELETE FILES ]---------------------#
|
| 114 |
+
async def delete_one_file(self, _id):
|
| 115 |
+
await self.file.delete_one({'_id': ObjectId(_id)})
|
| 116 |
+
|
| 117 |
+
# ---------------------[ UPDATE FILES ]---------------------#
|
| 118 |
+
async def update_file_ids(self, _id, file_ids: dict):
|
| 119 |
+
await self.file.update_one({"_id": ObjectId(_id)}, {"$set": {"file_ids": file_ids}})
|
| 120 |
+
|
| 121 |
+
# ---------------------[ PAID SYS ]---------------------#
|
| 122 |
+
async def count_links(self, id, operation: str):
|
| 123 |
+
if operation == "-":
|
| 124 |
+
await self.col.update_one({"id": id}, {"$inc": {"Links": -1}})
|
| 125 |
+
elif operation == "+":
|
| 126 |
+
await self.col.update_one({"id": id}, {"$inc": {"Links": 1}})
|
Systumm/utils/file_properties.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import logging
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from pyrogram import Client
|
| 5 |
+
from typing import Any, Optional
|
| 6 |
+
from pyrogram.enums import ParseMode, ChatType
|
| 7 |
+
from pyrogram.types import Message
|
| 8 |
+
from pyrogram.file_id import FileId
|
| 9 |
+
|
| 10 |
+
# --- SYSTUMM IMPORTS ---
|
| 11 |
+
from Systumm.bot import SystummBot
|
| 12 |
+
from Systumm.utils.database import Database
|
| 13 |
+
from Systumm.config import Telegram, Server
|
| 14 |
+
|
| 15 |
+
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
async def get_file_ids(client: Client | bool, db_id: str, multi_clients, message) -> Optional[FileId]:
|
| 19 |
+
logging.debug("Starting of get_file_ids")
|
| 20 |
+
file_info = await db.get_file(db_id)
|
| 21 |
+
if (not "file_ids" in file_info) or not client:
|
| 22 |
+
logging.debug("Storing file_id of all clients in DB")
|
| 23 |
+
log_msg = await send_file(SystummBot, db_id, file_info['file_id'], message)
|
| 24 |
+
await db.update_file_ids(db_id, await update_file_id(log_msg.id, multi_clients))
|
| 25 |
+
logging.debug("Stored file_id of all clients in DB")
|
| 26 |
+
if not client:
|
| 27 |
+
return
|
| 28 |
+
file_info = await db.get_file(db_id)
|
| 29 |
+
|
| 30 |
+
file_id_info = file_info.setdefault("file_ids", {})
|
| 31 |
+
if not str(client.id) in file_id_info:
|
| 32 |
+
logging.debug("Storing file_id in DB")
|
| 33 |
+
log_msg = await send_file(SystummBot, db_id, file_info['file_id'], message)
|
| 34 |
+
msg = await client.get_messages(Telegram.FLOG_CHANNEL, log_msg.id)
|
| 35 |
+
media = get_media_from_message(msg)
|
| 36 |
+
file_id_info[str(client.id)] = getattr(media, "file_id", "")
|
| 37 |
+
await db.update_file_ids(db_id, file_id_info)
|
| 38 |
+
logging.debug("Stored file_id in DB")
|
| 39 |
+
|
| 40 |
+
logging.debug("Middle of get_file_ids")
|
| 41 |
+
file_id = FileId.decode(file_id_info[str(client.id)])
|
| 42 |
+
setattr(file_id, "file_size", file_info['file_size'])
|
| 43 |
+
setattr(file_id, "mime_type", file_info['mime_type'])
|
| 44 |
+
setattr(file_id, "file_name", file_info['file_name'])
|
| 45 |
+
setattr(file_id, "unique_id", file_info['file_unique_id'])
|
| 46 |
+
logging.debug("Ending of get_file_ids")
|
| 47 |
+
return file_id
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def get_media_from_message(message: "Message") -> Any:
|
| 51 |
+
media_types = (
|
| 52 |
+
"audio",
|
| 53 |
+
"document",
|
| 54 |
+
"photo",
|
| 55 |
+
"sticker",
|
| 56 |
+
"animation",
|
| 57 |
+
"video",
|
| 58 |
+
"voice",
|
| 59 |
+
"video_note",
|
| 60 |
+
)
|
| 61 |
+
for attr in media_types:
|
| 62 |
+
media = getattr(message, attr, None)
|
| 63 |
+
if media:
|
| 64 |
+
return media
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def get_media_file_size(m):
|
| 68 |
+
media = get_media_from_message(m)
|
| 69 |
+
return getattr(media, "file_size", "None")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def get_name(media_msg: Message | FileId) -> str:
|
| 73 |
+
if isinstance(media_msg, Message):
|
| 74 |
+
media = get_media_from_message(media_msg)
|
| 75 |
+
file_name = getattr(media, "file_name", "")
|
| 76 |
+
|
| 77 |
+
elif isinstance(media_msg, FileId):
|
| 78 |
+
file_name = getattr(media_msg, "file_name", "")
|
| 79 |
+
|
| 80 |
+
if not file_name:
|
| 81 |
+
if isinstance(media_msg, Message) and media_msg.media:
|
| 82 |
+
media_type = media_msg.media.value
|
| 83 |
+
elif media_msg.file_type:
|
| 84 |
+
media_type = media_msg.file_type.name.lower()
|
| 85 |
+
else:
|
| 86 |
+
media_type = "file"
|
| 87 |
+
|
| 88 |
+
formats = {
|
| 89 |
+
"photo": "jpg", "audio": "mp3", "voice": "ogg",
|
| 90 |
+
"video": "mp4", "animation": "mp4", "video_note": "mp4",
|
| 91 |
+
"sticker": "webp"
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
ext = formats.get(media_type)
|
| 95 |
+
ext = "." + ext if ext else ""
|
| 96 |
+
|
| 97 |
+
date = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 98 |
+
file_name = f"{media_type}-{date}{ext}"
|
| 99 |
+
|
| 100 |
+
return file_name
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def get_file_info(message):
|
| 104 |
+
media = get_media_from_message(message)
|
| 105 |
+
if message.chat.type == ChatType.PRIVATE:
|
| 106 |
+
user_idx = message.from_user.id
|
| 107 |
+
else:
|
| 108 |
+
user_idx = message.chat.id
|
| 109 |
+
return {
|
| 110 |
+
"user_id": user_idx,
|
| 111 |
+
"file_id": getattr(media, "file_id", ""),
|
| 112 |
+
"file_unique_id": getattr(media, "file_unique_id", ""),
|
| 113 |
+
"file_name": get_name(message),
|
| 114 |
+
"file_size": getattr(media, "file_size", 0),
|
| 115 |
+
"mime_type": getattr(media, "mime_type", "None/unknown")
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
async def update_file_id(msg_id, multi_clients):
|
| 120 |
+
file_ids = {}
|
| 121 |
+
for client_id, client in multi_clients.items():
|
| 122 |
+
log_msg = await client.get_messages(Telegram.FLOG_CHANNEL, msg_id)
|
| 123 |
+
media = get_media_from_message(log_msg)
|
| 124 |
+
file_ids[str(client.id)] = getattr(media, "file_id", "")
|
| 125 |
+
|
| 126 |
+
return file_ids
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
async def send_file(client: Client, db_id, file_id: str, message):
|
| 130 |
+
file_caption = getattr(message, 'caption', None) or get_name(message)
|
| 131 |
+
log_msg = await client.send_cached_media(chat_id=Telegram.FLOG_CHANNEL, file_id=file_id,
|
| 132 |
+
caption=f'**{file_caption}**\n\n_Logged by Systumm Bot_')
|
| 133 |
+
|
| 134 |
+
if message.chat.type == ChatType.PRIVATE:
|
| 135 |
+
await log_msg.reply_text(
|
| 136 |
+
text=f"**RᴇQᴜᴇꜱᴛᴇᴅ ʙʏ :** [{message.from_user.first_name}](tg://user?id={message.from_user.id})\n**Uꜱᴇʀ ɪᴅ :** `{message.from_user.id}`\n**Fɪʟᴇ ɪᴅ :** `{db_id}`",
|
| 137 |
+
disable_web_page_preview=True, parse_mode=ParseMode.MARKDOWN, quote=True)
|
| 138 |
+
else:
|
| 139 |
+
await log_msg.reply_text(
|
| 140 |
+
text=f"**RᴇQᴜᴇꜱᴛᴇᴅ ʙʏ :** {message.chat.title} \n**Cʜᴀɴɴᴇʟ ɪᴅ :** `{message.chat.id}`\n**Fɪʟᴇ ɪᴅ :** `{db_id}`",
|
| 141 |
+
disable_web_page_preview=True, parse_mode=ParseMode.MARKDOWN, quote=True)
|
| 142 |
+
|
| 143 |
+
return log_msg
|
Systumm/utils/human_readable.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def humanbytes(size):
|
| 2 |
+
if not size:
|
| 3 |
+
return ""
|
| 4 |
+
power = 2**10
|
| 5 |
+
n = 0
|
| 6 |
+
Dic_powerN = {0: ' ', 1: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'}
|
| 7 |
+
while size > power:
|
| 8 |
+
size /= power
|
| 9 |
+
n += 1
|
| 10 |
+
return str(round(size, 2)) + " " + Dic_powerN[n] + 'B'
|
Systumm/utils/render_template.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import aiohttp
|
| 2 |
+
import jinja2
|
| 3 |
+
import urllib.parse
|
| 4 |
+
from Systumm.config import Telegram, Server
|
| 5 |
+
from Systumm.utils.database import Database
|
| 6 |
+
from Systumm.utils.human_readable import humanbytes
|
| 7 |
+
from Systumm.server.exceptions import FIleNotFound
|
| 8 |
+
|
| 9 |
+
db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME)
|
| 10 |
+
|
| 11 |
+
async def render_page(db_id):
|
| 12 |
+
file_data = await db.get_file(db_id)
|
| 13 |
+
|
| 14 |
+
if not file_data:
|
| 15 |
+
# Raising this allows stream_routes.py to catch it and show a proper 404
|
| 16 |
+
raise FIleNotFound("File not found in database")
|
| 17 |
+
|
| 18 |
+
src = urllib.parse.urljoin(Server.URL, f'dl/{file_data["_id"]}')
|
| 19 |
+
file_size = humanbytes(file_data['file_size'])
|
| 20 |
+
file_name = file_data['file_name'].replace("_", " ")
|
| 21 |
+
|
| 22 |
+
if str((file_data['mime_type']).split('/')[0].strip()) == 'video':
|
| 23 |
+
template_file = "Systumm/template/play.html"
|
| 24 |
+
else:
|
| 25 |
+
template_file = "Systumm/template/dl.html"
|
| 26 |
+
|
| 27 |
+
with open(template_file) as f:
|
| 28 |
+
template = jinja2.Template(f.read())
|
| 29 |
+
|
| 30 |
+
return template.render(
|
| 31 |
+
file_name=file_name,
|
| 32 |
+
file_url=src,
|
| 33 |
+
file_size=file_size
|
| 34 |
+
)
|
Systumm/utils/time_format.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def get_readable_time(seconds: int) -> str:
|
| 2 |
+
count = 0
|
| 3 |
+
readable_time = ""
|
| 4 |
+
time_list = []
|
| 5 |
+
time_suffix_list = ["s", "m", "h", " days"]
|
| 6 |
+
while count < 4:
|
| 7 |
+
count += 1
|
| 8 |
+
if count < 3:
|
| 9 |
+
remainder, result = divmod(seconds, 60)
|
| 10 |
+
else:
|
| 11 |
+
remainder, result = divmod(seconds, 24)
|
| 12 |
+
if seconds == 0 and remainder == 0:
|
| 13 |
+
break
|
| 14 |
+
time_list.append(int(result))
|
| 15 |
+
seconds = int(remainder)
|
| 16 |
+
for x in range(len(time_list)):
|
| 17 |
+
time_list[x] = str(time_list[x]) + time_suffix_list[x]
|
| 18 |
+
if len(time_list) == 4:
|
| 19 |
+
readable_time += time_list.pop() + ", "
|
| 20 |
+
time_list.reverse()
|
| 21 |
+
readable_time += ": ".join(time_list)
|
| 22 |
+
return readable_time
|
Systumm/utils/translation.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
| 2 |
+
from Systumm.config import Telegram
|
| 3 |
+
|
| 4 |
+
class LANG(object):
|
| 5 |
+
|
| 6 |
+
START_TEXT = """
|
| 7 |
+
<b>👋 Hᴇʏ, </b>{}\n
|
| 8 |
+
<b>I'ᴍ ᴛᴇʟᴇɢʀᴀᴍ ғɪʟᴇs sᴛʀᴇᴀᴍɪɴɢ ʙᴏᴛ ᴀs ᴡᴇʟʟ ᴅɪʀᴇᴄᴛ ʟɪɴᴋs ɢᴇɴᴇʀᴀᴛᴏʀ</b>\n
|
| 9 |
+
<b>ᴡᴏʀᴋɪɴɢ ᴏɴ ᴄʜᴀɴɴᴇʟs ᴀɴᴅ ᴘʀɪᴠᴀᴛᴇ ᴄʜᴀᴛ</b>\n
|
| 10 |
+
<b>💕 @{}</b>\n"""
|
| 11 |
+
|
| 12 |
+
HELP_TEXT = """
|
| 13 |
+
<b>- ᴀᴅᴅ ᴍᴇ ᴀs ᴀɴ ᴀᴅᴍɪɴ ᴏɴ ᴛʜᴇ ᴄʜᴀɴɴᴇʟ</b>
|
| 14 |
+
<b>- sᴇɴᴅ ᴍᴇ ᴀɴʏ ᴅᴏᴄᴜᴍᴇɴᴛ ᴏʀ ᴍᴇᴅɪᴀ</b>
|
| 15 |
+
<b>- ɪ'ʟʟ ᴘʀᴏᴠɪᴅᴇ sᴛʀᴇᴀᴍᴀʙʟᴇ ʟɪɴᴋ</b>\n
|
| 16 |
+
<b>🔞 ᴀᴅᴜʟᴛ ᴄᴏɴᴛᴇɴᴛ sᴛʀɪᴄᴛʟʏ ᴘʀᴏʜɪʙɪᴛᴇᴅ.</b>\n"""
|
| 17 |
+
|
| 18 |
+
ABOUT_TEXT = """
|
| 19 |
+
<b>⚜ ᴍʏ ɴᴀᴍᴇ : Systumm File to Link Bot</b>\n
|
| 20 |
+
<b>✦ ᴠᴇʀsɪᴏɴ : {}</b>
|
| 21 |
+
<b>✦ ᴅᴇᴠᴇʟᴏᴘᴇʀ : Ajay Yadav</b>\n
|
| 22 |
+
<b>Made with ♥️ by Ajay Yadav</b>
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
STREAM_TEXT = """
|
| 26 |
+
<i><u>𝗬𝗼𝘂𝗿 𝗟𝗶𝗻𝗸 𝗚𝗲𝗻𝗲𝗿𝗮𝘁𝗲𝗱 !</u></i>\n
|
| 27 |
+
<b>📂 Fɪʟᴇ ɴᴀᴍᴇ :</b> <b>{}</b>\n
|
| 28 |
+
<b>📦 Fɪʟᴇ ꜱɪᴢᴇ :</b> <code>{}</code>\n
|
| 29 |
+
<b>📥 Dᴏᴡɴʟᴏᴀᴅ :</b> <code>{}</code>\n
|
| 30 |
+
<b>🖥 Wᴀᴛᴄʜ :</b> <code>{}</code>\n
|
| 31 |
+
<b>🔗 Sʜᴀʀᴇ :</b> <code>{}</code>\n\n
|
| 32 |
+
<b>Made with ♥️ by Ajay Yadav</b>"""
|
| 33 |
+
|
| 34 |
+
STREAM_TEXT_X = """
|
| 35 |
+
<i><u>𝗬𝗼𝘂𝗿 𝗟𝗶𝗻𝗸 𝗚𝗲𝗻𝗲𝗿𝗮𝘁𝗲𝗱 !</u></i>\n
|
| 36 |
+
<b>📂 Fɪʟᴇ ɴᴀᴍᴇ :</b> <b>{}</b>\n
|
| 37 |
+
<b>📦 Fɪʟᴇ ꜱɪᴢᴇ :</b> <code>{}</code>\n
|
| 38 |
+
<b>📥 Dᴏᴡɴʟᴏᴀᴅ :</b> <code>{}</code>\n
|
| 39 |
+
<b>🔗 Sʜᴀʀᴇ :</b> <code>{}</code>\n\n
|
| 40 |
+
<b>Made with ♥️ by Ajay Yadav</b>"""
|
| 41 |
+
|
| 42 |
+
BAN_TEXT = "__Sᴏʀʀʏ Sɪʀ, Yᴏᴜ ᴀʀᴇ Bᴀɴɴᴇᴅ ᴛᴏ ᴜsᴇ ᴍᴇ.__\n\n**Cᴏɴᴛᴀᴄᴛ ᴛʜᴇ Dᴇᴠᴇʟᴏᴘᴇʀ ғᴏʀ ᴀssɪsᴛᴀɴᴄᴇ.**"
|
| 43 |
+
|
| 44 |
+
class BUTTON(object):
|
| 45 |
+
START_BUTTONS = InlineKeyboardMarkup(
|
| 46 |
+
[[
|
| 47 |
+
InlineKeyboardButton('ʜᴇʟᴘ', callback_data='help'),
|
| 48 |
+
InlineKeyboardButton('ᴀʙᴏᴜᴛ', callback_data='about'),
|
| 49 |
+
InlineKeyboardButton('ᴄʟᴏsᴇ', callback_data='close')
|
| 50 |
+
],
|
| 51 |
+
[InlineKeyboardButton("📢 ᴜᴘᴅᴀᴛᴇ ᴄʜᴀɴɴᴇʟ", url=f'https://t.me/{Telegram.UPDATES_CHANNEL}')]
|
| 52 |
+
]
|
| 53 |
+
)
|
| 54 |
+
HELP_BUTTONS = InlineKeyboardMarkup(
|
| 55 |
+
[[
|
| 56 |
+
InlineKeyboardButton('ʜᴏᴍᴇ', callback_data='home'),
|
| 57 |
+
InlineKeyboardButton('ᴀʙᴏᴜᴛ', callback_data='about'),
|
| 58 |
+
InlineKeyboardButton('ᴄʟᴏsᴇ', callback_data='close'),
|
| 59 |
+
],
|
| 60 |
+
[InlineKeyboardButton("📢 ᴜᴘᴅᴀᴛᴇ ᴄʜᴀɴɴᴇʟ", url=f'https://t.me/{Telegram.UPDATES_CHANNEL}')]
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
ABOUT_BUTTONS = InlineKeyboardMarkup(
|
| 64 |
+
[[
|
| 65 |
+
InlineKeyboardButton('ʜᴏᴍᴇ', callback_data='home'),
|
| 66 |
+
InlineKeyboardButton('ʜᴇʟᴘ', callback_data='help'),
|
| 67 |
+
InlineKeyboardButton('ᴄʟᴏsᴇ', callback_data='close'),
|
| 68 |
+
],
|
| 69 |
+
[InlineKeyboardButton("📢 ᴜᴘᴅᴀᴛᴇ ᴄʜᴀɴɴᴇʟ", url=f'https://t.me/{Telegram.UPDATES_CHANNEL}')]
|
| 70 |
+
]
|
| 71 |
+
)
|