In this project, we'll deploy a simple “music discovery” web app that shows song(s) from your favorite artist(s) and link(s) to the music and lyrics.
This data will be dynamically generated using third-party APIs from Spotify and Genius.
https://floating-island-45726.herokuapp.com/
- On
https://github.com/new
, create a new personal repository. Name it whatever you want. - In terminal, in your home directory, clone the repo:
https://github.com/NJIT-CS490-SP21/project1-as3627.git
. cd
into the repository that is created and you should see all the files.- Then, connect this cloned repo to your new personal repo made in Step 1:
git remote set-url origin https://www.github.com/{your-username}/{repo-name}
(be sure to change your username and repo-name and remove the curly braces) - Run
git push origin main
to push the local repo to remote. You should now see this same code in your personal repo.
- You can sign up for a free Spotify Developer account on their website here: https://developer.spotify.com/.
- Once you make an account, head on over to the Dashboard and hit
Create an App
. Fill out the information on there. - After you finish filling in the information for creating the app, make note of the
Client ID
andClient Secret
, we'll need them later. - Spotify is used to gather song data.
- You can sign up for a free Genius account on this link here: https://genius.com/signup_or_login.
- Once you make an account, head over to https://genius.com/api-clients and hit
New API Client
and fill in the information. - Once you fill in the information hit
Generate Access Token
and make note of this Access Token, we'll need it later. - Genius is used to gather song lyrics.
- You can sign up for a free Heroku account on their website here: https://signup.heroku.com/login.
- Heroku is used to host the web app.
Note: if for some reason these commands don't work, put sudo
in front of each command and then try running it.
pip install python-dotenv
pip install requests
pip install Flask
npm install -g heroku
(Note this one might take a while to install, don't worry)
- Create a
.env
file in your project directory. - Add the Spotify
Client ID
andClient Secret
variables inside the.env
file, with the lines:export CLIENT_ID = '{YOUR_ID}'
andexport CLIENT_SECRET = '{YOUR_SECRET}'
. - Add the Genius
Access Token
variable in the.env
file, with the lines:export GENIUS_KEY = '{YOUR KEY}'
. - Inside
app.py
add your favorite artists' Artist IDs inside theids
list beginning on line 18. To find out how to get Artist IDs, check this link out: https://support.tunecore.com/hc/en-us/articles/360040325651-How-to-Find-my-Spotify-Artist-ID
- Run command in terminal with
python app.py
- Preview the web page in browser
'/'
- If you don't have this file already, create
Procfile
and writeweb: python app.py
on the first line. This is used to deploy the app on Heroku. - If you don't have this file already, create
requirements.txt
and writeFlask
,requests
, andpython-dotenv
. Make sure each is separated with a newline. If you use more packages, make sure to include them here. - Make sure to have all your files commited with git by this point.
- Login to your Heroku account with
heroku login -i
. - Create a Heroku app with the command:
heroku create
. This will create a new URL and associated host for you. - Push your code to Heroku with the command:
git push heroku main
. This pushes your code to Heroku's remote repository. - Once that's done, visit https://dashboard.heroku.com/apps. Click on the app that you made, and then hit settings. Inside settings, click on
Reveal Config Vars
. - Add all three variables that you had in the
.env
file:CLIENT_ID
,CLIENT_SECRET
, andGENIUS_KEY
and the associated key. (Note you don't need to include quotation marks with the keys) - Run
heroku open
or refresh the URL if you have it open, and the web app should be running.
- Initially with the Spotify API, there were some songs that did not have any preview. In these cases I would still be attempting to put something into the audio player, which would be giving me errors. I actually thought it was an audio player error in the beginning,
which made me go to w3schools.com/html/html5_audio.asp to check how audio players worked again. After seeing I didn't make any mistakes with the audio player, I went
back to the
json
in the python file. After running the code a few times, I found that for these songs, hadNone
for their preivew url. I realized I was passing the value ofNone
instead of a URL, so I modified my HTML with Flask python tags{% if song_preview == None %}
. Now in my code, I made it so that the audio player only comes up if a song preview link was provided. - Some songs had multiple artists. Initially I was pulling only the first artist of the song, since I didn't really know how to get the rest. After going through the lecture 4 demo again,
I decided to change how the code worked. Since for that demo, we passed a list and the length of the list, I decided to do the same for the artist names. Before I could do anything, I went back to the Spotify api.
I had to look back at the
json
and see where the artist names were being stored again. After I found that, I made a loop in the spotify file, and then appended every artist name into a list. After that I also took the length of the list and returned both the list and length with the rest of the variables. - When I used the Genius API, there were some cases where I could not return a lyrics page. For some reason, it would be throwing an error for me and not returning
None
, as the case was for the Spotify API when there wasn't a song preview. I thought I was messing up with my usage of the API, so I decided to look up a tutorial on how to use the Genius API. I actually found a link to help: https://dev.to/willamesoares/how-to-integrate-spotify-and-genius-api-to-easily-crawl-song-lyrics-with-python-4o62 which actually goes over a similar project. In this project's case, I followed along with the code structure and I noticed that they initially set the link to beNone
, and if there was a lyric page found, they would set the varible to the link. In the cases where no link was found, it would automatically returnNone
. Which ended up solving my issue. This site also helped me clean up my code for API usage.
- If the api crashes, or hit its limit for the day, the app will most likely return an error message or crash. This issue has not occured (yet), but is possible. If this occurs, I advise waiting some time before using the app again. I have not been able to fix this potential issue yet.
- If I had more time to work on it, I would like to add a search bar so the user can search for an artist themselves. Similar to how we did HW8, I would be using Javascript with
@app.route("/search/<user_text>")
in Flask. I would most likely use an html button along with a text box to send the arguments to the back end. I would then create a new method under the search route, and make use of the Spotify Search API for searching the inputted artist's URI, and then using the top-tracks API to pick a random song. The only issue is that I had a hard time working with the JS part, especially with parsing thejson
and changing the html. It would take me a long time to add this feature, and givin the due date is soon, I decided against adding it. If given more time I would have implemented it.
- I got tired of having to hit
CTRL + R
every time to refresh the page, so I created a refresh button on the page itself. I looked up how to make a refresh button in html and found this site: https://www.htmlgoodies.com/tutorials/getting_started/article.php/3479551/reloading-the-page.htm that taught me how to do it. Clicking on the 🔄 emoji will refresh the page. - I added a
Play on Spotify!
feature as well. With the API'sjson
it also returned a link for the song itself on Spotify. I thought it would be a good idea to include this link so the user can play the song right away on their Spotify.