Data, Maps, Usability, and Performance

Automate Pinterest Pinning with CasperJS

Last updated on April 22, 2015 in Development

Auto Pin on Pinterest Script

How do you pin photos on Pinterest automatically? I have covered some elements of the Pinterest API before, but I did not expose the endpoints necessary to pin via the API. None of that is yet publicly available but it got me thinking about doing more programmatically on Pinterest in general. What would it take to automate the process of pinning an image automatically? I think a simple solution is to use a headless browser and recreate the steps to make that happen. Lets go over a simple script that will login into Pinterest, retrieve an image from an external source, add pin it to a board with a pin description.

We are going to use PhantomJS as the headless browser, CasperJS as a nice utility to make working with PhantomJS easier, and Node to run CasperJS. If you are on a Mac, grab homebrew and install all 3 with these simple commands:

brew install node
brew install phantomjs
brew install casperjs

Now you are ready to make that headless browser do anything you want. Here is the full script that goes through Pinterest login authentication, finds an image via external url, selects a board, adds description, and pins the picture on Pinterest:

Since you can’t see what PhantomJS is doing, I like to debug with “debug” loglevel and screenshots (commented out). The code is pretty straightforward, we first define some browser settings like viewport size, useragent, etc. We navigate to the login page and submit the form (put in your own credentials). We then go to the “find pin by url” page and start clicking around. Pinterest uses a lot of JavaScript and DOM manipulation so I had to setup custom timeouts to wait for certain elements to appear on the browser screen (things like modal or board selection dropdown get inserted into the DOM with JavaScript).

As the code shows, we click on the pin it button which pops a modal. We click on “select a board” dropdown which pops a list of boards. We use nth-child selector to select a specific board, and finally we add a description and submit the form. I wait 5 seconds and take another screenshot in the end just to verify everything worked (and to generate an image for this article).

Tags: , , , , ,

Facebook Twitter Hacker News Reddit More...