Spaces:
Runtime error
Runtime error
Create bot.py
Browse files
bot.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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))
|
| 10 |
+
API_HASH = os.getenv("API_HASH")
|
| 11 |
+
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
| 12 |
+
|
| 13 |
+
# --- Database Setup ---
|
| 14 |
+
DB_PATH = "bot_data.db"
|
| 15 |
+
|
| 16 |
+
def init_db():
|
| 17 |
+
conn = sqlite3.connect(DB_PATH)
|
| 18 |
+
cursor = conn.cursor()
|
| 19 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS history
|
| 20 |
+
(user_id INTEGER, file_name TEXT, file_id TEXT)''')
|
| 21 |
+
conn.commit()
|
| 22 |
+
conn.close()
|
| 23 |
+
|
| 24 |
+
def add_to_history(user_id, name, fid):
|
| 25 |
+
conn = sqlite3.connect(DB_PATH)
|
| 26 |
+
cursor = conn.cursor()
|
| 27 |
+
cursor.execute("INSERT INTO history VALUES (?, ?, ?)", (user_id, name, fid))
|
| 28 |
+
conn.commit()
|
| 29 |
+
conn.close()
|
| 30 |
+
|
| 31 |
+
def get_history(user_id):
|
| 32 |
+
conn = sqlite3.connect(DB_PATH)
|
| 33 |
+
cursor = conn.cursor()
|
| 34 |
+
cursor.execute("SELECT file_name, file_id FROM history WHERE user_id = ?", (user_id,))
|
| 35 |
+
rows = cursor.fetchall()
|
| 36 |
+
conn.close()
|
| 37 |
+
return rows
|
| 38 |
+
|
| 39 |
+
init_db()
|
| 40 |
+
|
| 41 |
+
# --- Bot Client ---
|
| 42 |
+
app = Client(
|
| 43 |
+
"FastRename",
|
| 44 |
+
api_id=API_ID,
|
| 45 |
+
api_hash=API_HASH,
|
| 46 |
+
bot_token=BOT_TOKEN,
|
| 47 |
+
workers=20 # Increase workers for parallel chunk handling
|
| 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 |
+
# Fast prompt for new name
|
| 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 |
+
path = await client.download_media(
|
| 63 |
+
message,
|
| 64 |
+
file_name=new_name,
|
| 65 |
+
progress=progress_callback,
|
| 66 |
+
progress_args=(status, start_time, "Downloading")
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Upload
|
| 70 |
+
start_up = time.time()
|
| 71 |
+
sent_file = await client.send_document(
|
| 72 |
+
chat_id=message.chat.id,
|
| 73 |
+
document=path,
|
| 74 |
+
caption=f"β
**Done:** `{new_name}`",
|
| 75 |
+
progress=progress_callback,
|
| 76 |
+
progress_args=(status, start_up, "Uploading")
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Store in SQLite
|
| 80 |
+
new_fid = sent_file.document.file_id if sent_file.document else sent_file.video.file_id
|
| 81 |
+
add_to_history(user_id, new_name, new_fid)
|
| 82 |
+
|
| 83 |
+
# Instant Cleanup
|
| 84 |
+
if os.path.exists(path):
|
| 85 |
+
os.remove(path)
|
| 86 |
+
await status.delete()
|
| 87 |
+
|
| 88 |
+
async def progress_callback(current, total, status_msg, start_time, mode):
|
| 89 |
+
now = time.time()
|
| 90 |
+
diff = now - start_time
|
| 91 |
+
if round(diff % 3.00) == 0 or current == total:
|
| 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:** {current/1024/1024:.2f}/{total/1024/1024:.2f} MB\n"
|
| 99 |
+
f"**Speed:** {speed/1024/1024:.2f} MB/s\n"
|
| 100 |
+
f"**ETA:** {int(eta)}s"
|
| 101 |
+
)
|
| 102 |
+
except: pass
|
| 103 |
+
|
| 104 |
+
@app.on_message(filters.command("history"))
|
| 105 |
+
async def show_history(client, message):
|
| 106 |
+
rows = get_history(message.from_user.id)
|
| 107 |
+
if not rows:
|
| 108 |
+
return await message.reply("No history found.")
|
| 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 |
+
|
| 119 |
+
@app.on_callback_query(filters.regex(r"^get_"))
|
| 120 |
+
async def send_from_history(client, query):
|
| 121 |
+
idx = int(query.data.split("_")[1])
|
| 122 |
+
rows = get_history(query.from_user.id)
|
| 123 |
+
file_name, file_id = rows[idx]
|
| 124 |
+
|
| 125 |
+
await client.send_document(query.message.chat.id, file_id, caption=f"Retrieved: {file_name}")
|
| 126 |
+
await query.answer()
|
| 127 |
+
|
| 128 |
+
app.run()
|