Tech Note: Retweet Code Revamp

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

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 [Link: littlegreenfootballs.com…]

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? 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

► 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.

We don't feel like satan.

TwitterFacebook
LGF Pages
Recent Pages

ggt
Leona's Sister Gerri
38 minutes ago
Views: 20 • Comments: 0
Tweets: 0 • Rating: 0

Daniel Ballard
Prove Mitt's Not a Unicorn-Apologies To The Unicorns
1 hour, 48 minutes ago
Views: 45 • Comments: 0
Tweets: 0 • Rating: 0

researchok
Out of Gas
2 hours, 33 minutes ago
Views: 72 • Comments: 0
Tweets: 0 • Rating: 0

I AM BREITBART!
Seattle Manhunt Launched After Deadly Cafe Shootings, Carjacking
2 hours, 48 minutes ago
Views: 76 • Comments: 1
Tweets: 0 • Rating: 1

Shiplord Kirel
Dragon to Depart Station Thursday
4 hours ago
Views: 36 • Comments: 0
Tweets: 0 • Rating: 1

Randall Gross
Citadel Malware Delivers Reveton Ransomware in attempts to extort money
4 hours, 52 minutes ago
Views: 108 • Comments: 0
Tweets: 0 • Rating: 2

Channeling Confucius
From the previous Diamond Jubilee: Kipling's 'Recessional'
5 hours, 23 minutes ago
Views: 72 • Comments: 0
Tweets: 0 • Rating: 0

Bob Dillon
REVEALED: Hundreds of Words to Avoid Using Online if You Don't Want the Government Spying on You
6 hours, 28 minutes ago
Views: 139 • Comments: 2
Tweets: 0 • Rating: 2

Learned Mother of Zion
Native Americans Have Jewish DNA!
6 hours, 48 minutes ago
Views: 260 • Comments: 9
Tweets: 1 • Rating: 6

Eclectic Infidel
Mitt Romney for a better Amercia!
6 hours, 52 minutes ago
Views: 133 • Comments: 1
Tweets: 0 • Rating: 0

 Frank says:

May your shit come to life and kiss you on the face. -- to Mrs. Gore about parental advisory labels on album covers