Featured Post

Why I’m glad I grew up in Australia

In my project management tutorial at uni yesterday, the task was to get into groups and firstly write the tasks you think you would need to plan a party, then create a work breakdown structure and see if you missed any. I got together with my group. Being the only non-asian in the entire subject my group...

Read More

Lyrical twitter bot

Posted by brendo | Posted in All Posts, programming | Posted on 18-03-2008

Tags: , , , , ,

0

After making the sickipedia twitter bot, I decided to make another that made use of a little bit more of the Twitter API this time. I call him lyricbot. Basically, you send a reply to lyricbot (after you have followed) with a line from a song and it will have a red hot go at telling you what the song is…

I decided to post some of the code for anyone interested in how I did it.

The bot is written in Python, and uses the python-twitter library for the Twitter API calls. I will apologize upfront, the code is probably disgusting, but this is only the second thing I’ve ever written in python, so I’m still an L plater.

The first thing we want to do is import the 3 libraries we need; re, twitter and urllib2.

import twitter
import re
import urllib2

Next we need to authenticate with the twitter API, then we’ll get the replies in the form of a Status object.

api = twitter.Api(username='lyricbot', password='nottelling:)')
status = api.GetReplies()

Now that we have all the replies sent to that user, we can iterate through them to decipher what people have said:

for stat in status: #iterate through replies
lyric = stat.GetText()[10:] #strip the '@lyricbot ' from the front and grab the text
userN = str(stat.GetUser().GetScreenName()) #grab the name of the user that replied
idno = stat.GetId() #the unique id no of the message. This is for ensuring people don't get multiple replies for one request

Next I parse the string for use in the URL and get the html:

lyric = urllib2.quote(lyric)
f = urllib2.urlopen(url) #open the URL into the stream
html = f.read() #read the url into a string

Once I have the html of the search results, I need to use regular expressions to find the song names from that site.

#compile the regex that finds the first 3 song names returned
re_lyr = re.compile('[123].([A-Za-z0-9].{10,80}).Lyrics', re.DOTALL)
#find the matches for the expression in the html
newRes = re_lyr.findall(html,1)
songs = ''

Once I have the (1, 2 or 3) song results stored in the newRes tuple, I need to check how many (if any) it found, or deal with none being found.

reslen = len(newRes)
if reslen > 0 :
songs = songs+newRes[0] #append to the string that gets sent
if tuplen > 1 :
songs = songs+' '+newRes[1]
if tuplen > 2 :
songs = songs+' '+newRes[2]
else : #if there were no songs found
songs = 'Sorry :( I couldn\'t find that one!

Now that we have a string to post (either the song names or the fail message), it’s time to send:

api.PostDirectMessage(userN, songs)

That’s about it really.
There will be another post after I launch it properly, probably with some updates.