Fix YouTube 500 error (n-challenge) and Telegram callback_data overflow
This commit is contained in:
parent
4629535e97
commit
326eabaa99
12 changed files with 292 additions and 254 deletions
27
bot.py
27
bot.py
|
|
@ -984,9 +984,14 @@ async def get_formats_from_service(url: str) -> list[dict] | None:
|
|||
|
||||
|
||||
async def show_quality_selection(status_message: Message, formats: list[dict], locale: str):
|
||||
"""Показывает inline клавиатуру с выбором качества видео"""
|
||||
"""Показывает inline клавиатуру с выбором качества видео
|
||||
|
||||
Используем короткий индекс (quality:0, quality:1, ...) вместо полного format_id,
|
||||
т.к. Telegram ограничивает callback_data 64 байтами, а format_id может быть длинным
|
||||
(например "308+251-drc/bestvideo[height<=1080]+bestaudio/best[height<=1080]").
|
||||
"""
|
||||
keyboard = []
|
||||
for fmt in formats:
|
||||
for idx, fmt in enumerate(formats):
|
||||
label = fmt.get('label', fmt.get('quality', 'Unknown'))
|
||||
filesize = fmt.get('filesize_mb')
|
||||
if filesize:
|
||||
|
|
@ -995,7 +1000,7 @@ async def show_quality_selection(status_message: Message, formats: list[dict], l
|
|||
button_text = label
|
||||
keyboard.append([InlineKeyboardButton(
|
||||
text=button_text,
|
||||
callback_data=f"quality:{fmt['format_id']}"
|
||||
callback_data=f"quality:{idx}"
|
||||
)])
|
||||
|
||||
# Кнопка отмены
|
||||
|
|
@ -1032,8 +1037,17 @@ async def handle_format_selection(update: Update, context: ContextTypes.DEFAULT_
|
|||
await status_message.edit_text(get_text(locale, 'quality_cancelled'))
|
||||
return
|
||||
|
||||
# Извлекаем format_id
|
||||
format_id = callback_data.replace('quality:', '')
|
||||
# Извлекаем индекс формата и получаем format_id из сохранённого списка
|
||||
try:
|
||||
format_index = int(callback_data.replace('quality:', ''))
|
||||
formats_list = data.get('formats_list', [])
|
||||
if format_index < 0 or format_index >= len(formats_list):
|
||||
raise ValueError(f"Index {format_index} out of range")
|
||||
format_id = formats_list[format_index].get('format_id', '')
|
||||
except (ValueError, IndexError) as e:
|
||||
logger.error(f"Invalid format selection: {e}")
|
||||
await status_message.edit_text(get_text(locale, 'processing'))
|
||||
format_id = None # Скачиваем без выбора качества
|
||||
|
||||
# Обновляем сообщение - добавляем в очередь
|
||||
await status_message.edit_text(get_text(locale, 'processing'))
|
||||
|
|
@ -1139,7 +1153,8 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||
'chat_id': chat_id,
|
||||
'chat_type': chat_type,
|
||||
'original_message': update.message,
|
||||
'status_message': status_message
|
||||
'status_message': status_message,
|
||||
'formats_list': formats, # для lookup по индексу в callback
|
||||
}
|
||||
await show_quality_selection(status_message, formats, locale)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue