fix: remove client-side format cache, prefer first audio track over largest
This commit is contained in:
parent
e2ead9db52
commit
60a0373d7f
3 changed files with 90 additions and 81 deletions
27
bot.py
27
bot.py
|
|
@ -22,10 +22,6 @@ HTTP_TIMEOUT = httpx.Timeout(connect=None, read=None, write=None, pool=None)
|
|||
# Таймаут для запроса форматов (не такой критичный, но не должен висеть вечно)
|
||||
FORMATS_TIMEOUT = httpx.Timeout(connect=15, read=30, write=15, pool=15)
|
||||
|
||||
# Клиентский кэш форматов: {normalized_url: (timestamp, formats)}
|
||||
_formats_cache: dict[str, tuple[float, list[dict]]] = {}
|
||||
_FORMATS_CACHE_TTL = 30 * 60 # 30 минут
|
||||
|
||||
# Настройка логирования
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
|
|
@ -971,29 +967,10 @@ async def download_tiktok_video(url: str, chat_id: int, max_retries: int = 3) ->
|
|||
# ============================================================================
|
||||
|
||||
|
||||
def _normalize_youtube_url_for_cache(url: str) -> str:
|
||||
"""Нормализует URL для кэша: оставляет только video ID"""
|
||||
import re
|
||||
m = re.search(r'(youtu\.be/|youtube\.com/watch\?v=)([a-zA-Z0-9_-]{11})', url)
|
||||
if m:
|
||||
return f"https://www.youtube.com/watch?v={m.group(2)}"
|
||||
return url
|
||||
|
||||
|
||||
async def get_formats_from_service(url: str) -> list[dict] | None:
|
||||
"""Получает список доступных форматов для YouTube URL через сервис youtube-downloader"""
|
||||
logger.info(f"Получение форматов для YouTube: {url}")
|
||||
|
||||
cache_key = _normalize_youtube_url_for_cache(url)
|
||||
now = time.time()
|
||||
|
||||
if cache_key in _formats_cache:
|
||||
cached_time, cached_formats = _formats_cache[cache_key]
|
||||
if now - cached_time < _FORMATS_CACHE_TTL:
|
||||
logger.info(f"Форматы взяты из кэша ({len(cached_formats)} шт., возраст {now - cached_time:.0f}с)")
|
||||
return cached_formats
|
||||
del _formats_cache[cache_key]
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=FORMATS_TIMEOUT) as client:
|
||||
response = await client.post(
|
||||
|
|
@ -1003,9 +980,7 @@ async def get_formats_from_service(url: str) -> list[dict] | None:
|
|||
)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
formats = data.get('formats', [])
|
||||
_formats_cache[cache_key] = (time.time(), formats)
|
||||
return formats
|
||||
return data.get('formats', [])
|
||||
logger.warning(f"Не удалось получить форматы: {response.status_code}")
|
||||
return None
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue