简体   繁体   中英

Twitter API - If Rate limit exceeded

I am developing a Twitter web app for myself. I am retrieving the latest trending topics.

Here's how I'm doing it:

    $.ajax({
                      url: 'http://api.twitter.com/1/trends/1.json',
                      dataType: 'jsonp',
                       success: function(data){


                       $.each(data[0].trends, function(i){
                          $('div#trending').hide().append("<p><a href='"+data[0].trends[i].url+"'>"+data[0].trends[i].name+"</a></p>").fadeIn(1000);
                        //Cache into LocalStorage
                    localStorage["trending"+i] = data[0].trends[i].name; //Name
                    localStorage["trendurl"+i] = data[0].trends[i].url;//URL

                                });
                            }
});

But sometimes while I am developing it, the rate limit is exceeded.

How can I detect if the rate limit has been exceeded?

I cannot seem to detect if this error is being shown:

{"error":"Rate limit exceeded. Clients may not make more than 150 requests per hour.","request":"\/1\/trends\/1.json"}

I have tried it by:

success: function(data){
  if(data[0].error != 'undefined'){
    //load localstorage cache
  }
}

But that doesn't seem to work. Please help.

Thanks :)

The Twitter API sends a HTTP 400 status code when you are rate limited, so check for that:

$.ajax({
    // ...
    statusCode: {
        400: function() {
            alert( 'rate limited.' );
        }
    }
});

Also note that your comparison is a bit wrong. data[0].error != 'undefined' will always yield true when the error text is not 'undefined' . So even when you are rate limited, the error text won't be 'undefined' and as such succeed. What you probably want to check is this:

if ( !data[0].error ) { // data[0].error is not null
    // ...
}

try something like $.ajax({..}).fail(function(){});

ie

$.ajax({..})
.done(function() { alert("success"); })
.fail(function() { alert("error"); })
.always(function() { alert("complete"); });

and let me know how this works now.

cheers, /Marcin

If you're not making an OAuth call, you'll be rate limited to 150 calls per hour. But, there's a small work-around which has worked for me.

According to the Twitter page on Rate Limiting (http://dev.twitter.com/docs/rate-limiting), "Rate limits are applied to methods that request information with the HTTP GET command. Generally API methods that use HTTP POST to submit data to Twitter are not rate limited, however some methods are being rate limited now."

Since the default type of an AJAX call is 'GET', try explicitly changing your type to 'POST' like this:

$.ajax({
     url: 'http://api.twitter.com/1/trends/1.json',
     type: 'POST',
     dataType: 'jsonp',
     success: function(data){
              $.each(data[0].trends, function(i){
                   $('div#trending').hide().append("<p><a href='"+data[0].trends[i].url+"'>"+data[0].trends[i].name+"</a></p>").fadeIn(1000);
                    //Cache into LocalStorage
                   localStorage["trending"+i] = data[0].trends[i].name; //Name
                   localStorage["trendurl"+i] = data[0].trends[i].url;//URL

               });
      }

});

Hope this helps!

James

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM