<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>oldmatebrendo.com &#187; lyricbot</title>
	<atom:link href="http://oldmatebrendo.com/tag/lyricbot/feed/" rel="self" type="application/rss+xml" />
	<link>http://oldmatebrendo.com</link>
	<description>the incoherent ramblings of brendo</description>
	<lastBuildDate>Fri, 18 Sep 2009 01:02:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Lyrical twitter bot</title>
		<link>http://oldmatebrendo.com/2008/03/lyrical-twitter-bot/</link>
		<comments>http://oldmatebrendo.com/2008/03/lyrical-twitter-bot/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 21:57:38 +0000</pubDate>
		<dc:creator>brendo</dc:creator>
				<category><![CDATA[All Posts]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[lyricbot]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[twitter api]]></category>
		<category><![CDATA[twitter bot]]></category>
		<category><![CDATA[twitterpython]]></category>

		<guid isPermaLink="false">http://oldmatebrendo.com/2008/03/lyrical-twitter-bot/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>After making the <a href="http://twitter.com/sickipedia">sickipedia twitter bot</a>, I decided to make another that made use of a little bit more of the Twitter API this time. I call him <a href="http://twitter.com/lyricbot">lyricbot</a>. 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&#8230;</p>
<p>I decided to post some of the code for anyone interested in how I did it.</p>
<p>The bot is written in Python, and uses the <a href="http://code.google.com/p/python-twitter/">python-twitter library</a> for the Twitter API calls. I will apologize upfront, the code is probably disgusting, but this is only the second thing I&#8217;ve ever written in python, so I&#8217;m still an L plater.</p>
<p>The first thing we want to do is import the 3 libraries we need; re,  twitter and urllib2.</p>
<p><code>import twitter<br />
import re<br />
import urllib2</code></p>
<p>Next we need to authenticate with the twitter API, then we&#8217;ll get the replies in the form of a Status object.<br />
<code><br />
api = twitter.Api(username='lyricbot', password='nottelling:)')<br />
status = api.GetReplies()</code></p>
<p>Now that we have all the replies sent to that user, we can iterate through them to decipher what people have said:</p>
<p><code>for stat in status: <font color="#99cc00">#iterate through replies</font><br />
lyric = stat.GetText()[10:] <font color="#99cc00">#strip the '@lyricbot ' from the front and grab the text</font><br />
userN = str(stat.GetUser().GetScreenName()) <font color="#99cc00">#grab the name of the user that replied</font><br />
idno = stat.GetId() <font color="#99cc00">#the unique id no of the message. This is for ensuring people don't get multiple replies for one request</font></code></p>
<p>Next I parse the string for use in the URL and get the html:<br />
<code></code></p>
<p><code>lyric = urllib2.quote(lyric)<br />
f = urllib2.urlopen(url) <font color="#99cc00">#open the URL into the stream</font><br />
html = f.read() <font color="#99cc00">#read the url into a string</font><br />
</code></p>
<p>Once I have the html of the search results, I need to use regular expressions to find the song names from that site.<br />
<code><br />
<font color="#99cc00">#compile the regex that finds the first 3 song names returned</font><br />
re_lyr = re.compile('<strong>[123].</strong><strong>([A-Za-z0-9].{10,80}).Lyrics</strong>', re.DOTALL)<br />
<font color="#99cc00"> #find the matches for the expression in the html</font><br />
newRes = re_lyr.findall(html,1)<br />
songs = ''<br />
</code></p>
<p>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.<br />
<code><br />
reslen = len(newRes)<br />
if reslen &gt; 0 :<br />
songs = songs+newRes[0]  <font color="#99cc00">#append to the string that gets sent</font><br />
if tuplen &gt; 1 :<br />
songs = songs+' '+newRes[1]<br />
if tuplen &gt; 2 :<br />
songs = songs+' '+newRes[2]<br />
else : <font color="#99cc00">#if there were no songs found</font><br />
songs = 'Sorry <img src='http://oldmatebrendo.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  I couldn\'t find that one!<br />
</code></p>
<p>Now that we have a string to post (either the song names or the fail message), itÃ¢â‚¬â„¢s time to send:<br />
<code><br />
api.PostDirectMessage(userN, songs)<br />
</code><br />
ThatÃ¢â‚¬â„¢s about it really.<br />
There will be another post after I launch it properly, probably with some updates.</p>
]]></content:encoded>
			<wfw:commentRss>http://oldmatebrendo.com/2008/03/lyrical-twitter-bot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
