From 382c42262954767acc3f5ee3e008e03acbfc4f26 Mon Sep 17 00:00:00 2001 From: Parker Date: Thu, 28 Nov 2024 00:04:48 -0600 Subject: Overhaul \`play\` command - Split custom sources into helper functions - Add proper logging and handling - Fix LoadError embed messsage --- code/utils/source_helpers/parse.py | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 code/utils/source_helpers/parse.py (limited to 'code/utils/source_helpers/parse.py') diff --git a/code/utils/source_helpers/parse.py b/code/utils/source_helpers/parse.py new file mode 100644 index 0000000..8489515 --- /dev/null +++ b/code/utils/source_helpers/parse.py @@ -0,0 +1,80 @@ +import discord + +from utils.source_helpers.apple import ( + album as apple_album, + playlist as apple_playlist, + song as apple_song, +) +from utils.source_helpers.spotify import ( + album as spotify_album, + playlist as spotify_playlist, + song as spotify_song, +) +from utils.custom_sources import AppleSource, SpotifySource + + +async def parse_custom_source( + self, provider: str, query: str, user: discord.User +): + """ + Parse the query and run the appropriate functions to get the results/info + + Return the results and an embed or None, None + """ + load_funcs = { + "apple": { + "album": apple_album.load, + "playlist": apple_playlist.load, + "song": apple_song.load, + }, + "spotify": { + "album": spotify_album.load, + "playlist": spotify_playlist.load, + "song": spotify_song.load, + }, + } + + headers = { + "apple": self.bot.apple_headers, + "spotify": self.bot.spotify_headers, + } + + sources = { + "apple": AppleSource, + "spotify": SpotifySource, + } + + # Catch all songs + if "?i=" in query or "/track/" in query: + song, embed = await load_funcs[provider]["song"]( + headers[provider], query, user + ) + + if song: + results = await sources[provider].load_item(self, user, song) + else: + return None, embed + # Catch all playlists + elif "/playlist/" in query: + playlist, embed = await load_funcs[provider]["playlist"]( + headers[provider], query, user + ) + + if playlist: + results = await sources[provider].load_playlist( + self, user, playlist + ) + else: + return None, embed + # Catch all albums + elif "/album/" in query: + album, embed = await load_funcs[provider]["album"]( + headers[provider], query, user + ) + + if album: + results = await sources[provider].load_album(self, user, album) + else: + return None, embed + + return results, embed -- cgit v1.2.3-70-g09d2