abudker
abudker

Reputation: 712

How to get twitter server time?

So I'm trying to build a real time monitoring tool for twitter key words using tweet sharp. I'm using the search API to collect queries every 10-15 seconds. When I make the calls, I only want to collect tweets that have appeared since the pervious update.

var twitter = FluentTwitter.CreateRequest().AuthenticateAs("username", "password").Search().Query().Containing("key word").Take(1000);
var response = twitter.Request();

currentResponseDateTime= Convert.ToDateTime(response.ResponseDate);

var messages = from m in response.AsSearchResult().Statuses
               where m.CreatedDate > lastUpdateDateTime
               select m;

lastUpdateDateTime = currentResponseDateTime;

My issue is that the twitter server time is different from the client times by a few seconds. I looked around and tried to get the datetime I recieved the response from the Response.ResponseDate property, but it looks like that is set based on the local computer time. I.e currentResponseDateTime is a few seconds ahead of the Twitter Server time. So I end up not collecting a few of the tweets.

Does anyone know how I can get the current server time from twitter search or REST API?

Thanks

Upvotes: 0

Views: 3148

Answers (2)

Inactivist
Inactivist

Reputation: 10477

Another approach (and one that Twitter recommends) is to pull the Date header from their API server's response, which provides Twitter's notion of time in GMT. This assumes that you can access the server response headers, and that depends on the method you're using to access the API.

For example, hitting https://api.twitter.com/1/help/test.json

$ lynx --dump --head https://api.twitter.com/1/help/test.json
HTTP/1.0 200 OK
Date: Tue, 22 Jan 2013 13:30:36 GMT
...

Reference: how to get the twitter server time (synchronize)? on dev.twitter.com support site.

Quoting Taylor Singletary:

The current time that Twitter "thinks" it is is returned in the "Date" HTTP header of every response to an API call you make. You can also issue a simple HTTP HEAD request to GET help/test to get the header as an initial syncing step for your app.

Upvotes: 0

Ryan Brunner
Ryan Brunner

Reputation: 14851

I'm not sure how you would get the local server time of the twitter service, but one approach you could take is to store the date of the most recent twitter update seen in the "lastUpdateDateTime" field. That way, you're guaranteed to get all the messages since the last one you saw, regardless of the offset of the twitter server.

        var twitter = FluentTwitter.CreateRequest().AuthenticateAs("username", "password").Search().Query().Containing("key word").Take(1000);
        var response = twitter.Request();

        currentResponseDateTime= Convert.ToDateTime(response.ResponseDate);

        var messages = from m in response.AsSearchResult().Statuses
                       where m.CreatedDate > lastUpdateDateTime
                       select m;

        lastUpdateDateTime = messages.Select(m => m.CreatedDate).Max();

Upvotes: 4

Related Questions