pallavr commited on
Commit
e393a54
·
1 Parent(s): 34afc9e

Upload app.py

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