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