Python yt-dlp doesn't find the video from YouTube that definitely exists

174 views Asked by At

I wanna make bot for telegram that will take link or the name of video, show 10 first results of what will be in youtube and than I could choose what video I want to download. (I'm using MacOS)

@bot.message_handler(func=lambda message: is_started is True)
def handle_message(message):
global user_format_choice
global video_links
global user_video_url
query = message.text
print("ISmessage_handler_func")

if AddFunc.is_valid_url(query):
    print("message_handler_url_accepted")
    user_video_url = query

    kb = types.InlineKeyboardMarkup(row_width=1)
    btn_audio = types.InlineKeyboardButton(text='Скачать аудио', callback_data='audio')
    btn_video = types.InlineKeyboardButton(text='Скачать видео', callback_data='video')
    kb.add(btn_audio, btn_video)
    bot.send_message(message.chat.id, "Выберите действие:", reply_markup=kb)
else:
    video_links.clear()

    print("message_handler_url_is_not_accepted")
    print(query)
    ydl_opts = {
        'quiet': True,
        'yes-playlist': False,
        'max_downloads' : 10,
        'skip_download' : True
    }
    print(1)

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        search_results = ydl.extract_info(f"ytsearch{numberOfVideos}:{query}", download=False)
    print(2)
    print(search_results)

    for entry in search_results['entries']:
        print("1 time loop got")
        video_title = entry['title']
        video_link = entry['url']
        video_links.append((video_title, video_link))

    print(3)

    keyboard = types.InlineKeyboardMarkup(row_width=1)
    for i, (video_title, video_link) in enumerate(video_links[:numberOfVideos]):
        button_text = f"{i + 1}. {video_title}"
        button = types.InlineKeyboardButton(text=button_text, callback_data=video_link)
        keyboard.add(button)

    print(4)
    if video_links.__len__() != 0:
        bot.send_message(message.chat.id, f"Вот {video_links.__len__()} видео по вашему запросу:", reply_markup=keyboard)
    else:
        bot.send_message(message.chat.id, f"По вашему запросу ничего не найдено! :(", reply_markup=keyboard)

The problem is that this thing works not always, for example if i make query Stxrbxtn(there are many videos in YT for this query), my bot will say me that nothing was found. Attaching print(search_results) for this query:

{'id': 'Stxrbxtn', 'title': 'Stxrbxtn', '_type': 'playlist', 'entries': [], 'webpage_url': 'ytsearch10:Stxrbxtn', 'original_url': 'ytsearch10:Stxrbxtn', 'webpage_url_basename': 'Stxrbxtn', 'webpage_url_domain': None, 'extractor': 'youtube:search', 'extractor_key': 'YoutubeSearch', 'playlist_count': 0, 'epoch': 1698245758}

I tried to change ydl_opts, i have 'yes-playlist': False for case if there will be playlist, but it seems that the problem in search_results maybe?? I'm writing here because for many other queries this works, and the program gives me 10 buttons with results from youtube. The problem is not in spaces, or characters, at least i guess so Could it be the problem with yt-dlp or maybe I don't know about something?

0

There are 0 answers