Skip to content
This repository was archived by the owner on Feb 13, 2018. It is now read-only.

Commit 070e57a

Browse files
author
Tim Whitlock
committed
added rate limit utilities
1 parent 51fd905 commit 070e57a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

examples/rate-limits.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Example spanks API rate limit and shows how to get rate limit data from client
3+
*/
4+
5+
var client = require('../lib/twitter').createClient();
6+
7+
client.setAuth (
8+
'4MROlaLN7k2IyzhLrC7bg',
9+
'x0HLIFFAvCGq3kse9EZt5asrPTzMhYxTtztp1QidI',
10+
'16683251-gRyf6HILdKYrnGYvcYl4ZABS2h7Js9ylJ2h22kD8Q',
11+
'V0fmla3bfopj9ApclxcKRfppU8OF9V6iLbHwm9U7I'
12+
);
13+
14+
15+
// Page through followers - this will run out pretty quickly
16+
var cursor = '-1';
17+
function nextPage(){
18+
client.get( 'followers/ids', { cursor: cursor, screen_name: 'timwhitlock', skip_status: true }, function( page, error, status ){
19+
if( error ){
20+
if( 429 === status ){
21+
var resetTime = client.getRateLimitReset();
22+
console.error('Wait until '+ resetTime.toString() );
23+
}
24+
process.exit();
25+
}
26+
else {
27+
console.log('OK: '+cursor+' -> '+page.ids.length+' followers' );
28+
cursor = page.next_cursor_str;
29+
30+
var limit = client.getRateLimit();
31+
if( limit ){
32+
var remaining = client.getRateLimitRemaining();
33+
if( remaining ){
34+
console.log( 'No requests left. Next call will throw 429 .. you\'ll see' );
35+
}
36+
else {
37+
console.log( remaining+' of '+limit+' requests remaining');
38+
}
39+
}
40+
}
41+
cursor && nextPage();
42+
} );
43+
}
44+
nextPage();

lib/twitter.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ OAuthParams.prototype.getHeader = function(){
118118
*/
119119
function TwitterClient(){
120120
this.deAuth();
121+
// register rate limits for all REST calls
122+
this.lastCall = null;
123+
this.lastMeta = {};
124+
}
125+
126+
TwitterClient.prototype.getLastMeta = function( method ){
127+
return this.lastMeta[ method || this.lastCall ] || { limit: 0, remaining: 0, reset: 0 };
128+
}
129+
130+
TwitterClient.prototype.getRateLimit = function( method ){
131+
return this.getLastMeta(method).limit;
132+
}
133+
134+
TwitterClient.prototype.getRateLimitRemaining = function( method ){
135+
return this.getLastMeta(method).remaining;
136+
}
137+
138+
TwitterClient.prototype.getRateLimitReset = function( method ){
139+
return new Date( this.getLastMeta(method).reset * 1000 );
121140
}
122141

123142
TwitterClient.prototype.setAuth = function( consumerKey, consumerSecret, accessKey, accessSecret ){
@@ -159,6 +178,8 @@ TwitterClient.prototype._rest = function( requestMethod, requestPath, requestArg
159178
console.error('No callback for '+requestMethod+' '+requestUri);
160179
}
161180
}
181+
this.lastCall = requestPath;
182+
var client = this;
162183
return this.call( requestMethod, requestUri, requestArgs, TWITTER_API_TIMEOUT, function( res, err ) {
163184
if( ! res ){
164185
callback( null, err, 0 );
@@ -191,6 +212,16 @@ TwitterClient.prototype._rest = function( requestMethod, requestPath, requestArg
191212
callback( data, error, res.statusCode );
192213
}
193214
} );
215+
// remember last rest call
216+
// pull rate limit data from headers
217+
var limit = res.headers['x-rate-limit-limit'];
218+
if( limit ){
219+
client.lastMeta[ client.lastCall ] = {
220+
limit: Number( limit ),
221+
remaining: Number( res.headers['x-rate-limit-remaining'] ),
222+
reset: Number( res.headers['x-rate-limit-reset'] )
223+
}
224+
}
194225
} );
195226
}
196227

0 commit comments

Comments
 (0)