-
Notifications
You must be signed in to change notification settings - Fork 0
/
yelp_api_pull.Rmd
124 lines (82 loc) · 2.06 KB
/
yelp_api_pull.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
---
title: "Yelp API"
author: "Gordon Blasco"
date: "1/22/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# GOAL: Set up the yelp API to get category aliases for resturaunt data
```{r}
library(tidyverse)
library(here)
library(yelp)
```
```{r}
access_token <- "add in api key from comp"
```
```{r}
raw_data <- read_csv(here("data", "master_data.csv"))
geo_raw <- read_csv(here("data", "geocode_ref.csv"))
raw_master <- raw_data %>%
left_join(geo_raw)
```
# just use a for loop...
```{r}
## prep the data
loop_prep <- raw_master %>%
#head(7) %>%
mutate(city = "Santa Barbara",
country = "US",
state = "CA",
yelp_business_id = NA)
## run the loop
loop_final <- loop_prep
for (i in 1:nrow(loop_prep)) {
#create a vector for each bit of data we need
the_name = loop_prep$Name[i]
the_city = loop_prep$city[i]
the_country = loop_prep$country[i]
the_state = loop_prep$state[i]
the_address = loop_prep$Address[i]
the_lat = loop_prep$lat[i]
the_lon = loop_prep$lon[i]
test <- yelp::business_match(
access_token = access_token,
name = the_name,
city = the_city,
country = the_country,
state = the_state,
address1 = the_address,
latitude = the_lat,
longitude = the_lon
)
b_id <- test$business_id
b_id <- if_else(is.null(b_id), "null", b_id)
loop_final$yelp_business_id[i] <- b_id
}
beezies <- loop_final %>%
filter(yelp_business_id != "null") %>%
pull(yelp_business_id)
blank_df <- yelp::business_lookup(
businesses = beezies[1],
access_token = access_token)
final_df <- blank_df %>%
filter(city == "made up town to blank df")
for (i in 1:length(beezies)) {
the_biz <- beezies[i]
df <- yelp::business_lookup(
businesses = the_biz,
access_token = access_token)
final_df <- final_df %>% bind_rows(df)
}
saveRDS(loop_final, here("data", "yelp_business_id"))
saveRDS(final_df, here("data", "yelp_categories"))
```
```{r}
#
# yelp_categories <- yelp::categories("appliances",
# access_token = access_token
# )
```