Created
September 2, 2012 08:33
-
-
Save blha303/3595906 to your computer and use it in GitHub Desktop.
Lyrics getter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment