I've a data frame with locations and I want to append phone numbers and websites to them from Google Places in a pipeable solution.
The closest I've gotten to doing this was using googleway
, unlisting the JSON and using regexes to extract the place id, and doing the same again for the phone numbers and email addresses. Is there a more targeted way of doing this?
library(googleway)
library(tidyverse)
set_key("api")
index_no <- c(1,2,3,4)
landmark<- c("Sydney Opera House","Eiffel Tower","Empire State Building","Big Ben")
df <- data.frame(index_no,landmark, stringsAsFactors = F)
df %>%
rowwise() %>%
# Place IDs are required for the function beneath
do(data.frame(., place_id = unlist(google_places(search_string = .$landmark)))) %>%
# Place IDs are 27 chars
filter(grepl("^\\S{27}$", place_id )) %>%
do(data.frame(., details = unlist(google_place_details(place_id = .$place_id )))) %>%
unique() %>%
# Gets non-Google URls and Phone Numbers
filter(grepl("(?!.*(google|maps))(^https?.*|^\\+\\d)", details, perl = T )) %>%
group_by(landmark) %>%
mutate(seq = 1:n()) %>%
spread(seq, details) %>%
rename(phone_number = `1`, website = `2`) %>%
select(-place_id) %>%
ungroup()
I doubt there is. It is a two step process at the side of Google API (see the documentation: https://developers.google.com/places/web-service/details):
You need to :
placeid
(via Place Search call)placeid
(via Place Details call)You might have more control over the process if you pasted your url yourself and executed it via httr (the Google API is known for breaking changes, and hard for R packages to keep pace with) and you can wrap the ugly piece of code in your own function, making the call tidy - but ultimately it will have to be 2 API calls.