Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a RateLimitStatus object in Twitter4j?

I'm trying to make a method public void limit() that checks the rate limit and sleeps however long it is until the reset if it is being rate limited. I cannot, however, figure out how to make a RateLimitStatus. I have tried: RateLimitStatus status = twitter.getRateLimitStatus(); but it doesn't actually return a RateLimitStatus... Quite frankly, I'm not sure what the point of that is. Anyway, if anyone is aware of how to get a RateLimitStatus, their help would be much appreciated as currently my project is capable of crashing due to rate limits and I'd like to change this. Thanks in advance!

like image 898
AgentOrange96 Avatar asked Dec 12 '22 14:12

AgentOrange96


1 Answers

The new Twitter API has a rate limit status per resource “family”, so twitter.getRateLimitStatus() returns a mapping between families/endpoints and rate limit statuses, e.g.:

RateLimitStatus status = twitter.getRateLimitStatus().get("/users/search");

// Better: specify the family
RateLimitStatus status2 = twitter.getRateLimitStatus("users").get("/users/search");

So, you could write a method public void limit(String endpoint), which would check the proper rate limit status.

public void limit(String endpoint) {
  String family = endpoint.split("/", 3)[1];
  RateLimitStatus status = twitter.getRateLimitStatus(family).get(endpoint);
  // do what you want…
}

You’ll then call it with .limit("/users/search").

like image 116
bfontaine Avatar answered Dec 28 '22 05:12

bfontaine