Using the Pinterest API to find duplicate pins


Post Image

Pinterest is a popular social media platform, and a great way to help you reach more people. But at some point you will find yourself faced with a build up of dead links and duplicate pins.

So I googled “find duplicate pins on pinterest” and found what looked to be a pretty handy tool called Pin4Ever. My only issue was that I couldn’t warrant spending $50 a year on something I probably wouldn’t use that much. Then I remembered that Pinterest had an API, so I figured I would just make a script myself to find duplicate pins, and maybe also find pins with dead links.

Pinterest API issues

While playing with the API, there were a few problems I discovered. Nothing major, but they certainly became slight irritations. More so during the creating and testing!

API LIMITS

The first issue I found was that the API limit of 1,000 calls per hour could be quite frustrating. Especially when putting a script together and testing it. One wrong php loop and you’ve blown all 1,000 of them by mistake meaning you then have to wait an hour before trying again.

This happened quite a few times while trying to figure out how to return all the pins in a board since the only way to do so through the API was with a “next” url which is outlined below.

API PAGINATION

The next problem was returning all the pins from a board. The maximum number of pins you can return is 100. So if a board had 600+ pins, how do you get them all?

Well the kind folks at pinterest basically return a “Next page” url you can use to get the next page of results. This for me was the most frustrating part and required a recursive function that basically pushed the pins into an array and if a “next” url was found repeat the process over and over until there were no pages (or pins) left to find.

The function I ended up using was:


function findchildren(&$array, $i){
global $pins;
if (true == $i){return;}
foreach($array as $k=>$v){
array_push($pins, $v['data']);
if(!empty($v['page']['next'])){
$newar = multiRequest(array(pinterest('getboardpins2',$v['page']['next'])));
findchildren($newar, false);
}
}
}

pinterest() is just a function I created to build the GET/POST/PATCH/DELETE urls. And multiRequest was a function for multiple cURL (or singular).

The way I called this on the page was:


$pins = array();
$allboards = multiRequest(array('https://api.pinterest.com/v1/boards/<MYBOARDID>/pins/?access_token=<MYACCESSTOKEN>&fields=id%2Clink%2Cnote%2Curl%2Coriginal_link%2Cimage%2Cboard%2Ccreated_at&limit=100'));
findchildren($allboards, false);

This feeds the first page of a board to the findchildren function and if there is another page found, it will loop again and also push all the pins into the pin array. I put the pins in the database so I didn’t have to keep getting them with the API.

API Tips

If you’re using the Pinterest API it would be a VERY good idea to save data locally (i.e in your database) as this will cut down on the number of API calls you need to make. It also makes it easier to compare all your pins to find duplicates ect since you will be querying your database, not the API.

If using the delete function of the API be careful as you may accidentally delete all the pins in a board. Again, playing with php loops caused this to happen a good few times (oops).

Other uses for the Pinterest API

There are so many ways you can use the API. For a typical user, it can be a relatively simple way to manage your pins, find duplicates and also uncover dead links using cURL or PHP’s get_headers() to check the pins URL is returning a 200 status code.

If you’re only posting/comparing against items in your sites database (maybe you’re an online store and posting new items to pinterest??), your script could simply sync with your database.

Once you’ve located and fixed any duplicates, the rest will be easy to keep updated. If your product, service, post (or what ever) is active, the link wouldn’t be dead. Then if you remove anything from your site, you just use the API to delete it from pinterest.

Download my free PHP pinterest API script to save your boards and pins locally, find duplicate pins and also see if any pins have dead links!