Site icon DataFlair

Song Lyrics Extractor in Python

python project song lyrics extractor

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:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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:

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:

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

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!😊

Exit mobile version