diff options
author | Parker <contact@pkrm.dev> | 2024-12-03 06:05:14 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-03 06:05:14 +0000 |
commit | 15e33831639355546b32477a6870eb0a3ac47e24 (patch) | |
tree | a5455e0a8391747c7226a751354b7236c8c5d40b /code/utils/source_helpers/spotify/album.py | |
parent | fcbfe460701316ded25e29356ed1fda42386e5c0 (diff) | |
parent | ce18cd27488d90fbd0aae7319a36a89e9fa85aa7 (diff) |
Merge pull request #10 from PacketParker/dev
Update
Diffstat (limited to 'code/utils/source_helpers/spotify/album.py')
-rw-r--r-- | code/utils/source_helpers/spotify/album.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/code/utils/source_helpers/spotify/album.py b/code/utils/source_helpers/spotify/album.py new file mode 100644 index 0000000..0ebc7d5 --- /dev/null +++ b/code/utils/source_helpers/spotify/album.py @@ -0,0 +1,68 @@ +import datetime +import discord +import requests +from typing import Tuple, Optional +from requests.exceptions import JSONDecodeError + +from utils.config import create_embed, LOG + + +async def load( + headers: dict, + query: str, + user: discord.User, +) -> Tuple[Optional[dict], Optional[discord.Embed]]: + """ + Get the album info from the Spotify API + """ + album_id = query.split("/album/")[1] + + try: + # Get the album info + response = requests.get( + f"https://api.spotify.com/v1/albums/{album_id}", + headers=headers, + ) + + if response.status_code == 404: + embed = create_embed( + title="Album Not Found", + description=( + "The album could not be found as the provided link is" + " invalid. Please try again." + ), + ) + return None, embed + + if response.status_code == 401: + LOG.error( + "Could not authorize with Spotify API. Likely need to" + " restart the bot." + ) + return None, None + + response.raise_for_status() + # Unpack the album info + album = response.json() + name = album["name"] + artist = album["artists"][0]["name"] + num_tracks = len(album["tracks"]["items"]) + artwork_url = album["images"][0]["url"] + except IndexError: + LOG.error("Failed unpacking Spotify album info") + return None, None + except (JSONDecodeError, requests.HTTPError): + LOG.error("Failed making request to Spotify API") + return None, None + + embed = create_embed( + title="Album Queued", + description=( + f"**{name}** by **{artist}**\n" + f"` {num_tracks} ` tracks\n\n" + f"Queued by: {user.mention}" + ), + thumbnail=artwork_url, + ) + + return album, embed |