From a40679ee1179400c9c3f63978ee4b0d2949c381f Mon Sep 17 00:00:00 2001 From: Top1055 <123alexfeetham@gmail.com> Date: Fri, 10 Nov 2023 14:22:12 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20spotify=20playlists=20now=20work?= =?UTF-8?q?ing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cogs/music/main.py | 11 ----------- cogs/music/queue.py | 21 ++++++++++----------- cogs/music/translate.py | 36 +++++++++++++++++++++++++++++++++--- cogs/music/util.py | 2 +- requirements.txt | 1 + 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/cogs/music/main.py b/cogs/music/main.py index eb246fe..0bda551 100644 --- a/cogs/music/main.py +++ b/cogs/music/main.py @@ -7,7 +7,6 @@ import cogs.music.translate as translate from cogs.music.help import music_help -import discord import spotipy from spotipy.oauth2 import SpotifyClientCredentials @@ -39,16 +38,6 @@ def get_spotify_creds(): -# call play on ffmpeg exit -class AstroPlayer(discord.FFmpegPCMAudio): - def __init__(self, ctx, source, options) -> None: - #self.ctx = ctx - super().__init__(source, **options) - - def _kill_process(self): - super()._kill_process() - #asyncio.create_task(play(self.ctx)) - class music(commands.Cog): def __init__(self, client): self.client = client diff --git a/cogs/music/queue.py b/cogs/music/queue.py index 8cbfeca..2c6d9a7 100644 --- a/cogs/music/queue.py +++ b/cogs/music/queue.py @@ -278,6 +278,15 @@ async def grab_songs(server_id): return max, songs +# call play on ffmpeg exit +class AstroPlayer(discord.FFmpegPCMAudio): + def __init__(self, ctx, source, options) -> None: + self.ctx = ctx + super().__init__(source, **options) + + def _kill_process(self): + super()._kill_process() + asyncio.run(play(self.ctx)) # Play and loop songs in server async def play(ctx): @@ -297,14 +306,4 @@ async def play(ctx): # else play next song and call play again await ctx.voice_client.play( - AstroPlayer(ctx, url, FFMPEG_OPTS)) - -# call play on ffmpeg exit -class AstroPlayer(discord.FFmpegPCMAudio): - def __init__(self, ctx, source, options) -> None: - self.ctx = ctx - super().__init__(source, **options) - - def _kill_process(self): - super()._kill_process() - asyncio.run(play(self.ctx)) \ No newline at end of file + AstroPlayer(ctx, url, FFMPEG_OPTS)) \ No newline at end of file diff --git a/cogs/music/translate.py b/cogs/music/translate.py index 182ed79..5cd0830 100644 --- a/cogs/music/translate.py +++ b/cogs/music/translate.py @@ -23,7 +23,7 @@ def main(url, sp): if 'track' in url: return spotify_song(url, sp) elif 'playlist' in url: - return spotify_playlist(url) + return spotify_playlist(url, sp) soundcloud_song = 'soundcloud' in url and 'sets' not in url # Not implemented yet @@ -75,8 +75,38 @@ def spotify_song(url, sp): return search_song(query) -def spotify_playlist(url): - return [] +def spotify_playlist(url, sp): + # Get the playlist uri code + code = url.split("/")[-1].split("?")[0] + + # Grab the tracks if the playlist is correct + try: + results = sp.playlist_tracks(code)['items'] + except spotipy.exceptions.SpotifyException: + return [] + + # Go through the tracks + songs = [] + for track in results: + search = "" + + # Fetch all artists + for artist in track['track']['artists']: + + # Add all artists to search + search += f"{artist['name']}, " + + # Remove last column + search = search[:-2] + search += f" - {track['track']['name']}" + + searched_result = search_song(search) + if searched_result == []: + continue + + songs.append(searched_result[0]) + + return songs def song_download(url): diff --git a/cogs/music/util.py b/cogs/music/util.py index 80600f2..530030f 100644 --- a/cogs/music/util.py +++ b/cogs/music/util.py @@ -69,7 +69,7 @@ async def display_server_queue(ctx: Context, songs, n): title=f"{server.name}'s Queue!", color=config.get_color("main")) - display = f"🔊 Currently playing: ``{await queue.get_current_song(ctx.guild.id)}``\n" + display = f"🔊 Currently playing: ``{await queue.get_current_song(ctx.guild.id)}``\n\n" for i, song in enumerate(songs): display += f"``{i + 1}.`` {song[0]} - {format_time(song[1])} Queued by {song[2]}\n" msg.add_field(name="Songs:", diff --git a/requirements.txt b/requirements.txt index cf613e7..6e575d5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,3 +25,4 @@ urllib3==2.0.2 websockets==11.0.3 yarl==1.9.2 yt-dlp +spotipy