I’ve been playing with programmatically fetching screenshots for years but never felt happy with the end result. Using PhantomJS directly seemed cumbersome and things didn’t work reliably for me when they reached production. It was whilst playing with Aesthetic that I learned how easy it was to use Capybara outside of a test environment as a tool for saving screenshots — I was struck by a thought:
My next screenshot-fetching app will use PhantomJS via Capybara and Poltergeist and will be just great!
— Me, before I’d written any code.
So I wrote a Rails app that receives a request, creates a database record, returns an identifier, queues up a job, fetches the screenshot and writes it to a file, and finally notifies the requesting app via a webhook that the screenshot is ready. It did seem a bit much for such a “simple” thing but I was quite pleased with it — and webhooks are cool. As usual it brought my server to its knees.
I started wondering about using Heroku which made me think that being synchronous might be the way to go — as in, the motivation was mostly $$$-driven… Anyway, I rewrote it again*.
It’s called Screamshot and can be found on GitHub, this time it’s synchronous and built on Sinatra and PhantomJS (via Capybara/Poltergeist).
Here’s how to use Screamshot:
$ curl "http://API_TOKEN@screamshot.dev/screenshot?url=http://example.com"
It has plenty of room for improvement: it doesn’t appreciate slow ad servers and therefore often times out, the fonts don’t look great, and it’s currently utilising the limited concurrency of WEBrick.
But I still like it*** — and you might too.
* To be honest part of my motivation for making this again is because I like the idea** for the logo!
** I said I like the idea for the logo…
*** Again, mainly just the logo and colour scheme.