Spaces:
Runtime error
Runtime error
Create bot.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128import osimport timeimport sqlite3import asynciofrom pyrogram import Client, filtersfrom pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
# --- Environment Variables ---API_ID = int(os.getenv("API_ID", 0))API_HASH = os.getenv("API_HASH")BOT_TOKEN = os.getenv("BOT_TOKEN")
# --- Database Setup ---DB_PATH = "bot_data.db"
def init_db(): conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cursor.execute('''CREATE TABLE IF NOT EXISTS history (user_id INTEGER, file_name TEXT, file_id TEXT)''') conn.commit() conn.close()
def add_to_history(user_id, name, fid): conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cursor.execute("INSERT INTO history VALUES (?, ?, ?)", (user_id, name, fid)) conn.commit() conn.close()
def get_history(user_id): conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cursor.execute("SELECT file_name, file_id FROM history WHERE user_id = ?", (user_id,)) rows = cursor.fetchall() conn.close() return rows
init_db()
# --- Bot Client ---app = Client( "FastRename", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN, workers=20 # Increase workers for parallel chunk handling)
@app.on_message(filters.document | filters.video)async def fast_process(client, message): user_id = message.from_user.id # Fast prompt for new name ask = await client.ask(message.chat.id, "π Send the **New Name** with extension:") new_name = ask.text status = await message.reply("β‘ **Starting High-Speed Stream...**") start_time = time.time()
# Download path = await client.download_media( message, file_name=new_name, progress=progress_callback, progress_args=(status, start_time, "Downloading") )
# Upload start_up = time.time() sent_file = await client.send_document( chat_id=message.chat.id, document=path, caption=f"β
**Done:** `{new_name}`", progress=progress_callback, progress_args=(status, start_up, "Uploading") )
# Store in SQLite new_fid = sent_file.document.file_id if sent_file.document else sent_file.video.file_id add_to_history(user_id, new_name, new_fid)
# Instant Cleanup if os.path.exists(path): os.remove(path) await status.delete()
async def progress_callback(current, total, status_msg, start_time, mode): now = time.time() diff = now - start_time if round(diff % 3.00) == 0 or current == total: speed = current / diff if diff > 0 else 0 eta = (total - current) / speed if speed > 0 else 0 try: await status_msg.edit( f"π **{mode}**\n" f"**Size:** {current/1024/1024:.2f}/{total/1024/1024:.2f} MB\n" f"**Speed:** {speed/1024/1024:.2f} MB/s\n" f"**ETA:** {int(eta)}s" ) except: pass
@app.on_message(filters.command("history"))async def show_history(client, message): rows = get_history(message.from_user.id) if not rows: return await message.reply("No history found.")
text = "π **Your File History:**\n\n" buttons = [] for idx, (name, fid) in enumerate(rows): # We use the index in callback_data to retrieve the fid text += f"{idx+1}. `{name}`\n" buttons.append([InlineKeyboardButton(f"Get {name}", callback_data=f"get_{idx}")])
await message.reply(text, reply_markup=InlineKeyboardMarkup(buttons))
@app.on_callback_query(filters.regex(r"^get_"))async def send_from_history(client, query): idx = int(query.data.split("_")[1]) rows = get_history(query.from_user.id) file_name, file_id = rows[idx] await client.send_document(query.message.chat.id, file_id, caption=f"Retrieved: {file_name}") await query.answer()
app.run()bot.pyAdded+128