Featured Post

Opening Twhirl links in Firefox on Vista

So I’m sure I’m not the only one who is extremely frustrated by all links that I click from Twhirl opening in Internet Explorer, even though I have Firefox set as my default browser. It turns out this is some little quirk between Vista and Air, but it is fixable if you follow these steps: Step...

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.

Opening Twhirl links in Firefox on Vista

Posted by brendo | Posted in All Posts, web | Posted on 08-03-2008

Tags: , , , , , , , ,

2

So I’m sure I’m not the only one who is extremely frustrated by all links that I click from Twhirl opening in Internet Explorer, even though I have Firefox set as my default browser.

It turns out this is some little quirk between Vista and Air, but it is fixable if you follow these steps:

Step 1:

Open Default Programs from the start menu.

Step 2:

Choose “Set Program Access and Computer Defaults”

Step 3:

Click Custom, then choose Mozilla Firefox (or whatever your default browser is) and click ok.

And now you are done :D

Brendo