My problem
I want to know what my friends on Twitter are talking about most
What’s the plan:
- Get the tweets from my friends
- Search this tweets for the #hashtags (Regex!!!)
- Count the hashtags
- Output the hashtags ordered by count
With tools like VS, .net framework and Twitter API this is EASY!!
Wooohooo
1. How to get the tweets
a) Twitter API
Find all info here http://apiwiki.twitter.com/
Currently Twitter has 2 APIs, maybe they make things easier and bring these together…
b) Tweetsharp
Tweetsharp is an awesome fluent interface for the Twitter API.
It’s toooo easy!!!! Make sure you check it out
c) Twitter "low level"
You can consume Twitter stuff via standard HTTP calls and get the response as XML or JSON, see this blog for an example http://psantos-blog.zi-yu.com/?p=197
That’s tooo hard :-)
I picked Tweetsharp, because there are so many samples available, and it is fluent! I love Intellisense!
- Download tweetsharp http://code.google.com/p/tweetsharp/
- Unzip to your References Solution folder
- Add reference to your project
- Done
How easy is that!
Note
Make sure to use a Console project, we want to focus on functionality not on UI
The code to get the list of tweets looks like this
var twitter = FluentTwitter.CreateRequest().AuthenticateAs(MyUser, MyPass)
.Statuses().OnFriendsTimeline().Take(1000).AsJson();
// Get response from Twitter
var response = twitter.Request();
// Convert response to data classes
var tweets = response.AsStatuses();
Next step
Iterate through tweets and search for the hashtags
We use a Regex for that
private static Regex regexHashTags = new Regex(@"#\w*", RegexOptions.IgnoreCase);
No more code, because that’s up to you how to do that
Put this hashtags into dictionary
Dictionary<string, int>
(string is the hashtag, int is the count)
Easy, so no code here
Sort the Dictionary with LINQ
var sortedDict = (from entry in hashtagCountDict orderby entry.Value descending select entry);
The final output is easy again
Iterate through the sorted dictionary and throw it to the Console
Figure: Output of current Twitter buzz. Sharepoint is big currently (13/05/2009)
because of the TechEd announcements of Sharepoint 2010
Advanced steps
- Get the tweet updates every couple of seconds
- Use a while (abortKeyNotPressed) { } with a Thread.Sleep to get the buzz every 1 minute
- Use Console KeyAvailable to abort application
- Use the space key to get the current state of the iteration
- Serialize current dictionary to file, and load that at startup
Continue from last time
Thanks Robert Mühsig for VERY useful input http://code-inside.de/blog/2009/04/20/howto-twittern-mit-c/
1 comment:
From workmate Justin: http://hashtags.org/ for global hasttag trends
Post a Comment