aboutsummaryrefslogtreecommitdiff
path: root/code/utils/source_helpers/spotify/artist.py
blob: 995e20847981791910aa4863b8467d5f96ab49af (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
69
70
71
72
73
74
75
76
77
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 artists top tracks from the Spotify API
    """
    artist_id = query.split("/artist/")[1]

    try:
        # Get the artists songs
        response = requests.get(
            f"https://api.spotify.com/v1/artists/{artist_id}/top-tracks",
            headers=headers,
        )

        if response.status_code == 404:
            embed = create_embed(
                title="Artist Not Found",
                description=(
                    "Either the provided link is malformed, the artist does"
                    " not exist, or the artist does not have any songs."
                ),
            )
            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 artists songs
        artist = response.json()
        name = artist["tracks"][0]["artists"][0]["name"]
        num_tracks = len(artist["tracks"])

        # Get the artist info (for the thumbnail)
        response = requests.get(
            f"https://api.spotify.com/v1/artists/{artist_id}",
            headers=headers,
        )

        response.raise_for_status()
        try:
            artwork_url = response.json()["images"][0]["url"]
        except IndexError:
            artwork_url = None

    except IndexError:
        LOG.error("Failed unpacking Spotify artist info")
        return None, None
    except (JSONDecodeError, requests.HTTPError):
        LOG.error("Failed making request to Spotify API")
        return None, None

    embed = create_embed(
        title="Artist Queued",
        description=(
            f"Top `{num_tracks}` track by **{name}**\n\n"
            f"Queued by {user.mention}"
        ),
        thumbnail=artwork_url,
    )
    return artist, embed