How to create Song lyrics finder application using python

How to create Song lyrics finder application using python

Lyrics finder application using python

Introduction

In general song lyrics finder to used to find song lyrics that are tough to remember and recognize so it will be very helpful for karaoke and sing-along. In this blog, we are going to see how to create a song lyrics finder using python

Requirements

pip install python-docx
pip install beautifulsoup4
pip install requests

Python-Docx is used to create a word document. Beautifulsoup4 is used to read and extract from webpages. Requests are used to make user request in the browser.

How to get lyrics from a website?

import requests
from bs4 import BeautifulSoup

songname="2002"
html_file=requests.get("https://search.azlyrics.com/search.php?q="+songname).text
soup=BeautifulSoup(html_file,"lxml")
match=[x.extract() for x in soup.findAll('td',class_='text-left visitedlyr')]
link=str(list(match[0])[1]).split('"')

html_files=requests.get(link[1]).text
soup=BeautifulSoup(html_files,"lxml")
match=[x.extract() for x in soup.findAll('div',class_='col-xs-12 col-lg-8 text-center')]

lyrics=str(match).split("\n")
start=lyrics.index('<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->\r')
end=lyrics.index('<!-- MxM banner -->')
print(lyrics[start+1:end-2])

for i in lyrics[start+1:end-2]:
    f=i.replace("<br/>","")
    print(f)
songname="2002"
html_file=requests.get("https://search.azlyrics.com/search.php?q="+songname).text
soup=BeautifulSoup(html_file,"lxml")

Then in soup it will have the entire website code we have to extract element of class 'text-left visitedlyr'

match=[x.extract() for x in soup.findAll('td',class_='text-left visitedlyr')]
link=str(list(match[0])[1]).split('"')

then further extraction will give the song lyrics link:- azlyrics.com/lyrics/annemarie/2002.html

Extracting song lyrics

By using the song lyrics link we have to again make a request

html_files=requests.get(link[1]).text
soup=BeautifulSoup(html_files,"lxml")

Then we have to extract

element of class 'col-xs-12 col-lg-8 text-center'

match=[x.extract() for x in soup.findAll('div',class_='col-xs-12 col-lg-8 text-center')]
lyrics=str(match).split("\n")
start=lyrics.index('<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->\r')
end=lyrics.index('<!-- MxM banner -->')

lyrics=lyrics[start+1:end-2]

Then Further extraction will give the desired song lyrics: -

I will always remember The day you kissed my lips Light as a feather And it went just like this No, it's never been better Than the summer of two thousand and two

We were only eleven But acting like grown-ups Like we are in the present Drinking from plastic cups Singing "love is forever and ever" Well, I guess that was true

Combining with GUI

from tkinter import *
from tkinter import messagebox
from docx import Document
import requests
from bs4 import BeautifulSoup
import random



root=Tk()
root.title("Lyrics Finder")
myscroll=Scrollbar(root,orient=VERTICAL)


lables2=[]
numline=0


def new_lyrics():
    global numline
    global lables
    global frame1
    global removebtn
    global count2
    global lables2
    global songbox
    global e1
    rem()
    songname=e1.get().replace(" ","+")
    fgh=[]
    html_file=requests.get("https://search.azlyrics.com/search.php?q="+songname).text
    soup=BeautifulSoup(html_file,"lxml")
    match=[x.extract() for x in soup.findAll('td',class_='text-left visitedlyr')]

    if match==[]:
        messagebox.showinfo("Lyric Finder","song not found ")
        return 
    link=str(list(match[0])[1]).split('"')
    html_files=requests.get(link[1]).text
    soup=BeautifulSoup(html_files,"lxml")
    match=[x.extract() for x in soup.findAll('div',class_='col-xs-12 col-lg-8 text-center')]
    lyrics=str(match).split("\n")
    start=lyrics.index('<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->\r')
    end=lyrics.index('<!-- MxM banner -->')

    for i in lyrics[start+1:end-2]:
        f=i.replace("<br/>","")
        songbox.insert(END,"          "+f)


    lables2=lyrics[start+1:end-2]
    numline=len(lables2)

    removebtn.grid(row=0,column=3,padx=2)
    save.grid(row=0,column=4,padx=2)


def sa():
    global lables2
    global numline
    global lynum
    global e1
    lynum=random.randint(0, 100)
    na=e1.get()
    name=na+"("+str(lynum)+").docx"
    doc =Document()
    doc.add_heading(na)

    with open(name,"a") as f:
        for i in range(numline):
            try:
                encoded=lables2[i].encode('utf-8')
                doc.add_paragraph(lables2[i].replace("<br/>",""))
            except:
                pass
    doc.save(name)
    messagebox.showinfo("lyric finder","song lyrics saved as "+name)


def rem():
    global numline
    global lables
    global removebtn
    global lables2
    global songbox
    global e1
    lables2=[]
    songbox.delete(0,numline-1)

    removebtn.grid_forget()
    save.grid_forget()



frame1=Frame(root)
labe=Label(frame1,text="Enter the song name:")
labe.grid(row=0,column=0)
e1=Entry(frame1,width=30)
e1.grid(row=0,column=1,padx=5)


sea=Button(frame1,text="Search",command=new_lyrics)
sea.grid(row=0,column=2,padx=2)


frame2=Frame(root)
myscroll2=Scrollbar(frame2,orient=HORIZONTAL)
myscroll=Scrollbar(frame2,orient=VERTICAL)
songbox=Listbox(frame2,width=70,height=30,borderwidth=0,bg="#fce303",fg="black",yscrollcommand=myscroll.set,font=("arial",13),xscrollcommand=myscroll2.set)
myscroll.config(command=songbox.yview)
myscroll2.config(command=songbox.xview)
myscroll.pack(side=RIGHT,fill=Y)
myscroll2.pack(side=BOTTOM,fill=X)
songbox.pack()

frame2.grid(row=1,column=0,pady=10)

removebtn=Button(frame1,text="Clear",command=rem)
save=Button(frame1,text="Save",command=sa)
frame1.grid(row=0,column=0)
root.mainloop()

Functions

rem()

This will clear the listbox and make it empty

sa()

This function will save the lyrics in ".docx" format and the show lyrics successfully saved as xxxx.docx.

Screenshot (229).png

Screenshot (231).png

new_lyrics()

This function will first empty the listbox using the rem() function then it will extract song lyrics from the web and display them in the list box

Screenshot (233).png

Github link:- click here