Spaces:
Runtime error
Runtime error
Update core/bot.py
Browse files- core/bot.py +107 -36
core/bot.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# filename: core/bot.py
|
| 2 |
|
| 3 |
import asyncio
|
| 4 |
import logging
|
|
@@ -7,7 +7,6 @@ import time
|
|
| 7 |
from collections import deque
|
| 8 |
from telethon import TelegramClient
|
| 9 |
from telethon.network import connection
|
| 10 |
-
import dns.asyncresolver # <-- IMPORT FOR YOUR DNS SUGGESTION
|
| 11 |
|
| 12 |
import config
|
| 13 |
import templates
|
|
@@ -16,27 +15,15 @@ from utils import terabox, ffmpeg, helpers
|
|
| 16 |
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
-
# ---
|
| 20 |
-
#
|
| 21 |
-
try:
|
| 22 |
-
async_resolver = dns.asyncresolver.Resolver(configure=False)
|
| 23 |
-
async_resolver.nameservers = ["8.8.8.8", "8.8.4.4", "1.1.1.1"]
|
| 24 |
-
dns.asyncresolver.default_resolver = async_resolver
|
| 25 |
-
logger.info("Successfully configured custom DNS resolvers.")
|
| 26 |
-
except Exception as e:
|
| 27 |
-
logger.warning(f"Could not configure custom DNS resolver: {e}")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# --- Core Bot Client (UPDATED WITH BOTH FIXES) ---
|
| 31 |
bot = TelegramClient(
|
| 32 |
'data/terabox_bot_session',
|
| 33 |
config.API_ID,
|
| 34 |
config.API_HASH,
|
| 35 |
-
connection=connection.ConnectionHttp
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# --- (The rest of the file is exactly the same as before) ---
|
| 39 |
-
|
| 40 |
# --- In-Memory State Management ---
|
| 41 |
PREMIUM_QUEUE = asyncio.Queue()
|
| 42 |
FREE_QUEUE = asyncio.Queue()
|
|
@@ -44,18 +31,26 @@ BATCH_JOBS = {}
|
|
| 44 |
ACTIVE_USER_TASKS = {}
|
| 45 |
USER_TURN_ORDER = deque()
|
| 46 |
|
|
|
|
| 47 |
# --- The Fair-Share Scheduler for Free Users ---
|
| 48 |
async def scheduler_loop():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
logger.info("Fair-Share Scheduler started.")
|
| 50 |
-
# ... (rest of the function is the same) ...
|
| 51 |
while True:
|
| 52 |
-
await asyncio.sleep(0.2)
|
|
|
|
| 53 |
if not USER_TURN_ORDER or FREE_QUEUE.full():
|
| 54 |
continue
|
|
|
|
| 55 |
user_id = USER_TURN_ORDER[0]
|
|
|
|
| 56 |
if user_id in ACTIVE_USER_TASKS and ACTIVE_USER_TASKS[user_id]:
|
| 57 |
task = ACTIVE_USER_TASKS[user_id].pop(0)
|
| 58 |
await FREE_QUEUE.put(task)
|
|
|
|
| 59 |
if not ACTIVE_USER_TASKS[user_id]:
|
| 60 |
USER_TURN_ORDER.popleft()
|
| 61 |
del ACTIVE_USER_TASKS[user_id]
|
|
@@ -64,12 +59,8 @@ async def scheduler_loop():
|
|
| 64 |
else:
|
| 65 |
USER_TURN_ORDER.popleft()
|
| 66 |
|
| 67 |
-
# --- The Worker Logic ---
|
| 68 |
-
async def _process_task(task: dict, worker_name: str):
|
| 69 |
-
# ... (rest of the function is the same) ...
|
| 70 |
-
pass # Placeholder for the large function body
|
| 71 |
|
| 72 |
-
# ---
|
| 73 |
async def _process_task(task: dict, worker_name: str):
|
| 74 |
"""The main logic for processing a single link. Executed by all workers."""
|
| 75 |
batch_id = task['batch_id']
|
|
@@ -92,27 +83,110 @@ async def _process_task(task: dict, worker_name: str):
|
|
| 92 |
|
| 93 |
try:
|
| 94 |
if is_cached_task:
|
|
|
|
| 95 |
cached_file_info = await db_manager.get_cached_file(task['short_id'])
|
| 96 |
if cached_file_info and config.BACKUP_CHANNEL_ID:
|
| 97 |
message_to_forward = await bot.get_messages(config.BACKUP_CHANNEL_ID, ids=int(cached_file_info['file_id']))
|
| 98 |
if message_to_forward:
|
| 99 |
await bot.send_file(user_id, file=message_to_forward)
|
| 100 |
else:
|
| 101 |
-
#
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
except Exception as e:
|
| 105 |
logger.error(f"[{worker_name}] Error processing {original_link}: {e}", exc_info=True)
|
| 106 |
error_reason = str(e)
|
| 107 |
|
| 108 |
finally:
|
| 109 |
-
#
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
# Update Batch Status
|
| 113 |
async with batch_info['lock']:
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
async def worker(name: str, queue: asyncio.Queue):
|
|
@@ -120,12 +194,11 @@ async def worker(name: str, queue: asyncio.Queue):
|
|
| 120 |
while True:
|
| 121 |
task = await queue.get()
|
| 122 |
try:
|
| 123 |
-
|
| 124 |
-
# The full _process_task logic from the previous file goes here.
|
| 125 |
-
await asyncio.sleep(1)
|
| 126 |
finally:
|
| 127 |
queue.task_done()
|
| 128 |
|
|
|
|
| 129 |
def start_bot_runtime():
|
| 130 |
"""Creates all the background tasks for the bot's engine."""
|
| 131 |
for i in range(config.PREMIUM_WORKERS):
|
|
@@ -135,6 +208,4 @@ def start_bot_runtime():
|
|
| 135 |
asyncio.create_task(worker(f"FreeWorker-{i+1}", FREE_QUEUE))
|
| 136 |
|
| 137 |
asyncio.create_task(scheduler_loop())
|
| 138 |
-
|
| 139 |
logger.info(f"Bot runtime started with {config.PREMIUM_WORKERS} premium and {config.FREE_WORKERS} free workers.")
|
| 140 |
-
|
|
|
|
| 1 |
+
# filename: core/bot.py
|
| 2 |
|
| 3 |
import asyncio
|
| 4 |
import logging
|
|
|
|
| 7 |
from collections import deque
|
| 8 |
from telethon import TelegramClient
|
| 9 |
from telethon.network import connection
|
|
|
|
| 10 |
|
| 11 |
import config
|
| 12 |
import templates
|
|
|
|
| 15 |
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
+
# --- Core Bot Client ---
|
| 19 |
+
# Includes fix for session path and connection mode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
bot = TelegramClient(
|
| 21 |
'data/terabox_bot_session',
|
| 22 |
config.API_ID,
|
| 23 |
config.API_HASH,
|
| 24 |
+
connection=connection.ConnectionHttp
|
| 25 |
)
|
| 26 |
|
|
|
|
|
|
|
| 27 |
# --- In-Memory State Management ---
|
| 28 |
PREMIUM_QUEUE = asyncio.Queue()
|
| 29 |
FREE_QUEUE = asyncio.Queue()
|
|
|
|
| 31 |
ACTIVE_USER_TASKS = {}
|
| 32 |
USER_TURN_ORDER = deque()
|
| 33 |
|
| 34 |
+
|
| 35 |
# --- The Fair-Share Scheduler for Free Users ---
|
| 36 |
async def scheduler_loop():
|
| 37 |
+
"""
|
| 38 |
+
The heart of the fair-share system.
|
| 39 |
+
This loop checks whose turn it is and feeds one task at a time into the FREE_QUEUE.
|
| 40 |
+
"""
|
| 41 |
logger.info("Fair-Share Scheduler started.")
|
|
|
|
| 42 |
while True:
|
| 43 |
+
await asyncio.sleep(0.2) # Prevent busy-looping
|
| 44 |
+
|
| 45 |
if not USER_TURN_ORDER or FREE_QUEUE.full():
|
| 46 |
continue
|
| 47 |
+
|
| 48 |
user_id = USER_TURN_ORDER[0]
|
| 49 |
+
|
| 50 |
if user_id in ACTIVE_USER_TASKS and ACTIVE_USER_TASKS[user_id]:
|
| 51 |
task = ACTIVE_USER_TASKS[user_id].pop(0)
|
| 52 |
await FREE_QUEUE.put(task)
|
| 53 |
+
|
| 54 |
if not ACTIVE_USER_TASKS[user_id]:
|
| 55 |
USER_TURN_ORDER.popleft()
|
| 56 |
del ACTIVE_USER_TASKS[user_id]
|
|
|
|
| 59 |
else:
|
| 60 |
USER_TURN_ORDER.popleft()
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# --- The Worker Logic ---
|
| 64 |
async def _process_task(task: dict, worker_name: str):
|
| 65 |
"""The main logic for processing a single link. Executed by all workers."""
|
| 66 |
batch_id = task['batch_id']
|
|
|
|
| 83 |
|
| 84 |
try:
|
| 85 |
if is_cached_task:
|
| 86 |
+
# For cached tasks, we forward the file from the backup channel
|
| 87 |
cached_file_info = await db_manager.get_cached_file(task['short_id'])
|
| 88 |
if cached_file_info and config.BACKUP_CHANNEL_ID:
|
| 89 |
message_to_forward = await bot.get_messages(config.BACKUP_CHANNEL_ID, ids=int(cached_file_info['file_id']))
|
| 90 |
if message_to_forward:
|
| 91 |
await bot.send_file(user_id, file=message_to_forward)
|
| 92 |
else:
|
| 93 |
+
# For new tasks, follow the full download -> process -> upload workflow
|
| 94 |
+
download_path = await terabox.download_file_from_url(
|
| 95 |
+
url=metadata['url'],
|
| 96 |
+
dir_path="downloads",
|
| 97 |
+
filename=metadata['file_name']
|
| 98 |
+
)
|
| 99 |
+
if not download_path:
|
| 100 |
+
raise ValueError("File download failed.")
|
| 101 |
+
|
| 102 |
+
final_file_path = download_path
|
| 103 |
+
|
| 104 |
+
if config.ENABLE_FFMPEG and final_file_path.lower().endswith(('.mp4', '.mkv', '.webm')):
|
| 105 |
+
if not final_file_path.lower().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 |
+
if os.path.exists(final_file_path): os.remove(final_file_path)
|
| 110 |
+
final_file_path = remuxed_path
|
| 111 |
+
|
| 112 |
+
thumb_path_temp = f"{os.path.splitext(final_file_path)[0]}.jpg"
|
| 113 |
+
thumbnail_path = await ffmpeg.generate_thumbnail(final_file_path, thumb_path_temp)
|
| 114 |
+
|
| 115 |
+
caption = templates.BotResponses.FILE_CAPTION.format(
|
| 116 |
+
file_name=metadata['file_name'],
|
| 117 |
+
file_size=helpers.format_bytes(metadata['file_size']),
|
| 118 |
+
source_url=original_link
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
backup_message = await bot.send_file(
|
| 122 |
+
config.BACKUP_CHANNEL_ID,
|
| 123 |
+
file=final_file_path,
|
| 124 |
+
thumb=thumbnail_path,
|
| 125 |
+
caption=caption
|
| 126 |
+
)
|
| 127 |
+
await db_manager.add_to_cache(
|
| 128 |
+
short_id=task['short_id'],
|
| 129 |
+
file_id=str(backup_message.id),
|
| 130 |
+
file_name=metadata['file_name'],
|
| 131 |
+
file_size=metadata['file_size']
|
| 132 |
+
)
|
| 133 |
+
await bot.send_file(user_id, file=backup_message)
|
| 134 |
|
| 135 |
except Exception as e:
|
| 136 |
logger.error(f"[{worker_name}] Error processing {original_link}: {e}", exc_info=True)
|
| 137 |
error_reason = str(e)
|
| 138 |
|
| 139 |
finally:
|
| 140 |
+
# Clean up local temporary files
|
| 141 |
+
for path in [download_path, final_file_path, thumbnail_path]:
|
| 142 |
+
if path and os.path.exists(path):
|
| 143 |
+
try: os.remove(path)
|
| 144 |
+
except OSError as e: logger.error(f"Error removing file {path}: {e}")
|
| 145 |
|
| 146 |
+
# --- Final Step: Update Batch Status Safely ---
|
| 147 |
async with batch_info['lock']:
|
| 148 |
+
batch_info['processed_links'] += 1
|
| 149 |
+
if error_reason:
|
| 150 |
+
batch_info['failed_links'].append({"link": original_link, "error": error_reason})
|
| 151 |
+
|
| 152 |
+
processed = batch_info['processed_links']
|
| 153 |
+
total = batch_info['total_links']
|
| 154 |
+
|
| 155 |
+
try:
|
| 156 |
+
await bot.edit_message(
|
| 157 |
+
batch_info['chat_id'],
|
| 158 |
+
batch_info['status_message_id'],
|
| 159 |
+
text=templates.BotResponses.BATCH_UPDATE_VALIDATED.format(
|
| 160 |
+
batch_id=batch_id,
|
| 161 |
+
valid_count=total,
|
| 162 |
+
total_count=batch_info['original_total'],
|
| 163 |
+
skipped_count=len(batch_info['skipped_links']),
|
| 164 |
+
progress_bar=helpers.create_progress_bar(processed / total),
|
| 165 |
+
processed_count=processed
|
| 166 |
+
)
|
| 167 |
+
)
|
| 168 |
+
except Exception: pass
|
| 169 |
+
|
| 170 |
+
if processed == total:
|
| 171 |
+
logger.info(f"[{worker_name}] Batch {batch_id} complete.")
|
| 172 |
+
failed_items = batch_info['skipped_links'] + batch_info['failed_links']
|
| 173 |
+
|
| 174 |
+
if failed_items:
|
| 175 |
+
failed_details = "\n".join([f"- `{item.get('link', 'N/A')}` ({item.get('error', 'Unknown Error')})" for item in failed_items])
|
| 176 |
+
final_text = templates.BotResponses.BATCH_COMPLETE_SUMMARY.format(
|
| 177 |
+
batch_id=batch_id,
|
| 178 |
+
success_count=total - len(failed_items),
|
| 179 |
+
skipped_count=len(failed_items),
|
| 180 |
+
failed_details=failed_details
|
| 181 |
+
)
|
| 182 |
+
else:
|
| 183 |
+
final_text = templates.BotResponses.BATCH_COMPLETE_NO_SKIPS.format(batch_id=batch_id, success_count=total)
|
| 184 |
+
|
| 185 |
+
try:
|
| 186 |
+
await bot.edit_message(batch_info['chat_id'], batch_info['status_message_id'], final_text)
|
| 187 |
+
except Exception: pass
|
| 188 |
+
|
| 189 |
+
if batch_id in BATCH_JOBS: del BATCH_JOBS[batch_id]
|
| 190 |
|
| 191 |
|
| 192 |
async def worker(name: str, queue: asyncio.Queue):
|
|
|
|
| 194 |
while True:
|
| 195 |
task = await queue.get()
|
| 196 |
try:
|
| 197 |
+
await _process_task(task, name)
|
|
|
|
|
|
|
| 198 |
finally:
|
| 199 |
queue.task_done()
|
| 200 |
|
| 201 |
+
|
| 202 |
def start_bot_runtime():
|
| 203 |
"""Creates all the background tasks for the bot's engine."""
|
| 204 |
for i in range(config.PREMIUM_WORKERS):
|
|
|
|
| 208 |
asyncio.create_task(worker(f"FreeWorker-{i+1}", FREE_QUEUE))
|
| 209 |
|
| 210 |
asyncio.create_task(scheduler_loop())
|
|
|
|
| 211 |
logger.info(f"Bot runtime started with {config.PREMIUM_WORKERS} premium and {config.FREE_WORKERS} free workers.")
|
|
|