Song Lyrics Extractor in Python

Python course with 57 real-time projects - Learn Python

In this Python project, we will build a GUI-based Lyrics Extractor from Song using the Tkinter, requests and JSON libraries and messagebox module. It is a beginner level project, where you will learn how to extract data from online APIs by converting them into JSON and apply these concepts in a real life project. Let’s get started!😊

About Lyrics Extractor:

A lyric extractor is used to take out the lyrics from a song. In our project, the user will provide the song name and the artist name and our program will then extract the lyrics of the song from a great resource called api.lyrics.ovh, using requests and JSON libraries.

About the project:

The objective of this is to create a GUI based Lyric Extractor. This is a beginner-level project and to build this, you will need basic understanding of Tkinter, requests and JSON libraries.

Project Prerequisites:

To build this project, we will need the following libraries:

1. Tkinter – To create the GUI.

2. Requests – To extract from the website and convert it to JSON format.

3. JSON – To convert JSON format data to readable text.

All of these libraries come pre-installed with Python, so you don’t need to worry about installing them.

Download Python Song Lyrics Extractor Project

Download source code of python song lyrics extractor from the following link: Song Lyrics Extractor Project Source Code

Project File Structure:

Here are the steps you will need to execute to build this project:

1. Importing the necessary libraries and modules
2. Creating the lyrics extraction function
3. Initializing the GUI window, placing components in it and finalizing it

Let’s take a closer look at these steps:

1. Importing the necessary libraries and modules:

# Importing necessary libraries
from tkinter import *
import tkinter.messagebox as mb

import json
import requests

Explanation:

Tkinter.messagebox module is used to display certain boxes as an external window which can display some information, an error, ask a yes or no question, etc.

2. Creating the lyrics extraction function:

# Function to extract lyrics
def extract_lyrics():
    global artist, song
    artist_name = str(artist.get())
    song_name = str(song.get()).lower()
    link = 'https://api.lyrics.ovh/v1/'+artist_name.replace(' ', '%20')+'/'+song_name.replace(' ', '%20')

    req = requests.get(link)
    json_data = json.loads(req.content)

    try:
        lyrics = json_data['lyrics']

        exec("print(lyrics)")

        mb.showinfo('Lyrics printed', f'The lyrics to the song you wanted have been extracted, and have been printed on your command terminal.')
    except:
        mb.showerror('No such song found',
        'We were unable to find such a song in our directory. Please recheck the name of the artist and the song, and if it correct, we apologize because we do not have that song available with us.')

Explanation:

  • In this step, we will create a function that will extract the lyrics from the song name and artist name.
  • First off, we will make the scope of the StringVar() variables containing the name of the song and artist, global. Then we will format those names according to the format to what is acceptable by our API website and generate a link accordingly (which will be stored in the variable ‘link’).
  • Then we will get the data of that website using requests.get(link) and load/convert it to JSON format using json.loads(req).
  • After that, we will run a try-except statement to see if the song is available with our API or not.
  • The ‘lyrics’ variable will store whatever is the value of the key lyrics in the JSON data that we recovered earlier. If the song is found, we will print the lyrics and display an information box that tells us that the lyrics have been printed on the console.
  • The exec() function will be used to print the lyrics because the value of key lyrics contains escape characters as well, and they need to be executed as in an actual print statement.
  • If the song is not found and the lyrics key is unavailable, it will throw an error, in which case we will go to the except statement and the error would be displayed using the mb.showerror() function.

3. Initializing the GUI window, placing components in it, and finalizing it:

# Initializing the window
root = Tk()
root.title("DataFlair Song Lyrics Extractor")
root.geometry("600x200")
root.resizable(0, 0)
root.config(bg='CadetBlue')

# Placing the components
Label(root, text='DataFlair Song Lyrics Extractor', font=("Comic Sans MS", 16, 'bold'), bg='CadetBlue').pack(side=TOP,fill=X)

Label(root, text='Enter the song name: ', font=("Times New Roman", 14), bg='CadetBlue').place(x=20, y=50)
song = StringVar()
Entry(root, width=40, textvariable=song, font=('Times New Roman', 14)).place(x=200, y=50)

Label(root, text='Enter the artist\'s name: ', font=("Times New Roman", 14), bg='CadetBlue').place(x=20, y=100)
artist = StringVar()
Entry(root, width=40, textvariable=artist, font=('Times New Roman', 14)).place(x=200, y=100)

Button(root, text='Extract lyrics', font=("Georgia", 10), width=15, command=extract_lyrics).place(x=220, y=150)

# Finalizing the window
root.update()
root.mainloop()

Explanation:

  • In this step, we will create our entire Python GUI window to extract lyrics from song.
  • By creating a Tk() instance, we create our window. The .title(), .geometry(), .resizable() methods are used to set the title and geometry (in pixels) of the window and decide if the window will be allowed to resize while active or not. The .config() method will be used to change the background color of the window.
  • After that, we will create a head label, packed to the center of the top side, using Label() class objects and the .pack() geometry manager method.
  • In our window, there will be two labels, one below the other, telling the user to enter the song/artist name in the adjacent entry fields (that will be created using Entry() class).
  • At the bottom of the window, there will be a button with the command as the extract_lyrics() function. So, when the button is pressed, the function will be executed.
  • Eventually, we will finalize our window using the root.mainloop() statement which prevents the window from shutting down as soon as it opens.

4. The final code:

# Importing necessary libraries to extract lyrics from song using Python
from tkinter import *
import tkinter.messagebox as mb

import json
import requests

# Functions
def extract_lyrics():
global artist, song
artist_name = str(artist.get())
song_name = str(song.get()).lower()
link = 'https://api.lyrics.ovh/v1/'+artist_name.replace(' ', '%20')+'/'+song_name.replace(' ', '%20')

req = requests.get(link)
json_data = json.loads(req.content)

try:
lyrics = json_data['lyrics']

exec("print(lyrics)")

mb.showinfo('Lyrics printed', f'The lyrics to the song you wanted have been extracted, and have been printed on your command terminal.')
except:
mb.showerror('No such song found',
'We were unable to find such a song in our directory. Please recheck the name of the artist and the song, and if it correct, we apologize because we do not have that song available with us.')


# Initializing the window
root = Tk()
root.title("DataFlair Song Lyrics Extractor")
root.geometry("600x200")
root.resizable(0, 0)
root.config(bg='CadetBlue')

# Placing the components
Label(root, text='DataFlair Song Lyrics Extractor', font=("Comic Sans MS", 16, 'bold'), bg='CadetBlue').pack(side=TOP,fill=X)

Label(root, text='Enter the song name: ', font=("Times New Roman", 14), bg='CadetBlue').place(x=20, y=50)
song = StringVar()
Entry(root, width=40, textvariable=song, font=('Times New Roman', 14)).place(x=200, y=50)

Label(root, text='Enter the artist\'s name: ', font=("Times New Roman", 14), bg='CadetBlue').place(x=20, y=100)
artist = StringVar()
Entry(root, width=40, textvariable=artist, font=('Times New Roman', 14)).place(x=200, y=100)

Button(root, text='Extract lyrics', font=("Georgia", 10), width=15, command=extract_lyrics).place(x=220, y=150)

# Finalizing the window
root.update()
root.mainloop()

Python Song Lyrics Extractor Output

python extract song lyrics output

Summary

Congratulations! You have now created your own GUI to extract Lyrics from song using the only in-built Python libraries where you only have to provide it the name of the song and the artist.

Have fun coding with it!😊

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *