example of using RateLimit?

45 views
Skip to first unread message

George Ludwig

unread,
May 5, 2011, 6:07:43 PM5/5/11
to jt...@googlegroups.com
Hi all,

I was wondering id there was an example anywhere of using the RateLimit object? Right now I'm going in to a wait state between api calls, and I know there is a better way.

Best,

George

Daniel Winterstein

unread,
May 6, 2011, 7:21:15 AM5/6/11
to jt...@googlegroups.com
Hi George,

If you want to run a loop at the limit, you could do something like this:

Twitter jtwit = ...;
while(true) {
try {
// Call Twitter methods, e.g.
jtwit.getUserTimeline();
} catch (TwitterException e) { /* handle exception or just continue? */ }
// pause as needed
if (jtwit.isRateLimited(KRequestType.NORMAL, 1)) {
RateLimit rateLimit = jtwit.getRateLimit(KRequestType.NORMAL);
rateLimit.waitForReset();
}
}

waitForReset() is a new method in the latest version which calls
Thread.sleep with the time until the reset date.

I've also improved the java-doc for the RateLimit methods. The
RateLimit object is created using the headers from the previous
Twitter call. So calling getRateLimit() is quick (it doesn't require a
fresh call to Twitter), but the RateLimit object isn't available until
after you make a call of the right type to Twitter. Also, be aware
that there are different rate-limits for the different Twitter
methods. For example, you can be OK for search() but not for show().

> Right now I'm going in to a wait state between api calls, and I know
> there is a better way.

When we need to make extensive use of Twitter at http://sodash.com, I
harness several Twitter accounts, and use the rate limit to switch
between them.

Best regards,
- Daniel

--
--------------------------------------------------
Daniel Winterstein
Edinburgh
http://winterwell.com   http://soda.sh

George Ludwig

unread,
May 6, 2011, 4:07:29 PM5/6/11
to jt...@googlegroups.com
Daniel,

Thanks so much for the response. Is it safe to assume that the RateLimit object is automatically handled by JTwitter, i.e. it figures out what my current rate limit is, or do I have to set the rate limit somewhere?

Using multiple accounts for rate limits is an interesting approach. If I use one account for a status search, would it be valid to then re-tweet that status using a different twitter account, or would that confuse some internal id? Just curious. :)

Best,

George

Daniel Winterstein

unread,
May 7, 2011, 2:50:14 PM5/7/11
to jt...@googlegroups.com
On 6 May 2011 21:07, George Ludwig <george...@gmail.com> wrote:
> Daniel,
> Thanks so much for the response. Is it safe to assume that the RateLimit
> object is automatically handled by JTwitter, i.e. it figures out what my
> current rate limit is, or do I have to set the rate limit somewhere?

No - it's set by Twitter. You can apply for an upgrade, but they
rarely grant them these days.

> Using multiple accounts for rate limits is an interesting approach. If I use
> one account for a status search, would it be valid to then re-tweet that
> status using a different twitter account, or would that confuse some
> internal id? Just curious. :)

You can certainly search from one Twitter account, and then post
responses from another.
But posting isn't rate-limited, so there'd be no need to do that.

Best regards,
- Daniel

Reply all
Reply to author
Forward
0 new messages