Hello,
I’m currently navigating my way through a post someone made, here. It’s about using Python along side a couple of other libraries in order to pull information from the Google My Business API and then sort it and save it. I’m tweaking and working on (expanding) this to do what I would like it to do, but before that can happen i’m running into a “TypeError: string indices must be integers” issue among a couple of others but they don’t seem to be major I don’t believe.
Traceback (most recent call last):
File "gmb.py", line 49, in <module>
main(sys.argv)
File "gmb.py", line 32, in main
'accounts/XXXXXX/locations/XXXXXX',
TypeError: string indices must be integers
Above is the full block of errors and below is the listed code.
def main(argv):
# Use the discovery doc for auth and grabbing account info
service, flags = sample_tools.init(argv, "mybusiness", "v4", __doc__, __file__, scope="https://www.googleapis.com/auth/business.manage", discovery_filename=discovery_doc)
# Get the list of accounts user has access to
output = service.accounts().list().execute()
print("List of Accounts:\n")
print(json.dumps(output, indent=2) + "\n")
firstAccount = output["accounts"][0]["name"]
# Get the list of locations for the first account in list
print("List of Locations for Account " + firstAccount)
body = {
"locationNames" [
'accounts/XXXXXX/locations/XXXXXX',
'accounts/XXXXXX/locations/XXXXXX'
]
}
locationsList = service.accounts().locations().batchGetReviews(name=firstAccount,body=body).execute()
print(json.dumps(locationsList, indent=2))
with open('gmb_batchreviews.json', 'w') as json_file:
json.dump(locationsList, json_file)
#load json file
with open('gmb_batchreviews.json') as f:
d = json.load(f)
#Clean the exported json file
df = json_normalize(d['locationReviews'])
df.to_csv('gmb_batchreviews.csv')
outfile.close()
if __name__ == "__main__":
main(sys.argv)
#filter results. Currnet date - 1
df2 = pd.read_csv('gmb_batchreviews.csv')
df2 = df2.drop(['Unnamed: 0'], axis=1)
df2['review.createTime'] = pd.to_datetime(df2['review.createTime'])
df2 = df2.set_index(['review.createTime'])
end_range = datetime.now().date()
d = d = datetime.today() - timedelta(days=90)
start_range = d.date()
df3 = df2[end_range:start_range]
#clean data for export
df3 = df3.drop(columns=['review.name', 'review.reviewer.profilePhotoUrl'])
df3.columns = ['location','comment','reply','reply_date','reviewer','review.reviewId','star_rating','date']
export_name = 'gmb_reviews_export_' + str(end_range) +'.xlsx'
df3.to_excel(export_name, index=False)
It’s my intention to eventually pull review.reviewId from the csv or json files, have that be put into one of say, 8, responses. then pick a response at random and send it as a reply.