Spaces:
Runtime error
Runtime error
Update bot.py
Browse files1 unmodified line23456789101112131415161718192021222334 unmodified lines585960476162635064656667546869707172737475616263646566676869767778798081828384858687888971727374757677787980818283909192939495969798991001011021031041051061 unmodified line1081091101119111211311411511611711811912012112212398991241251261271021281291301311323 unmodified lines1361371381121131391401151411421431446 unmodified lines1511521531541551561571581591281601 unmodified lineimport timeimport sqlite3import asynciofrom pyromod import listen # Required for client.askfrom pyrogram import Client, filtersfrom pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButtonfrom flask import Flaskfrom threading import Thread
# --- Flask Server for Hugging Face "Keep Alive" ---web_app = Flask(__name__)
@web_app.route('/')def health_check(): return "Bot is Running", 200
def run_server(): # Hugging Face looks for activity on port 7860 web_app.run(host="0.0.0.0", port=7860)
# --- Environment Variables ---API_ID = int(os.getenv("API_ID", 0))34 unmodified lines api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN, workers=20 # Increase workers for parallel chunk handling workers=20 )
@app.on_message(filters.document | filters.video)@app.on_message(filters.document | filters.video | filters.audio)async def fast_process(client, message): user_id = message.from_user.id # Fast prompt for new name # 1. Conversation to get 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()
# 2. Download (Handled in chunks by Pyrogram) # Download path = await client.download_media( message, file_name=new_name, progress=progress_callback, progress_args=(status, start_time, "Downloading") )
# Upload try: path = await client.download_media( message, file_name=new_name, progress=progress_callback, progress_args=(status, start_time, "Downloading") ) except Exception as e: return await status.edit(f"β **Download Error:** {e}")
# 3. Upload await status.edit("π **Upload Starting...**") start_up = time.time() try: 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 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") ) except Exception as e: return await status.edit(f"β **Upload Error:** {e}")
# 4. History & Cleanup file_obj = sent_file.document or sent_file.video or sent_file.audio add_to_history(user_id, new_name, file_obj.file_id)
if os.path.exists(path): os.remove(path) await status.delete()1 unmodified lineasync def progress_callback(current, total, status_msg, start_time, mode): now = time.time() diff = now - start_time # Update every 4 seconds to avoid flood limits if round(diff % 3.00) == 0 or current == total: if round(diff % 4.00) == 0 or current == total: speed = current / diff if diff > 0 else 0 eta = (total - current) / speed if speed > 0 else 0 # Convert to MB curr_mb = current / (1024 * 1024) tot_mb = total / (1024 * 1024) speed_mb = speed / (1024 * 1024)
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"**Size:** {curr_mb:.2f}/{tot_mb:.2f} MB\n" f"**Speed:** {speed_mb:.2f} MB/s\n" f"**ETA:** {int(eta)}s" ) except: pass except: pass
@app.on_message(filters.command("history"))async def show_history(client, message):3 unmodified lines
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 for idx, (name, fid) in enumerate(rows[-10:]): # Show last 10 text += f"{idx+1}. `{name}`\n" buttons.append([InlineKeyboardButton(f"Get {name}", callback_data=f"get_{idx}")]) buttons.append([InlineKeyboardButton(f"Get {name[:20]}...", callback_data=f"get_{idx}")])
await message.reply(text, reply_markup=InlineKeyboardMarkup(buttons))
6 unmodified lines await client.send_document(query.message.chat.id, file_id, caption=f"Retrieved: {file_name}") await query.answer()
if __name__ == "__main__": # Start the web server thread Thread(target=run_server, daemon=True).start() # Run the bot print("Bot is booting...")app.run() app.run()bot.py+65-33