I was looking some lyrics up online this week, so I wondered how hard to would be to write a simple application to find lyrics to your favourite song. Or to your least favourite song. Or, in fact, to any arbritrary song. Via programmableweb, I found the API to lyricsfly, which looked easy to use. Another IronPython console app beckoned.
Keeping it simple, I decided to use optparse to parse the command-line options and urllib to make the http calls. This way the program can be called with the user_id that lyricsfly requires (head to their website and you can get a temporary weekly key to try this out) along with the artist name and song title. What I decided not to do at this stage was to process the resulting XML. Or handle any errors. Or handle cases where the user_id, artist or title is not supplied. But, although rudimentary, it works. Here’s the code:
from System import Console
from optparse import OptionParser
parser = OptionParser()
action="store", type="string", dest="user_id",
help="The user id for the Lyrics Fly service")
action="store", type="string", dest="artist",
action="store", type="string", dest="title",
(options, args) = parser.parse_args()
print "Parsed options"
user_id = options.user_id
artist = options.artist
title = options.title
print "Getting Lyrics for " + artist + " - " + title
query = urllib.urlencode([("i", user_id), ("a", artist), ("t", title)])
url = "http://api.lyricsfly.com/api/api.php?" + query
data = urllib.urlopen(url)
print "Press any key to exit.."