Re.Mark

My Life As A Blog

Finding Lyrics

with 2 comments

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()
About these ads

Written by remark

June 4, 2010 at 4:51 pm

Posted in .NET, Python

2 Responses

Subscribe to comments with RSS.

  1. 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() # :D

    Konrad

    June 9, 2010 at 2:40 pm

  2. I like gathering utile information , this post has got me even more info! .

    Rusty Cwiklinski

    December 29, 2011 at 10:44 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: