aboutsummaryrefslogtreecommitdiff
path: root/code/utils/source_helpers/spotify/song.py
blob: 258b652e9d74be6bdbbdcd630c46fd66bec1da6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import datetime
import discord
import requests
from typing import Tuple, Optional
from requests.exceptions import JSONDecodeError

from utils.config import BOT_COLOR, LOG


async def load(
    headers: dict,
    query: str,
    user: discord.User,
) -> Tuple[Optional[dict], Optional[discord.Embed]]:
    """
    Get the song info from the Spotify API
    """
    song_id = query.split("/track/")[1]

    try:
        # Get the song info
        response = requests.get(
            f"https://api.spotify.com/v1/tracks/{song_id}",
            headers=headers,
        )

        if response.status_code == 404:
            embed = discord.Embed(
                title="Song Not Found",
                description=(
                    "The song could not be found as the provided link is"
                    " invalid. Please try again."
                ),
                color=BOT_COLOR,
            )
            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 song info
        song = response.json()
        name = song["name"]
        artist = song["artists"][0]["name"]
        artwork_url = song["album"]["images"][0]["url"]
    except IndexError:
        LOG.error("Failed unpacking Spotify song info")
        return None, None
    except (JSONDecodeError, requests.HTTPError):
        LOG.error("Failed making request to Spotify API")
        return None, None

    embed = discord.Embed(
        title="Song Queued",
        description=f"**{name}** by **{artist}**\n\nQueued by {user.mention}",
        color=BOT_COLOR,
    )
    embed.set_thumbnail(url=artwork_url)
    embed.set_footer(
        text=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + " UTC"
    )

    return song, embed