Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
screenshot all my favorite tweets
// use in console: copy(getUrls())
function getUrls() {
var $urls = $('.tweet-timestamp').map(function(i, el) {
return $(el).attr('href')
});
var urls = [];
for (var i = 0; i < $urls.length; ++i) {
urls.push('http://twitter.com'+$urls[i])
}
urls.reverse()
return JSON.stringify(urls)
}
// in phantomjs:
function pad(n, width, z) {
//pad(9, 4); // 0009
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
var webpage = require('webpage')
var fs = require('fs');
var favoriteUrls = JSON.parse(fs.read('favorites.json'));
function download(i) {
console.log(i)
var page = webpage.create();
console.log('opening page', favoriteUrls[i])
page.open(favoriteUrls[i], function() {
page.includeJs("https://code.jquery.com/jquery-1.11.3.min.js", function() {
var coordinates = page.evaluate(function() {
console.log('removing crap and getting coordinates')
function removecrap() {
$('.follow-bar').remove();
$('.stream-item-footer').remove();
$('.js-tweet-stats-container.tweet-stats-container').remove();
}
function getcoordinates() {
var $container = $('.permalink-inner').not('.in-reply-to');
var x = $container.offset().left
var y = $container.offset().top
var width = $container.width();
var height = $container.height();
// when a tweet is a reply, start screenshot there
var $replies = $('.permalink-in-reply-tos')
if ($replies.length > 0) {
y = $replies.offset().top
height += $replies.height()
}
var debug = false;
if (debug) {
var $vis = $('<div>').css({
position: 'absolute',
zindex: 400000000,
top: y,
left: x,
outline: '1px solid red',
height: height,
width: width,
});
$('body').append($vis);
}
return {
top: y,
left: x,
width: width,
height: height,
}
}
removecrap()
return getcoordinates();
});
page.clipRect = coordinates
var paddedn = pad(i, 3);
console.log('taking screenshot')
page.render('screenshots/'+paddedn+'.png')
if (i >= favoriteUrls.length-1) {
phantom.exit()
} else {
page.close()
download(i+1)
}
});
});
}
download(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.