sjw commited on
Commit
c002392
·
1 Parent(s): 56d232d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import base64
4
+ from requests import post, get
5
+ import json
6
+
7
+ # set up
8
+ client_id = os.environ["CLIENT_ID"]
9
+ client_secret = os.environ["CLIENT_SECRET"]
10
+
11
+ # load_dotenv()
12
+ # client_id = os.getenv("CLIENT_ID")
13
+ # client_secret = os.getenv("CLIENT_SECRET")
14
+
15
+ def get_token():
16
+ auth_string = client_id + ":" + client_secret
17
+ auth_bytes = auth_string.encode("utf-8")
18
+ auth_base64 = str(base64.b64encode(auth_bytes), "utf-8")
19
+
20
+ url = "https://accounts.spotify.com/api/token"
21
+ headers = {
22
+ "Authorization": "Basic " + auth_base64,
23
+ "Content-Type": "application/x-www-form-urlencoded"
24
+ }
25
+ data = {"grant_type": "client_credentials"}
26
+ result = post(url, headers=headers, data=data)
27
+ json_result = json.loads(result.content)
28
+ token = json_result['access_token']
29
+ return token
30
+
31
+ def get_auth_header(token):
32
+ return {"Authorization": "Bearer " + token}
33
+
34
+
35
+ def search_for_song(token, song, artist):
36
+ url = "https://api.spotify.com/v1/search"
37
+ headers = get_auth_header(token)
38
+ query = f"?q=artist:{artist} track:{song}&type=track&limit=1"
39
+
40
+ query_url = url + query
41
+ result = get(query_url, headers = headers)
42
+ json_result = json.loads(result.content)["tracks"]["items"]
43
+ if len(json_result) == 0:
44
+ print("No result...")
45
+ return None
46
+ return json_result[0]
47
+
48
+ def get_song_characteristics(token, track_id):
49
+ url = f"https://api.spotify.com/v1/audio-features/{track_id}"
50
+ headers = get_auth_header(token)
51
+ result = get(url=url, headers=headers)
52
+ json_result = json.loads(result.content)
53
+ return json_result
54
+
55
+
56
+ st.title(':blue[Spotify Song Characteristics]')
57
+
58
+ if 'token' not in st.session_state: st.session_state['token'] = get_token()
59
+ if 'song_id' not in st.session_state: st.session_state['song_id'] = []
60
+
61
+ input_container = st.container()
62
+ with input_container:
63
+ with st.form(key = 'my_form', clear_on_submit = True):
64
+ song_name = st.text_input("Enter song name", key = "song_name")
65
+ artist_name = st.text_input("Enter artist name", key = "artist_name")
66
+ submit_button = st.form_submit_button(label = 'search')
67
+
68
+ if submit_button and song_name and artist_name:
69
+ song_search = search_for_song(token = st.session_state['token'],
70
+ song = song_name,
71
+ artist = artist_name)
72
+ if song_search is not None:
73
+ song_id = song_search["id"]
74
+ st.session_state['song_id'].append(song_id)
75
+ st.markdown(f"Retreived song ID is {song_id}")
76
+ st.markdown(f"Retreived song name is {song_search['name']}")
77
+ song_characteristics = get_song_characteristics(token = st.session_state['token'],
78
+ track_id = song_id)
79
+
80
+ characteristics = ""
81
+ for i, item in enumerate(song_characteristics):
82
+ characteristics += f"{i + 1}. {item} : {song_characteristics[item]}\n"
83
+ st.markdown(f"Here are the song characteristics:\n{characteristics}")
84
+ uri = song_characteristics['uri'].split(":")[2]
85
+ st.markdown(f"Review song at https://open.spotify.com/track/{uri}")
86
+ else:
87
+ st.markdown(f"API did not find a result.")