From 3914b4427490ae86392d8158a565f10fd71f2033 Mon Sep 17 00:00:00 2001 From: a Date: Fri, 10 May 2024 19:02:03 +0000 Subject: [PATCH] Change to using youtube_search instead of yt-dlp for searches --- cogs/music.py | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/cogs/music.py b/cogs/music.py index f8526bf..d4b8aac 100644 --- a/cogs/music.py +++ b/cogs/music.py @@ -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', '') - duration = format_time(int(result.get('duration'))) if ('duration' in result) else '?:??' + duration = result.get('duration', '?:??') uploader = result.get('channel', '') - views = "{:,}".format(result.get('view_count')) if ('view_count' in result) else '' - 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, )