Automate YouTube Music to Spotify with Python
Sometimes we listen to a song on YouTube music, and we really found that song interesting, and you want to add that to your Spotify playlist. Then you manually search that song, add it to the Spotify playlist. But we can automate that process of searching YouTube songs and add them into Spotify playlist using python programming. In this article, we are going to learn how we can automate the process of adding YouTube music into a Spotify playlist using Python.
- Python 3 or newer should be installed on the system.
- Youtube Data API credentials
- Spotify API credentials
- Youtube dl for extracting track name and artist name
- Firstly, we will search and list the songs in the user’s YouTube playlist using this YouTube API.
- After saving the song information i.e. song name and artist name, we will search this information on Spotify using this Spotify API and save the data into a list.
- In the third step, we will be creating a playlist in the user’s account using this Spotify API.
- In the fourth and the last step, we will be traversing over the song list that we have created in step 2 and adding those songs into the playlist created in step 3 using the Spotify API.
Note: We will be needing a Google account and a Spotify account for generating credentials to automate music from YouTube to Spotify.
Here is the YouTube playlist that we are going to sync into the Spotify music:
Step1: Generating credentials for Spotify API and YouTube API. Go through the following links and follow the instructions.
Generating Spotify credentials and save your user ID for future use, go to this link, you may be prompted to log in with your Spotify account details and log in. And your username is your user ID. Save this user ID on your desktop.
Go to this link to generate token for authentication and click on the generate token and save this token somewhere on you’re desktop:
Similarly, we have to generate a YouTube OAuth token also, simply follow these instructions to generate the token.
Step 2: List all the songs from the playlist that the user wants to sync with the Spotify playlist. For that, we need to have the playlist ID of that particular playlist that we are going to sync with the Spotify playlist.
Here, the highlighted one is the playlist ID. Now, we just have to list all the songs that we have in this playlist using this You search API.
Step 3: After searching songs, we are going to append the title of these songs to a list, and then we are going to create a playlist on Spotify using Spotify API.
Saving song names and the artist names into a list using the information collected from step 1:
In the above code, we have created a function that will create a list of track names and their artists. It is taking the output from the step 1 code’s output. Then we are simply using the youtube-dl library to fetch the artist name and the title of the songs, then using a for loop to append track name and artist name separated by comma to a new list called info.
Creating a new Spotify playlist:
In the above piece of code, we are firstly creating a simple JSON object having a playlist title description and visibility of the playlist. Then we are using Spotify playlist API to send the newly creating JSON object that has all the details of the new playlist and then returning the playlist ID of the newly generated playlist.
Step 4: After creating a new playlist on Spotify, we need to search these songs and save their Spotify URL. Let’s see how we can do that.
Generating URL for Spotify songs:
Here, in the above function, we are searching for the song on Spotify using the song name and artist name. Using Spotify API we are passing track name and artist name as a parameter and then we are saving the URL of that Spotify song. We are taking the URL of the first song that comes up in our search.
Adding a song to the Spotify playlist:
In the above function, are simply adding the song that we have searched to the new playlist. We are taking the playlist ID and the URLs that we have extracted using the song name and artist name as a parameter and then we dump them into a JSON object because we are going to do a post request, then simply using the Spotify add items to the playlist API.
Below is the complete implementation:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course