
In this video: We'll discuss a Python Beginner Project using Spotify REST Api. Basically, we'll use LastFM API to
Get the current top songs
Connect to Spotify
Get the Unique Identifier for each song
Create A Playlist in Spotify
Add the top songs from LastFM into the new playlist
View the Songs in the Playlist.
We will build out a skeleton for the project. It is very important to have all the functions and classes you're going to use, ready and filled out. It gives us a vision of where we want to go and also more clarity on what we are trying to do.
Once this is complete, our task would be to fill these with their appropriate code.
In the last video we created the skeleton of our project with basic functions and a class that we are going to use. A class is a feature available in object oriented programming languages that helps you use your most common variables easily across all the functions.
Sadly, I do not see a lot of teachers who python beginner projects or beginner tutorials explain how classes make it easier and what the 'self' variable in a class does.
Every python beginner should be using classes if his Python beginner project consists of multiple functions. In this Spotify REST API project we use multiple functions to create to Last FM, Connect to Spotify and do various things so it is very useful to use a class and the 'self' variable in the functions.
So, this video is going help you understanding self and to explain why we use a 'self' variable for all the functions in the class and why it makes everything easier.
It's time to connect to Last FM. We will go step by step on how to sign up to Last FM and get your own API key. Once we have the key, we will make our first REST call to 'GET' the top songs from Last FM.
Last FM has a very easy setup for a new account and to get a new API key to make REST Calls. It's completely free and I'll recommend everyone to sign up and try this out since it's very useful to have a real life experience working with REST API in python.
We will start making REST API calls to Last FM using Python. We have the functions set up, Last fm account with our unique api key ready and it's time to put it to use. This video will also go through the 'Requests' module in python, the most common module that is used to make REST calls in python. We will use requests to make a "GET" call and then go over the type of response we should get and how to deal with them using JSON. A pro tip in this video is the PPrint Module.
This module has made life so much more easier for me, it's insane. It organizes the JSON and makes it easier to read and interpret. We will also see how to actually iterate over JSON or a dictionary in python. All in all, we cover a lot of things in this video so make sure you don't skip any steps.
Its time to make use of the songs Last FM generously gave us when we used REST API to connect to them. We will go about setting up a token in Spotify and how to use Spotify REST Api to make calls and get details from Spotify. We will pass each and every song we got from Last FM to Spotify and in return to the REST Api call we will get a JSON that will have tons of information.
We will again use JSON and dictionary iteration to go over the output, obviously using one of my favorite modules ever - Pretty Print, to interpret the output and pull out the information that we need.
When you make a REST API in Python to Spotify, spotify loves it and so it gives a LOT of data back to you. Specifically, it gives you back EVERYTHING it has on that requested call. So when you make a REST API call to get details of a song, passing the song name and the artist, it will give you all the details of each song matching that artist name and all the other versions of the same song.
In this video we will decode the response and understand how to get only the relevant information we need and modify our code accordingly to parse the non-sense out and focus on the positives :)
So we connected to Last FM, got the top trending songs, sent it using the Requests module to make REST API calls to Spotify and spent entirely 9 minutes to decode the lovely response Spotify gave us using PPrint.
Now we have the information we need, song URI. Why do we need this? To put it in our playlist. So the first step in this video would be to create a playlist. This requires a POST REST API call to spotify because we are changing the data.
REST API calls made with POST need to have data in the body of the call because if we are changing the data in our account, we need to also give Spotify the data we are changing. So we will see how to create the data in the request body and create our empty playlist in spotify.
We have a new empty playlist and its time to make use of it. Let's fill it up with another POST REST API call to add songs in it. In video 7 we decoded the JSON from Spotify and got the song name and the unique identifier. That is going to be put into use here. This video will focus on making another REST API call to Spotify to do a POST to add songs.
But we know for POST we need to send some data in there. That data would be a python list of URIs of all the songs that we need to get into our playlist. We will explore how to do that in this video.
We added the songs to the playlist, but how do we know if it actually did that without opening spotify and navigating to playlist and opening it to check. Well, we can do that from our code itself thanks to the REST API again.
We make a GET Call passing in our new playlist id and what Spotify would do is give us all the data again which we don't really need. We want to list the songs in the playlist and so we'll decode the output and understand the JSON to pull the necessary information.
Well so we put in all the hardwork and now its time to actually put everything together, add some pretty print statements to tell the user what is going on and then executing it. So this video only adds some of the good informational stuff so when anyone runs this they will know what the code is doing exactly.
I think this is actually commendable that you have completed all this and have done more than what any normal college going student would do using Python during their college times.
You not only used REST API to connect to spotify, but also modified your account to create a totally new playlist via API and also add songs inside it and list it out.
If you have any kind of feedback, good or bad, please feel free to let me know. I'll be putting more and more tutorials, trying to help you all. So see you guys in our next project/tutorials.
Lets make our app better. Its time to create a Website so users can easily access your application. In this video, I will go over what we will be doing next with Python Flask and why we will use Flask instead of any other frameworks.
This video will show you the advantages of getting Pycharm Professional and why it is worth the tiny investment you'll be making.
Now that you have Pycharm Professional. It's time to set it up.
Your first Flask program :)
We just created our Hello World Project in Flask. Let's move forward and start with our Home page.
A home page should be pretty and stylish. Flask has a different way of dealing with links in HTML, so we will go over that
Got variables? Lets pass it to our webpage and let Flask do your hardwork, process it and display it for the users.
Let's get the top songs from LastFm and show it on our website.
Lets convert our songs list into a table so its more user friendly.
Anytime we are asking user to input something on a website, we are basically asking for a form. This video will go over the form element in HTML.
Now that we know about forms, lets use one in our website. Give an option to the user to input the playlist name and description he wants in Spotify
Lets create playlist in Spotify using the Name and Description user put in your Flask Website.
We are almost done. We can now create a brand new playlist in spotify from our website. Lets give the user an option to now put the top songs into that playlist we just created. For this, we will need to convert top songs into a form element.
We cannot add songs in spotify without its URI. So in this video, we will get the URIs and add those into our newly created website.
Let's view the top songs we just added into playlist.
Well, we need a menu so users can easily navigate, so lets do a quick menu to make our website more user friendly.
What's Up Everyone!
I have been seeing a lot of users complaining they don't have a real life project to practice this skills.
So I made this quick 90 minutes project where you can put your python skills to use by creating a connection between two big real life applications.
We will use get the top trending songs with Last FM and then Create a Playlist in spotify and add it to the new playlist.
Now, I know this seems like a lot of lines of code but trust me it's only like 70 lines out of which most are semantics needed for declaration.
So what will you learn here? Well let me break it down.
You'll Learn how to use REST API with Python
How to connect to different applications using REST and Python
How to decode and read JSON responses and parse through them
How to use Spotify's REST API
How to modify data in your REAL WORLD APPLICATION
And last but not the lease you will end up with a very cool project to show on your resume.
Not only this, but I am working to add more functionalities and making it even more interactive and fun.
This is only the beginning, we can do a lot of stuff with this BUT before that you need to get this going.
Take out 90 minutes from your schedule. Turn off your mobile. Sit and complete this and then go celebrate.
Requirements:
Some basic level of python is necessary. If you are very new, you can check my profile, I have a course specifically geared towards people who have never programmed. You can take this once you learn from there.
But if you have a little knowledge of python and know what a dictionary and list is, you should be good here :)
If you have any questions, post it in the discussions and I'll be glad to help you out :)