-
Notifications
You must be signed in to change notification settings - Fork 5
/
spotify_client.py
37 lines (30 loc) · 1.02 KB
/
spotify_client.py
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
import os
from dataclasses import dataclass
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import dotenv
dotenv.load_dotenv()
@dataclass
class Playlist:
name: str
description: str
tracks: list[str]
class SpotifyClient:
def __init__(self) -> None:
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
auth_manager = SpotifyClientCredentials(
client_id=client_id, client_secret=client_secret
)
self.spotify = spotipy.Spotify(auth_manager=auth_manager)
def get_playlist(self, id: str):
playlist = self.spotify.playlist(id)
queries = []
tracks = playlist["tracks"]["items"]
for track in tracks:
track_name = track["track"]["name"]
artists = ", ".join(
[artist["name"] for artist in track["track"]["artists"]]
)
queries.append(f"{track_name} by {artists}")
return Playlist(playlist["name"], playlist["description"], queries)