The millionth Twitter parser
28 July 2009 –For some reason the thing I get contacted the most about my site is that thing in the right column:
I’m trying to show my latest tweet on my website like yours. How do you make the names and links show up like that?
Conceptually it’s very simple. If you like Regular Expressions, the implementation is very simple too. If you don’t, those bits might be described as hellish. Here’s the workflow:
- Fetch latest tweet via the Twitter API
- Find all @name references, look up their real name, and replace with a link
- Find all URLs (“http(s)://…”) and replace with a link to that URL
- Find all hashtags (#hashtag) and replace with a link to Twitter Search
- If the tweet starts with @name(s), wrap them in a “To {@name(s)}:”
Simple! Right? Right. So we take a tweet which looks like:
@ballance @bspotter a summary of my view on #sharepoint - http://rexmorgan.net/media/spcomparison.jpg
And format it to look like:
<span class="salutation">To
<a href="http://twitter.com/ballance">Chris</a>,
<a href="http://twitter.com/bspotter">Brandon</a>:
</span>
a summary of my view on <a href="http://twitter.com/#search?q=%23sharepoint">#sharepoint</a>
- <a href="http://rexmorgan.net/media/spcomparison.jpg">http://rexmorgan.net/media/spcomparison.jpg</a>
<a class="date" href="http://twitter.com/rexm/status/2772241681">— <span>8:06 PM yesterday</span></a>
The code’s a bit too long to paste in this format, so I got to take Rick Strahl’s sweet CodePaste.NET for a spin and dropped the code here.
Two thing to keep in mind: (1) Twitter can sometimes be slow. Multiple lookups to get people’s real names means this can be even slower. Now, Twitter is pretty cool, but it’s not worth making your audience wait for it to time out. It’s not worth making your audience wait at all. Stick all this code in an HttpHandler and include it as a JavaScript at the bottom of your page, so it will execute after your page is safely in your user's browser. If it fails, you'll just have a blank spot on your page:
</body>
<script type="text/javascript" src="twitter.ashx"></script>
(2) The Twitter's REST API has a 150-request-per-hour limit from a single account or IP. Cache your tweet results enough not to go over that limit, or you will be blacklisted! I cache mine for two minutes. Cheers!
Content available under a Creative Commons license.
Site code and design may not be reproduced.


I was going to upvote your blog post when I realised that this was not StackOverflow. I feel I may be spending too much time there... :)