How to get RateLimitStatus object on Twitter4j?

I am trying to make a public void limit() method that checks the speed limit and sleeps, but until it is reset if it is limited by the limit. However, I cannot figure out how to make RateLimitStatus. I tried: RateLimitStatus status = twitter.getRateLimitStatus(); but in fact it does not return RateLimitStatus ... Honestly, I'm not sure what it is. In any case, if someone knows how to get RateLimitStatus, their help would be greatly appreciated, as my project is currently crashing due to speed limits, and I would like to change that. Thanks in advance!

+4
source share
3 answers

The new Twitter API has a speed limit status for a "family" resource , so twitter.getRateLimitStatus() returns a mapping between families / endpoints and speed limit states, for example:

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

So you can write a public void limit(String endpoint) method that will check the correct speed limit state.

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

Then you call it with .limit("/users/search") .

+7
source
 Map<String ,RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus(); for (String endpoint : rateLimitStatus.keySet()) { RateLimitStatus status = rateLimitStatus.get(endpoint); System.out.println("Endpoint: " + endpoint); System.out.println(" Limit: " + status.getLimit()); System.out.println(" Remaining: " + status.getRemaining()); System.out.println(" ResetTimeInSeconds: " + status.getResetTimeInSeconds()); System.out.println(" SecondsUntilReset: " + status.getSecondsUntilReset()); } 
+5
source

The Twitter API also allows you to:

 Log.d("TwitterActivity", "Limit:" + mTwitter.getFavorites().getRateLimitStatus().getLimit()); 

Where:

  • mTwitter is your Twitter object.
  • getFavorites () can be replaced with any other function that Twitter4j provides for the Twitter object.
  • getLimit () is just one of the options you can choose

You can check like this:

 if(mTwitter.getFavorites().getRateLimitStatus().getLimit() <= 0){ //do something } 
0
source

Source: https://habr.com/ru/post/1483265/


All Articles