How to Search for Twitter status updates since certain time using REST API

In last post Search for twitter status updates I showed how you can do simple search. If you do not restrict or constrain your search, twitter will always return you maximum of latest 1500 updates. To make your query more efficient and reduce network traffic, you definitely want to only get results since your last search results. If you notice in the out put from last post, there is a property of status object named Id. Twitter always adds status entries in increasing order of this number. So if you have cached id of the latest results from last result, you can use that to tell twitter to return you the results since that Id only. You can also constrain the results by specifying a date as well. The following code snippet shows how you can perform search by specifying Since search criteria to get results from that point onwards only.


static void SearchForTermSince(string term, int id)
{
 IFluentTwitter ft = FluentTwitter.CreateRequest();
 ft.AuthenticateAs(LOGIN_NAME, PASSWORD);
 ITwitterSearchQuery sq = ft.Search().Query().Containing(term).Since(id);
 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("*************************");
 }
}

To test it, I used SearchForTermSince("swine", 1881546655);. Notice that id value of 1881546655 is third result from the top from previous run when there was no constraint added to search. Now see the results when I added Since criteria to search. You will notice that results started from id = 1881550472 which was the result right after 1881546655.

id: 1881554462
SwineFluNewsGB
5/22/2009 11:58:39 AM
0
Swine Flu News: H1N1 Swine Flu - Update 46. May 22 http://tinyurl.com/q357wf
*************************
id: 1881553402
mgrabois
5/22/2009 11:58:29 AM
0
@thedateabledork In a world without swine flu, I would be leaving for Cancun this morning ;(
*************************
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!!
comments powered by Disqus

Search

Social

Weather

Monthly Posts