comment 0

Code Sample: Consuming the Twitter Streaming API

Here’s a c# example of consuming the Twitter Streaming API.
Suppose you want to take all Tweets mentioning a country and save it to a database.
I will be using the Twitterizer c# library. Let’s first look at the complete code and then walk through it:

private static bool streamEnded = false;
public static void HandleStream()
{
  DateTime end = DateTime.Now + TimeSpan.FromSeconds(60);

  OAuthTokens tokens = new OAuthTokens { ConsumerKey = “YOUR_KEY”, ConsumerSecret = “YOUR_SECRET”, AccessToken = “ACCESS_TOKEN”, AccessTokenSecret = “ACCESS_TOKEN_SECRET” };
  TwitterStream s = new TwitterStream(tokens);
  s.OnStatusReceived += new TwitterStatusReceivedHandler(s_OnStatusReceived);
  s.OnStreamEnded += new TwitterStreamEnded(s_OnStreamEnded);
  FilterStreamOptions ops = new FilterStreamOptions { Track = new List<string> { “italy”, “germany”, “spain”, “france”, “england” } };
  s.StartFilterStream(ops);

  while (DateTime.Now < end && !streamEnded)
     Thread.Sleep(1000);
 

  s.EndStream();
}
static void s_OnStreamEnded()
{
   streamEnded = true;
}
static void s_OnStatusReceived(Twitterizer.TwitterStatus status)
{
  SaveTweetToDatabase(status);
}

Now let’s look at the code in depth. First, as we’ll be processing tweets in a loop, let’s set an end to our loop. Let’s say we want to process tweets for 60 seconds:

DateTime end = DateTime.Now + TimeSpan.FromSeconds(60);

Next, we setup the Twitterizer class used to handle the Streaming API:

OAuthTokens tokens = new OAuthTokens { ConsumerKey = “YOUR_KEY”, ConsumerSecret = “YOUR_SECRET”, AccessToken = “ACCESS_TOKEN”, AccessTokenSecret = “ACCESS_TOKEN_SECRET” };
TwitterStream s = new TwitterStream(tokens);

The class TwitterStream is part of the “addons” to Twitterizer, so you will need to download the source of the 2.3.2 release of Twitterizer and make a small fix as I described here.
You will pass to TwitterStream the app keys of your Twitter application which you set up here and the OAuth tokens.
Next, we set up event handlers for the stream of tweets:

s.OnStatusReceived += new TwitterStatusReceivedHandler(s_OnStatusReceived);
s.OnStreamEnded += new TwitterStreamEnded(s_OnStreamEnded);

s_OnStatusReceived will be called by TwitterStream when a tweet is received from Streaming API and s_OnStreamEnded will be called when the connection to Twitter ends.
Next, we may add some options to the Streaming API. There are several methods to access the Streaming API: you can either get tweets for a list of keywords, a list of users or a random sample of all tweets. In our example, let’s ask Twitter for tweets mentioning a country and open the connection:

FilterStreamOptions ops = new FilterStreamOptions { Track = new List<string> { “italy”, “germany”, “spain”, “france”, “england” } };
s.StartFilterStream(ops);

The last thing to do is wait for the amount of time we decided on in the beginning and then close the stream:

while (DateTime.Now < end && !streamEnded)
     Thread.Sleep(1000);
 
s.EndStream();

Leave a Reply

Your email address will not be published. Required fields are marked *