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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# filename: core/bot.py
|
| 2 |
+
|
| 3 |
+
import asyncio
|
| 4 |
+
import logging
|
| 5 |
+
import os
|
| 6 |
+
import time
|
| 7 |
+
from collections import deque
|
| 8 |
+
from telethon import TelegramClient
|
| 9 |
+
|
| 10 |
+
import config
|
| 11 |
+
import templates
|
| 12 |
+
from database import manager as db_manager
|
| 13 |
+
from utils import terabox, ffmpeg, helpers
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
# --- Core Bot Client ---
|
| 18 |
+
bot = TelegramClient('terabox_bot_session', config.API_ID, config.API_HASH)
|
| 19 |
+
|
| 20 |
+
# --- In-Memory State Management ---
|
| 21 |
+
# Queues for the workers
|
| 22 |
+
PREMIUM_QUEUE = asyncio.Queue()
|
| 23 |
+
FREE_QUEUE = asyncio.Queue()
|
| 24 |
+
|
| 25 |
+
# Dictionary to track the state of all active batch jobs
|
| 26 |
+
# Structure: { "batch_id": { ...job_details... } }
|
| 27 |
+
BATCH_JOBS = {}
|
| 28 |
+
|
| 29 |
+
# Dictionary to hold pending tasks for each free user before they hit the queue
|
| 30 |
+
# This is essential for the round-robin scheduler
|
| 31 |
+
# Structure: { user_id: [task1, task2, ...] }
|
| 32 |
+
ACTIVE_USER_TASKS = {}
|
| 33 |
+
|
| 34 |
+
# Deque to manage the turn order for the fair-share scheduler
|
| 35 |
+
USER_TURN_ORDER = deque()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# --- The Fair-Share Scheduler for Free Users ---
|
| 39 |
+
async def scheduler_loop():
|
| 40 |
+
"""
|
| 41 |
+
The heart of the fair-share system.
|
| 42 |
+
This loop checks whose turn it is and feeds one task at a time into the FREE_QUEUE.
|
| 43 |
+
"""
|
| 44 |
+
logger.info("Fair-Share Scheduler started.")
|
| 45 |
+
while True:
|
| 46 |
+
await asyncio.sleep(0.2) # Prevent busy-looping
|
| 47 |
+
|
| 48 |
+
if not USER_TURN_ORDER or FREE_QUEUE.full():
|
| 49 |
+
continue
|
| 50 |
+
|
| 51 |
+
# Get the next user in line
|
| 52 |
+
user_id = USER_TURN_ORDER[0]
|
| 53 |
+
|
| 54 |
+
if user_id in ACTIVE_USER_TASKS and ACTIVE_USER_TASKS[user_id]:
|
| 55 |
+
# Take the next task for this user
|
| 56 |
+
task = ACTIVE_USER_TASKS[user_id].pop(0)
|
| 57 |
+
await FREE_QUEUE.put(task)
|
| 58 |
+
|
| 59 |
+
# If the user has no more tasks, remove them from the turn order.
|
| 60 |
+
# Otherwise, move them to the back of the line.
|
| 61 |
+
USER_TURN_ORDER.rotate(-1) # Move current user to the end
|
| 62 |
+
if not ACTIVE_USER_TASKS[user_id]:
|
| 63 |
+
USER_TURN_ORDER.pop() # Remove them if their list is now empty
|
| 64 |
+
del ACTIVE_USER_TASKS[user_id]
|
| 65 |
+
else:
|
| 66 |
+
# Cleanup: If user is in the turn order but has no tasks, remove them.
|
| 67 |
+
USER_TURN_ORDER.popleft()
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# --- The Worker Logic ---
|
| 71 |
+
async def _process_task(task: dict, worker_name: str):
|
| 72 |
+
"""The main logic for processing a single link. Executed by all workers."""
|
| 73 |
+
batch_id = task['batch_id']
|
| 74 |
+
original_link = task['link']
|
| 75 |
+
user_id = task['user_id']
|
| 76 |
+
metadata = task['metadata'] # Metadata is pre-fetched by the handler
|
| 77 |
+
|
| 78 |
+
batch_info = BATCH_JOBS.get(batch_id)
|
| 79 |
+
if not batch_info:
|
| 80 |
+
logger.warning(f"[{worker_name}] Batch {batch_id} not found. Task for {original_link} skipped.")
|
| 81 |
+
return
|
| 82 |
+
|
| 83 |
+
logger.info(f"[{worker_name}] Starting processing for link: {original_link}")
|
| 84 |
+
|
| 85 |
+
download_path = None
|
| 86 |
+
final_file_path = None
|
| 87 |
+
error_reason = None
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
# Step 1: Download the file from the direct link
|
| 91 |
+
download_path = await terabox.download_file_from_url(
|
| 92 |
+
url=metadata['url'],
|
| 93 |
+
dir_path="downloads",
|
| 94 |
+
filename=metadata['file_name']
|
| 95 |
+
)
|
| 96 |
+
if not download_path:
|
| 97 |
+
raise ValueError("File download failed.")
|
| 98 |
+
|
| 99 |
+
final_file_path = download_path
|
| 100 |
+
|
| 101 |
+
# Step 2: FFMPEG processing (if enabled and it's a video)
|
| 102 |
+
thumbnail_path = None
|
| 103 |
+
if config.ENABLE_FFMPEG and final_file_path.endswith(('.mp4', '.mkv', '.webm')):
|
| 104 |
+
# Remux to MP4 if needed (fast operation)
|
| 105 |
+
if not final_file_path.endswith('.mp4'):
|
| 106 |
+
remuxed_path = f"{os.path.splitext(final_file_path)[0]}.mp4"
|
| 107 |
+
remuxed_path = await ffmpeg.remux_to_mp4(final_file_path, remuxed_path)
|
| 108 |
+
if remuxed_path:
|
| 109 |
+
os.remove(final_file_path) # remove original file
|
| 110 |
+
final_file_path = remuxed_path
|
| 111 |
+
|
| 112 |
+
# Generate thumbnail (fast operation)
|
| 113 |
+
thumb_path = f"{os.path.splitext(final_file_path)[0]}.jpg"
|
| 114 |
+
thumbnail_path = await ffmpeg.generate_thumbnail(final_file_path, thumb_path)
|
| 115 |
+
|
| 116 |
+
# Step 3: Upload to backup channel & cache
|
| 117 |
+
caption = templates.BotResponses.FILE_CAPTION.format(
|
| 118 |
+
file_name=metadata['file_name'],
|
| 119 |
+
file_size=helpers.format_bytes(metadata['file_size']),
|
| 120 |
+
source_url=original_link
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
backup_message = await bot.send_file(
|
| 124 |
+
config.BACKUP_CHANNEL_ID,
|
| 125 |
+
file=final_file_path,
|
| 126 |
+
thumb=thumbnail_path,
|
| 127 |
+
caption=caption
|
| 128 |
+
)
|
| 129 |
+
await db_manager.add_to_cache(
|
| 130 |
+
short_id=task['short_id'],
|
| 131 |
+
file_id=backup_message.id,
|
| 132 |
+
file_name=metadata['file_name'],
|
| 133 |
+
file_size=metadata['file_size']
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# Step 4: Deliver to user and schedule deletion
|
| 137 |
+
dm_message = await bot.send_file(
|
| 138 |
+
user_id,
|
| 139 |
+
file=backup_message, # Send using file_id from backup channel for speed
|
| 140 |
+
caption=caption
|
| 141 |
+
)
|
| 142 |
+
# Here you would add logic to call the APScheduler to delete dm_message in 30 mins
|
| 143 |
+
|
| 144 |
+
except Exception as e:
|
| 145 |
+
logger.error(f"[{worker_name}] Error processing {original_link}: {e}", exc_info=True)
|
| 146 |
+
error_reason = str(e)
|
| 147 |
+
|
| 148 |
+
finally:
|
| 149 |
+
# Clean up local files
|
| 150 |
+
if download_path and os.path.exists(download_path):
|
| 151 |
+
os.remove(download_path)
|
| 152 |
+
if final_file_path and final_file_path != download_path and os.path.exists(final_file_path):
|
| 153 |
+
os.remove(final_file_path)
|
| 154 |
+
if 'thumbnail_path' in locals() and thumbnail_path and os.path.exists(thumbnail_path):
|
| 155 |
+
os.remove(thumbnail_path)
|
| 156 |
+
|
| 157 |
+
# --- Final Step: Update Batch Status Safely ---
|
| 158 |
+
async with batch_info['lock']:
|
| 159 |
+
batch_info['processed_links'] += 1
|
| 160 |
+
if error_reason:
|
| 161 |
+
batch_info['failed_links'].append({"link": original_link, "error": error_reason})
|
| 162 |
+
|
| 163 |
+
# Update progress message
|
| 164 |
+
processed = batch_info['processed_links']
|
| 165 |
+
total = batch_info['total_links']
|
| 166 |
+
progress = processed / total
|
| 167 |
+
|
| 168 |
+
try:
|
| 169 |
+
await bot.edit_message(
|
| 170 |
+
batch_info['chat_id'],
|
| 171 |
+
batch_info['status_message_id'],
|
| 172 |
+
text=templates.BotResponses.BATCH_UPDATE_VALIDATED.format(
|
| 173 |
+
batch_id=batch_id[:6],
|
| 174 |
+
valid_count=total,
|
| 175 |
+
total_count=batch_info['original_total'],
|
| 176 |
+
skipped_count=batch_info['original_total'] - total,
|
| 177 |
+
progress_bar=helpers.create_progress_bar(progress),
|
| 178 |
+
processed_count=processed
|
| 179 |
+
)
|
| 180 |
+
)
|
| 181 |
+
except Exception:
|
| 182 |
+
pass # Ignore if message can't be edited
|
| 183 |
+
|
| 184 |
+
# If batch is fully processed, send final summary
|
| 185 |
+
if processed == total:
|
| 186 |
+
# Here you would call a final summary function
|
| 187 |
+
logger.info(f"[{worker_name}] Batch {batch_id[:6]} complete.")
|
| 188 |
+
# ... logic to send final summary and delete job from BATCH_JOBS ...
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
async def worker(name: str, queue: asyncio.Queue):
|
| 192 |
+
"""The generic worker function."""
|
| 193 |
+
while True:
|
| 194 |
+
task = await queue.get()
|
| 195 |
+
try:
|
| 196 |
+
await _process_task(task, name)
|
| 197 |
+
finally:
|
| 198 |
+
queue.task_done()
|
| 199 |
+
|
| 200 |
+
# --- Startup Function ---
|
| 201 |
+
def start_bot_runtime():
|
| 202 |
+
"""Creates all the background tasks for the bot's engine."""
|
| 203 |
+
# Create Premium Workers
|
| 204 |
+
for i in range(config.PREMIUM_WORKERS):
|
| 205 |
+
asyncio.create_task(worker(f"PremiumWorker-{i+1}", PREMIUM_QUEUE))
|
| 206 |
+
|
| 207 |
+
# Create Free Workers
|
| 208 |
+
for i in range(config.FREE_WORKERS):
|
| 209 |
+
asyncio.create_task(worker(f"FreeWorker-{i+1}", FREE_QUEUE))
|
| 210 |
+
|
| 211 |
+
# Start the Fair-Share Scheduler
|
| 212 |
+
asyncio.create_task(scheduler_loop())
|
| 213 |
+
|
| 214 |
+
logger.info(f"Bot runtime started with {config.PREMIUM_WORKERS} premium and {config.FREE_WORKERS} free workers.")
|
| 215 |
+
|