Tech Note: Retweet Code Revamp

Charles Johnsonfollow me on twitter
Technology • Tue Feb 9, 2010 at 4:25 pm PST • Views: 414

Here’s an email I recently received from the URL-shortening service bit.ly, a popular Twitter-related service that I use to automatically shorten URLs for LGF-related tweets. The nice thing about bit.ly (and j.mp, it’s the same service) is that it doesn’t just shorten URLs; if you register an account, it also tracks some interesting statistics for any URLs you shorten. And they make these statistics available to outside developers through the bit.ly API; this is how our Retweet buttons are able to show the number of clicks on each shortened link.

But apparently, LGF has been hammering their API servers too hard.

Hi,

I’m writing in regards to your bit.ly API usage. You are making an unusually large number of shorten requests to our API (often over 50,000/hour), and it looks like you are doing this on page load (for the Twitter button on pages such as http://littlegreenfootballs.com/article/35599).

Normally it is best not to make /shorten requests on a page load, but instead to make a /shorten request to bit.ly when a user clicks a share button. You can find an example of that at code.google.com. We are increasing our application of rate limits to manage abusive API users, and we encourage you to make the above fix as soon as possible to ensure that your site continues to function normally. I’d be glad to discuss this further and/or answer any questions you have, and thanks for using bit.ly!

The problem was related to the John Resig Retweet button, which I wrote about in this Tech Note. When the LGF home page loaded, the Retweet button Javascript code called the bit.ly API’s “shorten” function to find out the shortened URL, followed by the “stats” function to get the number of clicks, for each post, every time the page was loaded. As you can imagine, with 80,000 to 120,000 pageviews a day, that’s a lot of API calls. No wonder they noticed it.

I’ve vastly reduced the number of API calls now with the latest code refactoring of the Retweet button. I stopped using John Resig’s Javascript code (no hard feelings, it’s great for most sites but not LGF), and wrote my own jQuery routines that use Ajax to call a server-side script when the page loads, passing it the necessary data on every Retweet button in the page in JSON format. The Ajax server calls the API’s “stats” function, then sends back (again in JSON) the number of clicks and some other data. The jQuery code then displays those numbers in the Retweet buttons.

The key to reducing our API calls is that the shorten function is only called once, not when the page loads but when a new entry is posted to the LGF blog. The shortened URL is used to automatically tweet about the new post on Twitter, then it’s saved along with the new entry data, to be reused endlessly as needed without ever calling the API again.

Previously, the Resig code would call the API’s “shorten” routine and the “stats” routine for every post. Now the PHP Ajax server uses the saved bit.ly (or j.mp) URL to call the API’s “stats” routine only, which should cut our API usage in half. And retrieving statistics should be a much less costly operation for the server than shortening a URL. Hopefully, this will bring us back within the bit.ly API usage limits.

And since this is probably Venusian to most LGF readers, consider it an open thread from here on out…

Advertisement

546 comments

^ back to top ^

Name:

Pass:

Register Forgot Your Password? Account Settings Re-send Confirmation (To log in, cookies must be enabled in your browser!)

Turn off ads by subscribing!
For about 33 cents a day, our subscription option turns off all advertisements at LGF!
Read more...


► LGF Headlines

  • Loading...

► Tweeted Articles

  • Loading...

► Tweeted Pages

  • Loading...

► Top 10 Comments

  • Loading...

► Bottom Comments

  • Loading...

► Recent Comments

  • Loading...

► Tools/Info

► LGF Hits

► Resources

► Never Forget

► Statistics

► Tag Cloud

► Contact

You must have Javascript enabled to use the contact form.
Your email:

Subject:

Message:


Messages may be published in our weblog, unless you request otherwise.
Tech Note:
Using the Contact Form

More Partners

Compare Electricity Prices in your area. Texas Electricity is deregulated; you have the right to choose Texas Electric Rates from among many Texas Electric Companies.

Strangely filling.

TwitterFacebook
LGF Pages
Recent Pages

Curt
Ladies Home Journal Predictions for 2000, from 1900
3 minutes ago
Views: 4 • Comments: 0
Tweets: 0 • Rating: 0

Channeling Confucius
CPAC 2012: Are the Republican Party's Views on Gay Marriage Shifting?
14 minutes ago
Views: 12 • Comments: 0
Tweets: 0 • Rating: 0

Curt
U.S. Federal Deficits, Presidents, and Congress (Updated 09/2011)
2 hours, 56 minutes ago
Views: 57 • Comments: 0
Tweets: 0 • Rating: 1

Look At My New Grandbaby!
EPIC FAIL: Religious Magazine Mistakenly Publishes Smutty Content
3 hours, 36 minutes ago
Views: 116 • Comments: 0
Tweets: 3 • Rating: 2

Randall Gross
Lasers Plus a Crushing Magnetic Field May Make Fusion More Efficient
3 hours, 56 minutes ago
Views: 89 • Comments: 0
Tweets: 0 • Rating: 1

Randall Gross
White House: Matter of Time Before Assad Falls
3 hours, 59 minutes ago
Views: 76 • Comments: 0
Tweets: 0 • Rating: 0

Randall Gross
ADD theatre: A peek at Oscar's shorts
4 hours, 12 minutes ago
Views: 73 • Comments: 0
Tweets: 0 • Rating: 0

Randall Gross
When Will They Ever Learn?
4 hours, 15 minutes ago
Views: 99 • Comments: 0
Tweets: 2 • Rating: 0

Channeling Confucius
Why Israel Might Believe Attacking Iran Is Worthwhile - Michael Hirsh - the Atlantic
4 hours, 37 minutes ago
Views: 116 • Comments: 1
Tweets: 0 • Rating: 0

Channeling Confucius
6 Real Planets That Put Science Fiction to Shame
4 hours, 40 minutes ago
Views: 90 • Comments: 0
Tweets: 0 • Rating: 0

 Frank says:

Americans like to talk about (or be told about) Democracy but, when put to the test, usually find it to be an 'inconvenience.' We have opted instead for an authoritarian system disguised as a Democracy. We pay through the nose for an enormous joke-of-a-government, let it push us around, and then wonder how all those assholes got in there.