-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspotinfo.py
38 lines (28 loc) · 1 KB
/
spotinfo.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
import requests
CLIENT_ID = None
CLIENT_SECRET = None
try:
from config import CLIENT_ID, CLIENT_SECRET
except:
print("Error: Set up your Spotify developer account.")
BASE_URL = 'https://api.spotify.com/v1/'
AUTH_URL = 'https://accounts.spotify.com/api/token'
auth_response = requests.post(AUTH_URL, {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
})
auth_response_data = auth_response.json()
access_token = auth_response_data['access_token']
headers = {
'Authorization': 'Bearer {token}'.format(token=access_token)
}
def get_playlist_items(playlist_id: str, limit=50) -> list:
r = requests.get(BASE_URL + 'playlists/' + playlist_id + '/tracks',
headers=headers,
params={'include_groups': 'track', 'limit': limit})
d = r.json()
tracks = list()
for track in d['items']:
tracks.append(track['track']['name'] + ' by ' + track["track"]["album"]["artists"][0]["name"])
return tracks