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