Skip to content

Instantly share code, notes, and snippets.

@ariya
Created Dec 25, 2011
Embed
What would you like to do?
Given a URL, get the tweeted count from the embedded Twitter widget
// Given a URL, get the tweeted count from the embedded Twitter widget.
function findTweetCount(url, callback) {
var count = -1,
title = '';
function openWidget(location) {
var widget = require('webpage').create();
widget.open(location, function (status) {
if (status !== 'fail') {
count = widget.evaluate(function () {
return parseInt(document.querySelector('a#count').textContent, 10);
});
}
callback.call(this, count, title);
});
}
function openPage() {
var page = require('webpage').create();
page.settings.loadImages = false;
page.settings.loadPlugins = false;
page.open(url, function (status) {
if (status === 'fail') {
callback.call(this, count, title);
} else {
title = page.evaluate(function () {
return document.title;
});
openWidget(page.evaluate(function () {
return document.querySelector('iframe.twitter-share-button').src;
}));
}
});
}
openPage();
}
if (phantom.args.length !== 1) {
console.log('Usage:');
console.log(' tweetcount.js url');
console.log();
phantom.exit();
}
findTweetCount(phantom.args[0], function (count, title) {
if (count < 0) {
console.log('Error loading ' + title);
} else {
console.log(count + ' ' + title);
}
phantom.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment