-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
41 lines (27 loc) · 1.06 KB
/
data.py
File metadata and controls
41 lines (27 loc) · 1.06 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
from auth import get_auth
import random
import requests
def get_data(artist_id):
access_token = get_auth()
headers = {
'Authorization': 'Bearer {TOKEN}'.format(TOKEN=access_token)
}
URL = 'https://api.spotify.com/v1/artists/{id}/top-tracks'.format(id=artist_id)
data = requests.get(URL + "?market=US", headers = headers)
data = data.json()
#print(data)
#print(len(data['tracks']))
#rand = random.randint(0, len(data['tracks']) - 1)
rand = random.randint(0, len(data['tracks']) - 1)
artist_names = []
for i in data["tracks"][rand]["artists"]:
artist_names.append(i["name"])
song_name = data["tracks"][rand]["name"]
album_image = data["tracks"][rand]["album"]["images"][0]["url"] # Could also be null
song_preview = data["tracks"][rand]["preview_url"] # Can be Null
spotify_link = data["tracks"][rand]["external_urls"]["spotify"]
info = [song_name, artist_names, album_image, song_preview, spotify_link]
return info
if __name__ == '__main__':
x = get_data()
print(x)