Harnessing Recursion to Retrieve all Reviews.

A Google My Business Example with Python

Image for post
Image for post
Photo by Laurie Byrne on Unsplash

Obtaining Tokens and Ids

Using this resource you should be able to access all the info you need for the following code to work and the ideas to make sense. The resource linked is an excellent document that has much to offer including how to access the OAuth 2.0 Playground and make ‘test’ requests to the API. We will be using the OAuth 2.0 Playground to grab location and account identifiers and also temporary authorisation tokens that allow us access to the API for a time-limited period.

# Cell missing data intentionally
headers = {'Authorization': ''}
accountId = None
locationId = None
headers = {
'Authorization':
'Bearer 1hsdf0983223ladf.'xcvlwqr0i3098dfglm34eoj234rkjdfsav-90fdgfgfjfdlgkja'}

Obtaining Reviews by Recursion

Now the good part. You have reviews, and perhaps they total more than 50. The default and maximum pageSize is 50 so then it is certain that the API will show you only the first page. Therefore we can implement a recursive function to retrieve all the reviews spread across multiple pages. I will show by using the learning convention “Do it for one, then do it for all”.

Retrieve the first page of reviews:

We will need the requests library.

pip install requests import requests# Cell missing data intentionally
headers = {'Authorization': ''}
accountId = None
locationId = None
URI = f'https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews'r = requests.get(URI, headers=headers)
print(f'status code: {r.status_code}')

result = r.json()
result
Image for post
Image for post
Response body for the review list request.
Image for post
Image for post
Quer Parameters for locations reviews list
URI = f'https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews?pageToken='

Retrieving all reviews with recursion

If you’ve not been exposed to recursion or don’t have a good handle on how it works you might want to read this article about it before going forward.

import pandas 
import time
import requests
# Cell missing data intentionally
headers = {'Authorization': ''}
accountId = None
locationId = None
URI = f'<https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews>'
# Automate data extraction using recursive function:
def reviews_to_df(headers, accountId, locationId, df, URI):
r = requests.get(URI, headers=headers)
print(f'status code: {r.status_code}')
time.sleep(3)
result = r.json()
new_df = df.append(result['reviews'])
print(f'the shape of the df is now: {new_df.shape}')
if len(result['reviews']) != 50:
return new_df
else:
nextPage = result['nextPageToken']
URI = f'<https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews?pageToken={nextPage}>'
return reviews_to_df(headers, accountId, locationId, new_df, URI)
empty_df = pd.DataFrame()
df = reviews_to_df(headers, accountId, locationId, empty_df, URI)

Explanation:

The reviews_to_df() function takes in the parameters that we talked about headers, Ids, URI, and an empty data frame.

Conclusion

Taking the time to read about recursion has helped me a great deal and I was so glad to be able to break through the frustration of obtaining these reviews and accomplish a handy recursive function to boot! I hope this has helped streamline the process for you and if you’re still reading, well done for making it this far! Please leave a clap and a comment and thank you for your time!

References

  1. Using Google Cloud and My Business platforms to obtain reviews for a locationalgakovic.medium.com
  2. Basic Setup — Get Started Guide Google My Business API
  3. Requests library — Docs
  4. Reviews list method — Google My Business API Docs
  5. Recursion in Pythonalgakovic.medium.com

Written by

Practicing Data Scientist. Interested in Games, Gamification, Ocean Sciences, Music, Biology.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store