Getting Pinterest API data

The Pinterest API has the following three main endpoints:

  • Users: Users endpoint provides a convenient way to get information about an authenticated user. It also allows you to search for boards or pins for the user.
  • Boards: Boards endpoint allows you to work with authenticated users' boards. It also provides the ability to fetch the boards and pins of any other user.
  • Pins: The Pins endpoint, besides providing a way to manage your own pins, allows you to fetch pins, boards, and the pins on a board for any user.

We create a function pinterest_paging() which will help us to deal with pagination. It will return a list with JSON elements as a result:

import requests 
 
### Pinterest API connection with paging 
def pinterest_paging(url = '', params = ''): 
 
   data = [] 
 
   response = requests.get(url = url, params = params) 
   response = response.json() 
 
   data.extend(response['data']) 
 
   while response['page']['next'] != None: 
 
         response = requests.get(url = response['page']['next']) 
 
         response = response.json() 
 
         data.extend(response['data']) 
 
   return data 

Then, we create a code which will execute the pinterest_paging() function and convert a list of JSONs into a pandas dataframe.

import pandas as pd  
from pandas.io.json import json_normalize 
 
access_token = 'YOUR_ACCESS_TOKEN' 

In the Pinterest API, we can define the fields that we want to retrieve. For the purposes of analysis we will select: ID, board, note, and counts (the full list of available fields is available in the API documentation at https://developers.pinterest.com/docs/api/overview).

fields = 'id, board, note, counts' 

We use /v1/me/pins/ endpoint to get all pins associated with our own Pinterest account:

pin_data = pinterest_paging(url = 'https://api.pinterest.com/v1/me/pins/',  
params = {'access_token': access_token, 'fields': fields}) 

Then, we convert it to a dataframe.

pins = pd.io.json.json_normalize(pin_data) 

Finally, we obtain a dataframe with the following columns:

  • board.id: Board ID
  • board.name: Board name
  • board.url: Board URL link
  • counts.comments: Number of comments
  • counts.saves: Number of saves
  • id: Pin ID
  • note: Description

It is interesting to see the boards and how many pins are linked to them. We can use the pandas functionality groupby() to print the count of pins for each board as follows:

pins.groupby('board.name')['board.name'].count() 
board.name
Data Science     34
Luxury brands    53
Tech             18  

We can see that in our account we have three boards: Data Science, Luxury brands, and Tech and we've got 105 pins in total (34 for data science board, 53 for luxury brands, and 18 for tech).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.76.89