Lyrics Downloader

Here is a Python script I just wrote to download lyrics from lyrics.wikia.com

To run this script, you only need Python 2.7+ and install the BeautifulSoup module (if you have not) by executing this command:

sudo pip install beautifulsoup4


1:  import codecs  
2:  import json  
3:  import sys  
4:  import bs4  
5:  from bs4 import BeautifulSoup  
6:  import urllib, urllib2  
7:  ArtistInput = raw_input("Artist: ")  
8:  SongInput = raw_input("Song: ")  
9:  def Extraction(webpage):  
10:       soup = bs4.BeautifulSoup(webpage)  
11:       result = []  
12:       for tag in soup.find('div', 'lyricbox'):  
13:            if isinstance(tag, bs4.NavigableString):  
14:                 if not isinstance(tag, bs4.element.Comment):  
15:                      result.append(tag)  
16:            elif tag.name == 'br':  
17:                 result.append('\n')  
18:       return "".join(result)  
19:  Query = urllib.urlencode(dict(artist=ArtistInput, song=SongInput, fmt="realjson"))  
20:  Response = urllib2.urlopen("http://lyrics.wikia.com/api.php?" + Query)  
21:  Output = json.load(Response)  
22:  if (Output['lyrics'] != 'Not found'):  
23:       # print sneak peek  
24:       #print(data['lyrics'])  
25:       Lyrics = Extraction(urllib2.urlopen(Output['url']))  
26:       print Lyrics  
27:       IsSaveToFile = raw_input("Save to File?(y/n)")  
28:       if (IsSaveToFile == 'y'):  
29:            OutputPath = raw_input("Export to: ")  
30:            OutputPath = "%s//%s - %s.txt" % (OutputPath, Output['artist'], Output['song'])  
31:            with codecs.open(OutputPath, 'w', encoding='utf-8') as output_file:  
32:                 output_file.write(Lyrics)  
33:            print("Finished writing '%s'" % OutputPath)  
34:  else:  
35:       sys.exit('Lyrics not found')  

You can also find the source code here:
https://github.com/stinkinkevin/LyricsDownloader/tree/master

Enjoy!

Kev
Lyrics Downloader Lyrics Downloader Reviewed by Kevin Lai on 11:04:00 AM Rating: 5

No comments:

Powered by Blogger.