Automate Pinterest Pinning with CasperJS
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:
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).