Change to using youtube_search instead of yt-dlp for searches

This commit is contained in:
a 2024-05-10 19:02:03 +00:00
parent 6b091b0bd1
commit 3914b44274

View file

@ -227,8 +227,9 @@ class Music(Cog):
return await self.search_youtube(ctx, query=query) return await self.search_youtube(ctx, query=query)
if i not in range(self.MAX_RESULTS + 1): if i not in range(self.MAX_RESULTS + 1):
return await ctx.send(f"Please provide an integer between 1 and {self.MAX_RESULTS}") return await ctx.send(f"Please provide an integer between 1 and {self.MAX_RESULTS}")
url = self.search_results['entries'][i]['url'] url = f"https://youtube.com/watch?v={self.search_results[i]['id']}"
self.search_results = [] self.search_results = []
self.query = ""
logger.info(f"handling a prior search") logger.info(f"handling a prior search")
return await self.get_tracks_from_url(ctx, url) return await self.get_tracks_from_url(ctx, url)
@ -337,7 +338,8 @@ class Music(Cog):
"""Do a YouTube search for the given query""" """Do a YouTube search for the given query"""
logger.debug(f"search_youtube() called for query: {query}") logger.debug(f"search_youtube() called for query: {query}")
try: try:
self.search_results = ytdl.extract_info(f"ytsearch{self.MAX_RESULTS}:{query}", download=False) self.query = query
self.search_results = YoutubeSearch(query, max_results=MAX_RESULTS).to_dict()
except Exception as e: except Exception as e:
logger.error("Exception thrown!") logger.error("Exception thrown!")
logger.error(f"{e=}") logger.error(f"{e=}")
@ -366,45 +368,33 @@ class Music(Cog):
formatted_results = ( formatted_results = (
f"Performed a search for `{self.search_results['id']}`.\n" f"Performed a search for `{self.search_results['id']}`.\n"
"Which track would you like to play?\n" "Which track would you like to play?\n"
"Make your choice using the `play` command.\n\n" "Make your choice using the `play` command, e.g. `.play 1`.\n\n"
) )
for i, result in enumerate(self.search_results['entries']): for i, result in enumerate(self.search_results['entries']):
result: dict result: dict
if result['live_status'] == "is_upcoming":
continue # skip YT Premieres
title = result.get('title', '<no title found>') title = result.get('title', '<no title found>')
duration = format_time(int(result.get('duration'))) if ('duration' in result) else '?:??' duration = result.get('duration', '?:??')
uploader = result.get('channel', '<no uploader found>') uploader = result.get('channel', '<no uploader found>')
views = "{:,}".format(result.get('view_count')) if ('view_count' in result) else '<no view count found>' views = result.get('views', '??? views')
image = result['thumbnails'][-1]['url'] date = result.get('publish_time', 'some time ago')
height = result['thumbnails'][-1]['height'] image = result.get('thumbnails', [None])[-1]
width = result['thumbnails'][-1]['width'] url = f"https://youtube.com/watch?v={result.get('id', '')}"
url = result['url']
formatted_results += ( formatted_results += (
f"{i+1}: **{title}** ({duration})\n" f"{i+1}: **{title}**\n"
f"{uploader} - {views} views\n" f"{uploader} - {views} ({duration})\n"
) )
embeds.append( embeds.append(
discord.Embed( discord.Embed(
title = title, title = title,
description = f"Uploaded by {uploader} {date}\n{duration} - {views}",
url = url, url = url,
type = 'image', type = 'image',
colour = 0xff0000, colour = 0xff0000,
).add_field(
name = "Duration",
value = duration,
).add_field(
name = "Views",
value = views,
).add_field(
name = "Uploaded by",
value = uploader,
).set_thumbnail( ).set_thumbnail(
url = image, url = image,
) )