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)
if i not in range(self.MAX_RESULTS + 1):
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.query = ""
logger.info(f"handling a prior search")
return await self.get_tracks_from_url(ctx, url)
@ -337,7 +338,8 @@ class Music(Cog):
"""Do a YouTube search for the given query"""
logger.debug(f"search_youtube() called for query: {query}")
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:
logger.error("Exception thrown!")
logger.error(f"{e=}")
@ -366,45 +368,33 @@ class Music(Cog):
formatted_results = (
f"Performed a search for `{self.search_results['id']}`.\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']):
result: dict
if result['live_status'] == "is_upcoming":
continue # skip YT Premieres
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>')
views = "{:,}".format(result.get('view_count')) if ('view_count' in result) else '<no view count found>'
image = result['thumbnails'][-1]['url']
height = result['thumbnails'][-1]['height']
width = result['thumbnails'][-1]['width']
url = result['url']
views = result.get('views', '??? views')
date = result.get('publish_time', 'some time ago')
image = result.get('thumbnails', [None])[-1]
url = f"https://youtube.com/watch?v={result.get('id', '')}"
formatted_results += (
f"{i+1}: **{title}** ({duration})\n"
f"{uploader} - {views} views\n"
f"{i+1}: **{title}**\n"
f"{uploader} - {views} ({duration})\n"
)
embeds.append(
discord.Embed(
title = title,
description = f"Uploaded by {uploader} {date}\n{duration} - {views}",
url = url,
type = 'image',
colour = 0xff0000,
).add_field(
name = "Duration",
value = duration,
).add_field(
name = "Views",
value = views,
).add_field(
name = "Uploaded by",
value = uploader,
).set_thumbnail(
url = image,
)