How to Search for Twitter status updates using REST API

This code snippets will demonstrate how you can use Tweetsharp API.


static void SearchForTerm(string term)
{
 IFluentTwitter ft = FluentTwitter.CreateRequest();
 ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
 ITwitterSearchQuery sq = ft.Search().Query().Containing(term);
 var searchResults = new List();
 var resultsPerPage = 50;
 // Maximum results returned from tweeter are 1500.
 var totalPages = Math.Ceiling(1500m/resultsPerPage);
 for (var i = 1; i <= totalPages; i++)
 {
  var search = sq.Return(resultsPerPage)
	.Skip(i)
	.AsJson();
  var response = search.Request();
  var results = response.AsSearchResult();
  searchResults.AddRange(results.Statuses);
 }
 // Display results
 foreach (var status in searchResults)
 {
  Console.WriteLine("id: {0}", status.Id);
  Console.WriteLine(status.FromUserScreenName);
  Console.WriteLine(status.CreatedDate);
  Console.WriteLine(status.SinceId);
  Console.WriteLine(status.Text);
  Console.WriteLine("*************************");
 }
}

Here are some of the top results for query term swine that is one of the most talked about topics these days. You will notice that search results are returned in chronological order. For brevity I have only listed few results instead of all 1500 which is maximum number of results twitter API will return.

id: 1881551912
littlekelsey
5/22/2009 11:58:15 AM
0
sick, at home. i swear i have swine flu. wish i had company <3
*************************
id: 1881550472
MessiahKaeto
5/22/2009 11:58:00 AM
0
@envysays http://twitpic.com/5ooyq - You ain't kidding, 
I'd be all over that like a swine-flu patients mask!!
*************************
id: 1881546655
AstroXa
5/22/2009 11:57:22 AM
0
@in_health #H1N1 #Swine Flu WHO Update #36, 11168 cases 86 deaths 42 countries,
 22 May 2009, 06.00 GMT - http://bit.ly/YtLEj
*************************
id: 1881546160
jcesar18
5/22/2009 11:57:17 AM
0
Swine flu hit albany...
*************************
id: 1881545636
nationalpost
5/22/2009 11:57:12 AM
0
Season change could spread swine flu in Asia-Pacific http://tinyurl.com/oucylx
*************************
comments powered by Disqus

Search

Social

Weather

Monthly Posts