Created
January 9, 2011 02:31
-
-
Save e000/771362 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import urllib | |
from BeautifulSoup import BeautifulSoup | |
from tornado.escape import xhtml_unescape | |
class MetroLyrics(): | |
@staticmethod | |
def search(artist, title): | |
params = urllib.urlencode({'search': ' - '.join((artist, title)), 'category': 'artisttitle', 'submit': 'Search'}) | |
data = urllib.urlopen('http://www.metrolyrics.com/search.php', params) | |
soup = BeautifulSoup(data) | |
out = [] | |
for result in soup.find('ul', id='results').findAll('li'): | |
links = result.findAll('a') | |
out.append((links[1].text, links[2].text[:-7], links[2].get('href'))) | |
return out | |
@staticmethod | |
def extract(url): | |
data = urllib.urlopen('http://www.metrolyrics.com%s' % url).read() | |
soup = BeautifulSoup(data) | |
lyrics = xhtml_unescape(soup.find('div', id='lyrics').text) | |
artist, title = (xhtml_unescape(soup.find('div', id='lyricsBox').find('h4').text[:-7])).split(': ') | |
return artist, title, lyrics | |
import sys | |
q = ' '.join(sys.argv) | |
artist, title = q.split(' - ', 2) | |
res = MetroLyrics.search(artist, title) | |
print MetroLyrics.extract(res[0][2])[2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment