#!/usr/bin/env python
import re
import urllib
artist = "Dream Theater"
song = "Surrounded"

uartist = artist.lower().replace (" ", "")
searchlink = 'http://www.darklyrics.com/%s/%s.html' %  (uartist[0], uartist)
pattern = '<a href="../lyrics/%s/(.*).html#([^"])+" target="_blank"><FONT COLOR="#CCCCCC">%s</FONT></a><br>' \
    % (uartist, song.title())

f = urllib.urlopen(searchlink)
html = f.read()
results = re.findall (pattern, html)
if results:
    album = results[0][0]
    songnumber = results[0][1]
    lyriclink = 'http://www.darklyrics.com/lyrics/%s/%s.html' % (uartist, album)
    f = urllib.urlopen(lyriclink)
    htmlalbum = f.read()
    titleline = '(?ms)<a name=%s><FONT color=#DDDDDD><b>%s. %s</b></font>(.+?)<a' % \
        (results[0][1], results[0][1], song.title())
    lyricmatch = re.split (titleline, htmlalbum)
    if len (lyricmatch) > 1:
        lyrics = lyricmatch[1]
        lyrics = lyrics.replace ('\r', "")
        lyrics = re.sub (r'<.*?>', "", lyrics)
        lyrics = lyrics.strip ("\n")
        title = "%s - %s\n\n" % (artist, song.title())

        lyrics = title + str (lyrics)
        lyrics += "\n\nLyrics provided by Dark Lyrics"
        print lyrics
