Spaces:
Runtime error
Runtime error
Update bot.py
Browse files
bot.py
CHANGED
|
@@ -2,8 +2,22 @@ import os
|
|
| 2 |
import time
|
| 3 |
import sqlite3
|
| 4 |
import asyncio
|
|
|
|
| 5 |
from pyrogram import Client, filters
|
| 6 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# --- Environment Variables ---
|
| 9 |
API_ID = int(os.getenv("API_ID", 0))
|
|
@@ -44,43 +58,49 @@ app = Client(
|
|
| 44 |
api_id=API_ID,
|
| 45 |
api_hash=API_HASH,
|
| 46 |
bot_token=BOT_TOKEN,
|
| 47 |
-
workers=20
|
| 48 |
)
|
| 49 |
|
| 50 |
-
@app.on_message(filters.document | filters.video)
|
| 51 |
async def fast_process(client, message):
|
| 52 |
user_id = message.from_user.id
|
| 53 |
|
| 54 |
-
#
|
| 55 |
ask = await client.ask(message.chat.id, "π Send the **New Name** with extension:")
|
| 56 |
new_name = ask.text
|
| 57 |
|
| 58 |
status = await message.reply("β‘ **Starting High-Speed Stream...**")
|
| 59 |
start_time = time.time()
|
| 60 |
|
| 61 |
-
# Download
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
start_up = time.time()
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
if os.path.exists(path):
|
| 85 |
os.remove(path)
|
| 86 |
await status.delete()
|
|
@@ -88,18 +108,25 @@ async def fast_process(client, message):
|
|
| 88 |
async def progress_callback(current, total, status_msg, start_time, mode):
|
| 89 |
now = time.time()
|
| 90 |
diff = now - start_time
|
| 91 |
-
|
|
|
|
| 92 |
speed = current / diff if diff > 0 else 0
|
| 93 |
eta = (total - current) / speed if speed > 0 else 0
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
await status_msg.edit(
|
| 97 |
f"π **{mode}**\n"
|
| 98 |
-
f"**Size:** {
|
| 99 |
-
f"**Speed:** {
|
| 100 |
f"**ETA:** {int(eta)}s"
|
| 101 |
)
|
| 102 |
-
except:
|
|
|
|
| 103 |
|
| 104 |
@app.on_message(filters.command("history"))
|
| 105 |
async def show_history(client, message):
|
|
@@ -109,10 +136,9 @@ async def show_history(client, message):
|
|
| 109 |
|
| 110 |
text = "π **Your File History:**\n\n"
|
| 111 |
buttons = []
|
| 112 |
-
for idx, (name, fid) in enumerate(rows):
|
| 113 |
-
# We use the index in callback_data to retrieve the fid
|
| 114 |
text += f"{idx+1}. `{name}`\n"
|
| 115 |
-
buttons.append([InlineKeyboardButton(f"Get {name}", callback_data=f"get_{idx}")])
|
| 116 |
|
| 117 |
await message.reply(text, reply_markup=InlineKeyboardMarkup(buttons))
|
| 118 |
|
|
@@ -125,4 +151,10 @@ async def send_from_history(client, query):
|
|
| 125 |
await client.send_document(query.message.chat.id, file_id, caption=f"Retrieved: {file_name}")
|
| 126 |
await query.answer()
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
import sqlite3
|
| 4 |
import asyncio
|
| 5 |
+
from pyromod import listen # Required for client.ask
|
| 6 |
from pyrogram import Client, filters
|
| 7 |
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
| 8 |
+
from flask import Flask
|
| 9 |
+
from threading import Thread
|
| 10 |
+
|
| 11 |
+
# --- Flask Server for Hugging Face "Keep Alive" ---
|
| 12 |
+
web_app = Flask(__name__)
|
| 13 |
+
|
| 14 |
+
@web_app.route('/')
|
| 15 |
+
def health_check():
|
| 16 |
+
return "Bot is Running", 200
|
| 17 |
+
|
| 18 |
+
def run_server():
|
| 19 |
+
# Hugging Face looks for activity on port 7860
|
| 20 |
+
web_app.run(host="0.0.0.0", port=7860)
|
| 21 |
|
| 22 |
# --- Environment Variables ---
|
| 23 |
API_ID = int(os.getenv("API_ID", 0))
|
|
|
|
| 58 |
api_id=API_ID,
|
| 59 |
api_hash=API_HASH,
|
| 60 |
bot_token=BOT_TOKEN,
|
| 61 |
+
workers=20
|
| 62 |
)
|
| 63 |
|
| 64 |
+
@app.on_message(filters.document | filters.video | filters.audio)
|
| 65 |
async def fast_process(client, message):
|
| 66 |
user_id = message.from_user.id
|
| 67 |
|
| 68 |
+
# 1. Conversation to get new name
|
| 69 |
ask = await client.ask(message.chat.id, "π Send the **New Name** with extension:")
|
| 70 |
new_name = ask.text
|
| 71 |
|
| 72 |
status = await message.reply("β‘ **Starting High-Speed Stream...**")
|
| 73 |
start_time = time.time()
|
| 74 |
|
| 75 |
+
# 2. Download (Handled in chunks by Pyrogram)
|
| 76 |
+
try:
|
| 77 |
+
path = await client.download_media(
|
| 78 |
+
message,
|
| 79 |
+
file_name=new_name,
|
| 80 |
+
progress=progress_callback,
|
| 81 |
+
progress_args=(status, start_time, "Downloading")
|
| 82 |
+
)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return await status.edit(f"β **Download Error:** {e}")
|
| 85 |
+
|
| 86 |
+
# 3. Upload
|
| 87 |
+
await status.edit("π **Upload Starting...**")
|
| 88 |
start_up = time.time()
|
| 89 |
+
try:
|
| 90 |
+
sent_file = await client.send_document(
|
| 91 |
+
chat_id=message.chat.id,
|
| 92 |
+
document=path,
|
| 93 |
+
caption=f"β
**Done:** `{new_name}`",
|
| 94 |
+
progress=progress_callback,
|
| 95 |
+
progress_args=(status, start_up, "Uploading")
|
| 96 |
+
)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
return await status.edit(f"β **Upload Error:** {e}")
|
| 99 |
+
|
| 100 |
+
# 4. History & Cleanup
|
| 101 |
+
file_obj = sent_file.document or sent_file.video or sent_file.audio
|
| 102 |
+
add_to_history(user_id, new_name, file_obj.file_id)
|
| 103 |
+
|
| 104 |
if os.path.exists(path):
|
| 105 |
os.remove(path)
|
| 106 |
await status.delete()
|
|
|
|
| 108 |
async def progress_callback(current, total, status_msg, start_time, mode):
|
| 109 |
now = time.time()
|
| 110 |
diff = now - start_time
|
| 111 |
+
# Update every 4 seconds to avoid flood limits
|
| 112 |
+
if round(diff % 4.00) == 0 or current == total:
|
| 113 |
speed = current / diff if diff > 0 else 0
|
| 114 |
eta = (total - current) / speed if speed > 0 else 0
|
| 115 |
|
| 116 |
+
# Convert to MB
|
| 117 |
+
curr_mb = current / (1024 * 1024)
|
| 118 |
+
tot_mb = total / (1024 * 1024)
|
| 119 |
+
speed_mb = speed / (1024 * 1024)
|
| 120 |
+
|
| 121 |
try:
|
| 122 |
await status_msg.edit(
|
| 123 |
f"π **{mode}**\n"
|
| 124 |
+
f"**Size:** {curr_mb:.2f}/{tot_mb:.2f} MB\n"
|
| 125 |
+
f"**Speed:** {speed_mb:.2f} MB/s\n"
|
| 126 |
f"**ETA:** {int(eta)}s"
|
| 127 |
)
|
| 128 |
+
except:
|
| 129 |
+
pass
|
| 130 |
|
| 131 |
@app.on_message(filters.command("history"))
|
| 132 |
async def show_history(client, message):
|
|
|
|
| 136 |
|
| 137 |
text = "π **Your File History:**\n\n"
|
| 138 |
buttons = []
|
| 139 |
+
for idx, (name, fid) in enumerate(rows[-10:]): # Show last 10
|
|
|
|
| 140 |
text += f"{idx+1}. `{name}`\n"
|
| 141 |
+
buttons.append([InlineKeyboardButton(f"Get {name[:20]}...", callback_data=f"get_{idx}")])
|
| 142 |
|
| 143 |
await message.reply(text, reply_markup=InlineKeyboardMarkup(buttons))
|
| 144 |
|
|
|
|
| 151 |
await client.send_document(query.message.chat.id, file_id, caption=f"Retrieved: {file_name}")
|
| 152 |
await query.answer()
|
| 153 |
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
+
# Start the web server thread
|
| 156 |
+
Thread(target=run_server, daemon=True).start()
|
| 157 |
+
|
| 158 |
+
# Run the bot
|
| 159 |
+
print("Bot is booting...")
|
| 160 |
+
app.run()
|