-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
53 lines (40 loc) · 1.43 KB
/
script.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
import requests
import json
#Client ID and SECRET - USE YOUR OWN!
CLIENT_ID = 'Client ID Go Here'
CLIENT_SECRET = 'Client Secret Go Here'
AUTH_URL = 'https://accounts.spotify.com/api/token'
# POST
auth_response = requests.post(AUTH_URL, {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
})
# convert the response to JSON
auth_response_data = auth_response.json()
# save the access token
access_token = auth_response_data['access_token']
#creating the headers
headers = {
'Authorization': 'Bearer {token}'.format(token=access_token)
}
# base URL of all Spotify API endpoints
BASE_URL = 'https://api.spotify.com/v1/'
#Jay Z ARTIST ID
artist_id = '3nFkdlSjzX9mRTtwJOzDYB'
# actual GET request with proper header
r = requests.get(BASE_URL + 'artists/' + artist_id + '/albums', headers=headers)
data = r.json()
formatted_json = json.dumps(data, sort_keys=True, indent=10)
# print(formatted_json)
print('Album Name:', data['items'][0]['name'])
print('Release Date:', data['items'][0]['release_date'])
print('Track #:', data['items'][0]['total_tracks'])
print('Type:', data['items'][0]['type'])
print('Albulm URI:', data['items'][0]['uri'])
print()
print('Album Name:', data['items'][2]['name'])
print('Release Date:', data['items'][2]['release_date'])
print('Track #:', data['items'][2]['total_tracks'])
print('Type:', data['items'][2]['type'])
print('Albulm URI:', data['items'][2]['uri'])