From 78e500281800960dc87e665246be13055f0798cb Mon Sep 17 00:00:00 2001 From: BennyThink Date: Sat, 8 Jan 2022 22:05:52 +0800 Subject: [PATCH] convert audio in celery task disabled for now --- ytdlbot/tasks.py | 38 +++++++++++++++++++++++++++++++++++++- ytdlbot/ytdl_bot.py | 22 ++-------------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/ytdlbot/tasks.py b/ytdlbot/tasks.py index 58cc31d..eb3e466 100644 --- a/ytdlbot/tasks.py +++ b/ytdlbot/tasks.py @@ -21,7 +21,7 @@ from client_init import create_app from config import BROKER, ENABLE_CELERY, OWNER, WORKERS from constant import BotText from db import Redis -from downloader import sizeof_fmt, upload_hook, ytdl_download +from downloader import convert_flac, sizeof_fmt, upload_hook, ytdl_download from utils import (apply_log_formatter, customize_logger, get_metadata, get_user_settings) @@ -46,6 +46,14 @@ def download_task(chat_id, message_id, url): logging.info("celery tasks ended.") +@app.task() +def audio_task(chat_id, message_id): + logging.info("Audio celery tasks started for %s-%s", chat_id, message_id) + bot_msg = celery_client.get_messages(chat_id, message_id) + normal_audio(bot_msg) + logging.info("Audio celery tasks ended.") + + def download_entrance(bot_msg, client, url): if ENABLE_CELERY: download_task.delay(bot_msg.chat.id, bot_msg.message_id, url) @@ -53,6 +61,34 @@ def download_entrance(bot_msg, client, url): normal_download(bot_msg, client, url) +def audio_entrance(bot_msg): + if ENABLE_CELERY: + normal_audio(bot_msg) + # disable celery audio conversion for now + # audio_task.delay(bot_msg.chat.id, bot_msg.message_id) + else: + normal_audio(bot_msg) + + +def normal_audio(bot_msg): + chat_id = bot_msg.chat.id + mp4_name = bot_msg.video.file_name # 'youtube-dl_test_video_a.mp4' + flac_name = mp4_name.replace("mp4", "m4a") + + with tempfile.NamedTemporaryFile() as tmp: + logging.info("downloading to %s", tmp.name) + celery_client.send_chat_action(chat_id, 'record_video_note') + celery_client.download_media(bot_msg, tmp.name) + logging.info("downloading complete %s", tmp.name) + # execute ffmpeg + celery_client.send_chat_action(chat_id, 'record_audio') + flac_tmp = convert_flac(flac_name, tmp) + celery_client.send_chat_action(chat_id, 'upload_audio') + celery_client.send_audio(chat_id, flac_tmp) + Redis().update_metrics("audio_success") + os.unlink(flac_tmp) + + def get_worker_status(username): worker_name = os.getenv("WORKER_NAME") me = celery_client.get_me() diff --git a/ytdlbot/ytdl_bot.py b/ytdlbot/ytdl_bot.py index 7904a13..0211b7e 100644 --- a/ytdlbot/ytdl_bot.py +++ b/ytdlbot/ytdl_bot.py @@ -10,7 +10,6 @@ __author__ = "Benny " import logging import os import re -import tempfile import typing from apscheduler.schedulers.background import BackgroundScheduler @@ -24,9 +23,8 @@ from config import (AUTHORIZED_USER, ENABLE_CELERY, ENABLE_VIP, OWNER, REQUIRED_MEMBERSHIP) from constant import BotText from db import MySQL, Redis -from downloader import convert_flac from limit import verify_payment -from tasks import download_entrance +from tasks import audio_entrance, download_entrance from utils import (customize_logger, get_revision, get_user_settings, set_user_settings) @@ -202,23 +200,7 @@ def audio_callback(client: "Client", callback_query: types.CallbackQuery): Redis().update_metrics("audio_request") msg = callback_query.message - - chat_id = msg.chat.id - mp4_name = msg.video.file_name # 'youtube-dl_test_video_a.mp4' - flac_name = mp4_name.replace("mp4", "m4a") - - with tempfile.NamedTemporaryFile() as tmp: - logging.info("downloading to %s", tmp.name) - client.send_chat_action(chat_id, 'record_video_note') - client.download_media(msg, tmp.name) - logging.info("downloading complete %s", tmp.name) - # execute ffmpeg - client.send_chat_action(chat_id, 'record_audio') - flac_tmp = convert_flac(flac_name, tmp) - client.send_chat_action(chat_id, 'upload_audio') - client.send_audio(chat_id, flac_tmp) - Redis().update_metrics("audio_success") - os.unlink(flac_tmp) + audio_entrance(msg) if __name__ == '__main__':