diff --git a/bot.py b/bot.py index 1a59590..222d62a 100644 --- a/bot.py +++ b/bot.py @@ -576,25 +576,37 @@ async def process_queue_item(item: QueueItem): # Отправляем файл пользователю await item.status_message.edit_text(get_text(item.locale, 'sending')) - video_file = open(video_path, 'rb') caption = get_text(item.locale, 'caption', bot_username=TELEGRAM_BOT_USERNAME) caption += f"\n\n{item.url}" - + # Определяем имя файла для отправки video_filename = Path(video_path).name - - # Отправляем как видео со streaming — встроенный плеер Telegram - await item.original_message.reply_video( - video=video_file, - filename=video_filename, - caption=caption, - supports_streaming=True, - read_timeout=600, - write_timeout=600, - connect_timeout=60, - pool_timeout=60 - ) - video_file.close() + + # Отправляем как видео со streaming — встроенный плеер Telegram. + # Загрузка большого файла в Telegram API иногда обрывается транспортной + # ошибкой httpx (ReadError/RemoteProtocolError) — ретраим, открывая файл заново. + send_retries = 3 + for attempt in range(send_retries): + video_file = open(video_path, 'rb') + try: + await item.original_message.reply_video( + video=video_file, + filename=video_filename, + caption=caption, + supports_streaming=True, + read_timeout=600, + write_timeout=600, + connect_timeout=60, + pool_timeout=60 + ) + break + except Exception as send_error: + if attempt == send_retries - 1: + raise + logger.warning(f"Отправка видео: попытка {attempt + 1}/{send_retries} не удалась: {send_error}") + await asyncio.sleep((attempt + 1) * 2) + finally: + video_file.close() # Увеличиваем счетчик скачанных видео increment_downloads() diff --git a/youtube-downloader/Dockerfile b/youtube-downloader/Dockerfile index 52a2a8b..0d076b5 100644 --- a/youtube-downloader/Dockerfile +++ b/youtube-downloader/Dockerfile @@ -6,4 +6,4 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . RUN mkdir -p downloads ENV PYTHONUNBUFFERED=1 -CMD sh -c "gunicorn --workers=1 --timeout=600 --preload --max-requests=1 --bind=0.0.0.0:\${PORT:-5000} app:app" +CMD sh -c "gunicorn --workers=1 --timeout=600 --preload --max-requests=20 --max-requests-jitter=5 --bind=0.0.0.0:\${PORT:-5000} app:app" diff --git a/youtube-downloader/app.py b/youtube-downloader/app.py index 2754212..3d74c8c 100644 --- a/youtube-downloader/app.py +++ b/youtube-downloader/app.py @@ -133,8 +133,7 @@ YTDLP_CMD = 'yt-dlp' DOWNLOAD_TIMEOUT = 300 INFO_TIMEOUT = 60 -PLAYER_CLIENTS = 'web,android' -EXTRACTOR_ARGS = 'youtube:player_client=web,android:skip=translated_subs,hls' +EXTRACTOR_ARGS = 'youtube:player_client=web,android;skip=translated_subs,hls' def _build_ytdlp_base_cmd() -> list: