-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
56 lines (46 loc) · 1.6 KB
/
test_api.py
File metadata and controls
56 lines (46 loc) · 1.6 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
45
46
47
48
49
50
51
52
53
54
55
56
import requests
import random
import pandas as pd
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util
import os
from dotenv import load_dotenv
load_dotenv()
music_list = []
username = os.environ.get("USERNAME")
scope = os.environ.get("SCOPE")
client_id = os.environ.get("CLIENT_ID")
client_secret = os.environ.get("CLIENT_SECRET")
redirect_uri = os.environ.get("REDIRECT_URI")
token = util.prompt_for_user_token(username,
scope=scope,
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri
)
endpoint_url = "https://api.spotify.com/v1/recommendations?"
# OUR FILTERS
limit = 5
market = "US"
seed_genres = 'soul'
query = f'{endpoint_url}limit={limit}&market={market}&seed_genres={seed_genres}'
bearer = "Bearer " + token
response = requests.get(query,
headers={"Content-Type": "application/json",
"Authorization": bearer})
json_response = response.json()
artists = []
songs = []
external_urls = []
for i in json_response['tracks']:
# print(i['artists'][0]['external_urls']['spotify'])
artists.append(i['artists'][0]['name'])
songs.append(i['name'])
external_urls.append(i['artists'][0]['external_urls']['spotify'])
for music in zip(artists, songs, external_urls):
music_list.append(music)
chosen = random.choice(music_list)
# print(chosen)
music_list = [chosen[0] + ' by ' + chosen[1]]
music_url = chosen[2]
print(music_url)