Add to main:
import logging from telegram.ext import Application, CommandHandler, MessageHandler, filters from config import BOT_TOKEN logging.basicConfig(level=logging.INFO) logger = logging.getLogger()
[Unit] Description=YouTube Playlist Telegram Bot After=network.target [Service] User=youruser WorkingDirectory=/home/youruser/youtube-playlist-bot ExecStart=/home/youruser/youtube-playlist-bot/venv/bin/python bot.py Restart=always Telegram Bot To Download Youtube Playlist
for idx, video in enumerate(videos, 1): await context.bot.send_message(chat_id, f"⬇️ Downloading idx/len(videos): video['title']") try: if format_type == 'video': file_path = await loop.run_in_executor( executor, download_video, video['url'], user_dir ) else: file_path = await loop.run_in_executor( executor, download_audio, video['url'], user_dir ) # Step 3: Send file with open(file_path, 'rb') as f: if format_type == 'video': await context.bot.send_video(chat_id, f, caption=video['title']) else: await context.bot.send_audio(chat_id, f, title=video['title']) # Clean up os.remove(file_path) except Exception as e: await context.bot.send_message(chat_id, f"Failed for video['title']: str(e)")
with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([video_url]) # return actual file path info = ydl.extract_info(video_url, download=False) filename = ydl.prepare_filename(info).replace('.webm', '.mp4') return filename Add to main: import logging from telegram
# Start download process in background context.application.create_task( process_playlist(update.effective_chat.id, url, choice, context) )
await query.edit_message_text(f"⏳ Fetching playlist: url\nThis may take a moment...") video in enumerate(videos
import os file_size_mb = os.path.getsize(file_path) / (1024 * 1024) if file_size_mb > 50: await context.bot.send_message( chat_id, f"⚠️ video['title'] is file_size_mb:.1fMB > 50MB, skipped." ) continue 5.1 User Queue to Avoid Overload user_tasks = {} async def process_playlist_safe(chat_id, url, format_type, context): if chat_id in user_tasks and not user_tasks[chat_id].done(): await context.bot.send_message(chat_id, "You already have a playlist processing. Please wait.") return
async def start(update, context): await update.message.reply_text( "Send me a YouTube playlist URL.\n" "I'll download up to 15 videos (audio or video)." )