Feb 11th 2010 × Get a url’s tweet / retweet count via tweetmeme api and mootools JSONP
I needed to get retweet counts as data, as opposed embedding the standard widget that tweetmeme supply.
Turning to their API instead got me joy initially. After a stressful few minutes with a javascript label exception was taking place, it transpired that the tweetmeme API was not returning valid JSON within i’s deignated callback wrapper. Instead, it came back as simple JSON. I was about to give up and write a PHP/curl wrapper when I noticed their ‘format’ is called jsonc whereas I was requesting ‘json’. Oh well, works fine now.
Through extending the mootools’ Request.JSONP class, it’s a manner of seconds to get things going:
Request.tweetmeme = new Class({
// return json data with extended information of a place / location.
Extends: Request.JSONP,
options: {
url: "http://api.tweetmeme.com/url_info.jsonc?url={location}"
},
initialize: function(location, options) {
this.parent(options);
this.options.url = this.options.url.substitute({location: location});
},
success: function(data, script) {
this.parent(data, script);
}
});
// example use
// get retweets for all posts on a page and update the counter. URL in the rel property of the markup.
$$("div.twitterBar").each(function(el) {
var url = el.get("rel");
new Request.tweetmeme(url, {
log: true,
onSuccess: function(data) {
el.set("html", data.story.url_count);
}
}).send();
});
I hope this helps somebody – once again, how easy it is to extend JSONP and get things done in mootools.
2 Comments to Get a url’s tweet / retweet count via tweetmeme api and mootools JSONP
A tiny tiny error is causing an error in IE7.
There is one “,” too much at the end of line:
url: “http://api.tweetmeme.com/url_info.jsonc?url={location}”,.
Firefox is not so picky, but IE 7 is
Removing the “,” will fix it.
Thanks for the wonderfull script!
oh thanks, been meaning to fix this for a while (I noticed it when combining my set of social networking set tools for http://dci.uk.net/ – cheers)
Would you like to leave a comment?
You must be logged in to post a comment.




