Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Python Telegram Bot - Youtube Downloader

$
0
0

I Have This Code :

import osimport telepotfrom pytube import YouTubedef handle(msg):    chat_id = msg['chat']['id']    command = msg['text']    if command.startswith('/start'):        bot.sendMessage(chat_id, 'Hello! Send me a YouTube link to download the video :).')    else:        try:            yt = YouTube(command)            available_resolutions = []            for stream in yt.streams.filter(progressive=True, file_extension='mp4'):                resolution = f"{stream.resolution}p"                if resolution not in available_resolutions:                    available_resolutions.append(resolution)            if not available_resolutions:                bot.sendMessage(chat_id, 'Sorry, there are no available resolutions for this video.')            else:                # Ask user to choose resolution                keyboard = [[f"{resolution}"] for resolution in available_resolutions]                reply_markup = {'keyboard': keyboard, 'one_time_keyboard': True}                bot.sendMessage(chat_id, 'Please select a resolution:', reply_markup=reply_markup)                bot._set_inline_keyboard(chat_id, [])        except Exception as e:            bot.sendMessage(chat_id, 'Sorry, there was an error processing your request.')            print(str(e))    # Wait for user's response    user_response = ''    while user_response == '':        updates = bot.getUpdates()        for update in updates:            if 'message' in update and update['message']['chat']['id'] == chat_id:                user_response = update['message']['text']    # Download video with chosen resolution    video = yt.streams.filter(progressive=True, file_extension='mp4', resolution=user_response).first()    video.download()    bot.sendVideo(chat_id, open(video.default_filename, 'rb'), caption='Here is your video!')    os.remove(video.default_filename)TOKEN = 'MY TOKEN BOT'bot = telepot.Bot(TOKEN)bot.message_loop(handle)while True:    pass

When I start the bot and send it the video link, the only message I get is the (Sorry, there was an error processing your request) message. How do I fix this problem?

I tried to solve this problem by changing the quality functions, but no matter what I tried, the problem was not solved.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>