-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_client.py
118 lines (96 loc) · 3.52 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
from dotenv import load_dotenv
from typing import Any
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
load_dotenv()
class SpotifyClient:
"""Spotify class to handle spotify API requests"""
def __init__(self: "SpotifyClient") -> None:
client_id = os.getenv("SPOTIFY_CLIENT_ID")
client_secret = os.getenv("SPOTIFY_CLIENT_SECRET")
auth_manager = SpotifyClientCredentials(
client_id=client_id, client_secret=client_secret
)
self._spotify = spotipy.Spotify(auth_manager=auth_manager)
def get_artist(self: "SpotifyClient", artist_id: str) -> Any:
"""Gets artist information from Spotify API
Args:
self (SpotifyClient): Instance of SpotifyClient
artist_id (str): The artist id
Returns:
dict: Artist information
"""
return self._spotify.artist(artist_id=artist_id)
def search(
self: "SpotifyClient", q: str, cat: str, limit: int, offset: int
) -> Any:
"""Searches Spotify API for artists, albums, or tracks
Args:
self (SpotifyClient): Instance of SpotifyClient
q (str): The search query
cat (str): The category to search
limit (int): The number of results to return
offset (int): The offset for the search
Returns:
dict: The search results
"""
return self._spotify.search(q=q, type=cat, limit=limit, offset=offset)
def artists(self: "SpotifyClient", artists: list) -> Any:
"""Gets artist information from Spotify API
Args:
self (SpotifyClient): Instance of SpotifyClient
artists (list): The artist ids
Returns:
dict: Artist information
"""
return self._spotify.artists(artists=artists)
def artist_albums(
self: "SpotifyClient",
artist_id: str,
album_type: str,
limit: int = 20,
offset: int = 0,
) -> Any:
"""Gets albums for a given artist
Args:
self (SpotifyClient): Instance of SpotifyClient
artist_id (str): The artist id
album_type (str): The type of album
limit (int, optional): The number of albums to return. Defaults
to 20.
offset (int, optional): The offset for albums. Defaults to 0.
Returns:
dict: The albums
"""
return self._spotify.artist_albums(
artist_id=artist_id,
album_type=album_type,
limit=limit,
offset=offset,
)
def album_tracks(
self: "SpotifyClient", album_id: str, limit: int = 50, offset: int = 0
) -> Any:
"""Gets tracks for a given album
Args:
self (SpotifyClient): Instance of SpotifyClient
album_id (str): The album id
limit (int, optional): The number of tracks to return. Defaults
to 50.
offset (int, optional): The offset for tracks. Defaults to 0.
Returns:
dict: The tracks
"""
return self._spotify.album_tracks(
album_id=album_id, limit=limit, offset=offset
)
def albums(self: "SpotifyClient", albums: list) -> Any:
"""Gets album information from Spotify API
Args:
self (SpotifyClient): Instance of SpotifyClient
albums (list): The album ids
Returns:
dict: Album information
"""
return self._spotify.albums(albums=albums)