Finding Lyrics
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
import urllib
from optparse import OptionParser
print "Starting"
parser = OptionParser()
parser.add_option("-i", "--user_id",
action="store", type="string", dest="user_id",
help="The user id for the Lyrics Fly service")
parser.add_option("-a", "--artist",
action="store", type="string", dest="artist",
help="Artist name")
parser.add_option("-t", "--title",
action="store", type="string", dest="title",
help="Song title")
(options, args) = parser.parse_args()
print "Parsed options"
if (options.user_id):
user_id = options.user_id
if (options.artist):
artist = options.artist
if (options.title):
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
print url
data = urllib.urlopen(url)
print data.read()
print
print "Press any key to exit.."
Console.ReadKey()
It looks like Console.ReadKey() is the single line which wouldn’t work in CPython.
This would be:
import msvcrt
msvcrt.getch()
for windows. It’s a bit more tricky to have it on other platforms (tty functions needed), but doable. Alternatively:
print “Press Enter to continue…”
raw_input() # 😀
Konrad
June 9, 2010 at 2:40 pm
I like gathering utile information , this post has got me even more info! .
Rusty Cwiklinski
December 29, 2011 at 10:44 pm