-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpotifyUser.py
More file actions
44 lines (40 loc) · 1.67 KB
/
SpotifyUser.py
File metadata and controls
44 lines (40 loc) · 1.67 KB
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
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
class SpotifyUser:
"""Initialize a SpotifyUser object"""
def __init__(self) -> None:
pass
"""Function to authenticate a user with the Spotify API using OAuth 2.0
:param: .env file containing client environment variables
:returns: an authenticated client object
"""
def authenticate(self):
#load in environment vairables for API authentication
load_dotenv()
# Shows a user's playlists
scope = 'playlist-read-private playlist-modify-private ugc-image-upload playlist-modify-public'
client = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
print("Spotify authenticated")
self.sp = client
return client
"""Function to get all the playlists in a user's librar
:returns: a dictionary with the playlist name as the key and the spotify playlist id as the value
"""
def get_playlists(self):
#get all user playlists
playlist_data = self.sp.current_user_playlists(limit=50)['items']
#loop through every playlist
playlists = {}
for playlist in playlist_data:
name = playlist['name']
playlist_id = playlist['uri']
playlists[name] = playlist_id
return playlists
"""Function that sets a user's playlist cover image
:param playlist_id: the spotify playlist id
:param base64_str: the cover image encoded in a Base64 string
"""
def set_playlist_cover_img(self,playlist_id,base64_str):
self.sp.playlist_upload_cover_image(playlist_id,base64_str)
print('Set playlist cover image')