import re from pyrogram import filters, Client, enums from pyrogram.errors.exceptions.bad_request_400 import ChannelInvalid, UsernameInvalid, UsernameNotModified from info import ADMINS, LOG_CHANNEL, FILE_STORE_CHANNEL, PUBLIC_FILE_STORE from database.ia_filterdb import unpack_new_file_id from utils import temp import re import os import json import base64 import logging logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) async def allowed(_, __, message): if PUBLIC_FILE_STORE: return True return bool(message.from_user and message.from_user.id in ADMINS) @Client.on_message(filters.command(['batch', 'pbatch']) & filters.create(allowed)) async def gen_link_batch(bot, message): # sourcery skip: low-code-quality if " " not in message.text: return await message.reply("Use correct format.\nExample /batch https://t.me/TeamEvamaria/10 https://t.me/TeamEvamaria/20.") links = message.text.strip().split(" ") if len(links) != 3: return await message.reply("Use correct format.\nExample /batch https://t.me/TeamEvamaria/10 https://t.me/TeamEvamaria/20.") cmd, first, last = links regex = re.compile( "(https://)?(t\.me/|telegram\.me/|telegram\.dog/)(c/)?(\d+|[a-zA-Z_0-9]+)/(\d+)$") match = regex.match(first) if not match: return await message.reply('Invalid link') f_chat_id = match[4] f_msg_id = int(match[5]) if f_chat_id.isnumeric(): f_chat_id = int(f"-100{f_chat_id}") match = regex.match(last) if not match: return await message.reply('Invalid link') l_chat_id = match[4] l_msg_id = int(match[5]) if l_chat_id.isnumeric(): l_chat_id = int(f"-100{l_chat_id}") if f_chat_id != l_chat_id: return await message.reply("Chat ids not matched.") try: chat_id = (await bot.get_chat(f_chat_id)).id except ChannelInvalid: return await message.reply('This may be a private channel / group. Make me an admin over there to index the files.') except (UsernameInvalid, UsernameNotModified): return await message.reply('Invalid Link specified.') except Exception as e: return await message.reply(f'Errors - {e}') sts = await message.reply("Generating link for your message.\nThis may take time depending upon number of messages") FRMT = "Generating Link...\nTotal Messages: `{total}`\nDone: `{current}`\nRemaining: `{rem}`\nStatus: `{sts}`" outlist = [] # file store without db channel og_msg = 0 tot = 0 async for msg in bot.iter_messages(f_chat_id, l_msg_id, f_msg_id): tot += 1 if msg.empty or msg.service: continue if not msg.media: # only media messages supported. continue try: file_type = msg.media file = getattr(msg, file_type.value) caption = getattr(msg, 'caption', '') if caption: caption = caption.html if file: file = { "file_id": file.file_id, "caption": caption, "title": getattr(file, "file_name", ""), "size": file.file_size, "protect": cmd.lower().strip() == "/pbatch", } og_msg += 1 outlist.append(file) except Exception: pass if not og_msg % 20: try: await sts.edit(FRMT.format(total=l_msg_id-f_msg_id, current=tot, rem=((l_msg_id-f_msg_id) - tot), sts="Saving Messages")) except Exception: pass with open(f"batchmode_{message.from_user.id}.json", "w+") as out: json.dump(outlist, out) post = await bot.send_document(LOG_CHANNEL, f"batchmode_{message.from_user.id}.json", file_name="Batch.json", caption="⚠️Generated for filestore.") os.remove(f"batchmode_{message.from_user.id}.json") file_id, ref = unpack_new_file_id(post.document.file_id) await sts.edit(f"Here is your link\nContains `{og_msg}` files.\n https://t.me/{temp.U_NAME}?start=BATCH-{file_id}")