Last active 1444397284

Lyrics getter

Revision 37214b7db84902bf648be0e78b50d86246ef7cb0

lyrics.py Raw
1import urllib,re,sys;
2from BeautifulSoup import BeautifulSoup
3def error():
4 print "Song lookup failed."
5 print "It is possible that:"
6 print "Song name not in correct format: artistname/songname"
7 print "Example: amateurtransplants/londonunderground"
8 sys.exit()
9inp = str(sys.argv[1])
10a = urllib.urlopen("http://www.azlyrics.com/lyrics/" + inp + ".html").read()
11soup = BeautifulSoup(a)
12b = soup.findAll(style="margin-left:10px;margin-right:10px;")
13if str(b) == "[]":
14 error()
15try:
16 artist = re.search(r'ArtistName = "(.*?)"', a).group(1)
17except AttributeError:
18 error()
19title = re.search(r'SongName = "(.*?)"', a).group(1)
20c = str(b).replace("[<div", "<div")
21c = c.replace("</div>]", "</div>")
22file = open(artist + " - " + title + ".html", "w")
23file.write(c)
24file.close()
25print "Success. Lyrics written to " + file.name
26