Created
December 25, 2011 13:55
-
-
Save ariya/1519281 to your computer and use it in GitHub Desktop.
Given a URL, get the tweeted count from the embedded Twitter widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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