-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyrics.py
29 lines (27 loc) · 1.38 KB
/
lyrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import re
import urllib.request
from bs4 import BeautifulSoup
def get_lyrics(artist,song_title):
artist = artist.lower()
song_title = song_title.lower()
# remove all except alphanumeric characters from artist and song_title
artist = re.sub('[^A-Za-z0-9]+', "", artist)
song_title = re.sub('[^A-Za-z0-9]+', "", song_title)
if artist.startswith("the"): # remove starting 'the' from artist e.g. the who -> who
artist = artist[3:]
url = "http://azlyrics.com/lyrics/"+artist+"/"+song_title+".html"
try:
content = urllib.request.urlopen(url).read()
soup = BeautifulSoup(content, 'html.parser')
lyrics = str(soup)
# lyrics lies between up_partition and down_partition
up_partition = '<!-- Usage of azlyrics.com content by any third-party lyrics provider is prohibited by our licensing agreement. Sorry about that. -->'
down_partition = '<!-- MxM banner -->'
lyrics = lyrics.split(up_partition)[1]
lyrics = lyrics.split(down_partition)[0]
lyrics = lyrics.replace('<br>','').replace('</br>','').replace('</div>','').strip()
if 'http'.find(lyrics) > -1 or 'Http'.find(lyrics) > -1 or 'HTTP'.find(lyrics) > -1:
raise Exception('Exception occurred. Response was: \n{}'.format(lyrics))
return lyrics
except Exception as e:
return "Exception occurred \n" +str(e)