Automate Spotify with Python

Blissful Time
24 followers
21 Video Views·Oct 24, 2022
Hey there! So today we're going to be doing a really cool beginner-friendly Python project.
Has this ever happened to anyone? You're on YouTube and you find a song that you really,
really like, but then to add it to Spotify, you have to go into Spotify, log in,
like create a new playlist. Basically, it's a whole thing to add that new song to Spotify. So
we're going to solve that little inconvenience using Python. Before we even write a single line
of code, we need to create a plan so we know how to solve this problem. So the problem we're trying
to solve is that we want all of our liked videos on YouTube to be automatically generated into a
Spotify playlist. So the way we would do this manually, like we want to think high-level first,
the way we would do this manually is by first we would make sure that we're logged into YouTube,
we would go to our liked folders, we would open Spotify, then we would create a new playlist,
then we would search for the liked song, and then finally we would add the song into our playlist.
By thinking about these manual steps, this will kind of help us understand what we need to program.
What we want to do next is we want to map these steps out into Python functions. So once we've
written the scaffold and we've written the type of functions we would need to get this to work,
the next step is to research and look at what's available online that can make these steps a
reality. Don't worry guys, I've already done this research for you. The three APIs that you're going
to need to use are the YouTube data API, the Spotify web API, and finally the YouTube DL library.
Now we're ready to start to code. Let's start first with our create playlist function.
For this function we'll be using the Spotify web API. Before we start guys, make sure to get your
user ID and OAuth token from the Spotify web API and add them to a secrets file. So if you look at
the Spotify web API, we're really lucky and they have very good documentation that gives you a lot
of useful information about what we can use to figure out how to create a playlist using Spotify.
So we're also going to need the Python request library. This will allow us to make HTTP requests
using Python. If you look at the web API docs, you see that we are given a lot of very important
information. We're given the endpoint, we're given what the request should look like, we're given what
type of HTTP method that we have. Let's go ahead and write this function out. We'll start out by
filling out the request body based on what we see in the Spotify web API documentation. Then we'll
go ahead and specify what the query, what the endpoint is, and we're going to be calling it
query in this case. So let's go ahead and send out this request and once we send it out we want to
make sure that we save the playlist ID. In the future we'll need this playlist ID to help us add
specific songs to the playlist. So now let's move on to the second function. This function will allow
us to search for a song using the Spotify web API. Again we'll go ahead and look at the docs and we
will see in the docs it kind of tells us how to search for a song which they call track using a
song and artist name. Again we'll go ahead and format the query string to take in the song and
the artist and we'll go ahead and send this information using the request library and making
sure once we send this information to collect the URI because we'll need this URI so that the
playlist knows what specific songs to add. Then this next function is actually very easy to fill
out. We'll basically just be copying and pasting from the YouTube data API documentations. This
will allow us to get a YouTube client that we can later use but just make sure if you're going to
copy paste you also add in the packages that you need for this function to work. So now let's move
on to getting the liked videos. This is a more involved function. So for the liked videos again
we'll be using the YouTube data API. We're really lucky because the YouTube data API has the code
that we need to get our liked videos. With this information we can loop through and collect each
video item and then store each item in our all songs information dict. Even though the YouTube
data API is really great there are some limitations in the API. Like for example it's really hard to
parse out the song name and artist name. This is where YouTube DL library comes in. You'll be able
to provide that library with a YouTube URL and then from that URL it's going to give us
the song name and the artist. So then the final step is to make sure we add in the songs URI.
So remember guys we already wrote this function out so we're just going to use that function
call that function to help us collect the URI. Now the final function is us kind of putting it together.
Remember all the steps we talked about in the beginning? This function sort of allows us to run
all those steps so that we can programmatically move our songs into our playlist. So the first
thing we need to do is we need to populate our dictionary. So let's go ahead and grab all the URIs
and then we can put them all in a list. The last step is to check out the Spotify web API
and from there you'll see how we can add a song. Again we'll be doing basically what we've done
this entire time which is use the request library to help us send a request. So for this specific
HTTP request we're going to go ahead and and send over all of our URIs to the specified endpoint.
And now we're basically done. Let's go ahead and test it out. Awesome guys we were able to
get the project to work. So if you like this video make sure to give this video a thumbs up
and hit the subscribe button to see more videos like this. I'll see you guys next time.
