Show Lyrics of your Spotify songs with Python — Part 1

This is my first contribution to Medium and I’m very excited to show you all some things that I’ve been working on.
I’m going to call this “Automate the boring stuff” just like the book Automate the Boring Stuff with Python.
We are going to make a script that can get the current song from Spotify and automatically display the lyric of that song. Sounds awesome right? The best part of this is that it’s easier than you think and it’s a good tutorial for people who are still learning Python.
You can find the entire code on my GitHub repository: https://github.com/StriderKeni/spotify-lyrics
First of all, we’re going to use the Spotipy library. This allows us to obtain the current song from our Spotify account. We need to create an app on this page https://developer.spotify.com/dashboard/applications.
This will create a CLIENT_ID and CLIENT_SECRET_ID, this allows connect your app to Spotify and obtain the current song.
To obtain the token and connect your app to Spotify, we are going to use a method called Authorization Code Flow. Spotipy has two methods to obtain the token and connect the app. If you want to know more about these methods, please take a look at Spotipy’s official documentation: Spotipy Documentation
I recommend you to install Spotipy directly from the source. Even if PyPi claims has the latest version, the source code is different and if you install Spotipy like always (pip install spotipy) there are some methods that you will not be able to use.
pip install git+https://github.com/plamere/spotipy.git@master
Code:
import spotipy
import spotipy.util as util
from config.config import USERNAME, SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URIscope = 'user-read-currently-playing'token = util.prompt_for_user_token(
USERNAME, scope, client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET, redirect_uri=SPOTIPY_REDIRECT_URI)if token:
sp = spotipy.Spotify(auth=token)
current_song = sp.currently_playing()
artist = current_song['item']['artists'][0]['name']
name_song = current_song['item']['name']print(artist, name_song)else:
print("Can't get token for", USERNAME)
Let me explain a little bit what’s going on upstairs.
First, we need to import spotipy and util from spotipy’s library. I recommend you to create another .py archive to put all of your variables like Username, Client ID, Client Secret ID and Redirect URL.
The redirect URL can be whatever you want. In my case, it’s ‘http://127.0.0.1/callback' just make sure to let know Spotify what’s your URL. You can do that in your application that recently created.
In your Spotify’s developer website go to your Application -> Edit Settings -> Redirect URLs

So far so good. Let’s continue with the code.
scope = 'user-read-currently-playing'
Scope, in other words, means what’s our goal with the script. Make sure to put the correct scope in the variable. You can find all scopes available here: Authorization Scopes Spotify
After that, we create a token with all our variables like Client ID, Client Secret ID, Redirect URL, etc.
token = util.prompt_for_user_token(USERNAME, scope, client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET, redirect_uri=SPOTIPY_REDIRECT_URI)
Now, let’s figure out if the token is working with an if statement
if token:
sp = spotipy.Spotify(auth=token)
current_song = sp.currently_playing()
artist = current_song['item']['artists'][0]['name']
name_song = current_song['item']['name']print(artist, name_song)else:
print("Can't get token for", USERNAME)
Basically here we’re creating a new Spotify object called sp. This will allow us to call the method currently_playing()
This method returns a JSON Unicode. Feel free to immerse into this to figure out what else you can find there. For this tutorial, we’re just going to extract the artist name and current song.
Now, let’s give a try to our code.
Remember that you can run your code from Terminal. In my case, the name of the Python file is spotify_test.py
python spotify_test.py

After this, you can see that there’s a ‘u’ before the artist and song print statement. This means that the artist and name variable are Unicode types. Nothing wrong with this, but remember, if you want to apply string methods to the variables you’ll need to cast it first.
This is our first part, In the next one, we’re going to use these variables and apply Web Scraping to display the current lyric of our songs.
We’re going to need some libraries like Request and Beautiful Soap.
Please stay tuned to the next series.
You can find the entire code on my GitHub page (Please stay aware that the current code on my GitHub is with the part 2 implemented so maybe you can be a little bit lost of what’s going on there)
Feel free to text me whatever question you have.