Skip to content

Instantly share code, notes, and snippets.

@e000
Created January 9, 2011 02:31
Show Gist options
  • Save e000/771362 to your computer and use it in GitHub Desktop.
Save e000/771362 to your computer and use it in GitHub Desktop.
#!/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