forked from March-Hackbright/good-streets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yelp.py
253 lines (163 loc) · 6 KB
/
yelp.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Yelp API calls for business ids and reviews. Cap is 25,000 calls per day."""
import os
import requests
# Call limit to yelp api is 25,000/day
SECRET = os.environ["YELP_SECRET"]
Y_ID = os.environ["YELP_ID"]
API_ROOT = "https://api.yelp.com/v3/"
def obtain_bearer_token():
"""Request authorization tokens"""
endpoint = "https://api.yelp.com/oauth2/token"
response = requests.post(endpoint, data={"client_secret": SECRET,
"client_id": Y_ID,
"grant_type": "client_credentials"})
bearer_token = response.json()['access_token']
return bearer_token
def get_header():
"""get header and token information"""
token = obtain_bearer_token()
headers = {"Authorization": 'Bearer {}'.format(token)}
return headers
def yelp_information(business_id):
"""Returns image, rating, and open hours when given a business_id"""
endpoint = API_ROOT + "businesses/{}".format(business_id)
response = requests.get(endpoint, headers=get_header())
info = response.json()
yelp_info = {}
yelp_info["rating"] = info["rating"]
try:
yelp_info["open_now"] = info['hours'][0]["is_open_now"]
yelp_info["opens"] = info['hours'][0]["open"][0]['start']
yelp_info["closes"] = info['hours'][0]["open"][0]['end']
except KeyError:
yelp_info["open_now"] = ''
yelp_info["opens"] = ''
yelp_info["closes"] = ''
### to-do: key error if hours are not included.
print yelp_info
return yelp_info
def get_yelp_reviews(business_id):
"""Given a business_id returns yelp reviews
yelp_reviews[reviews{user: "Jane Reviewer",
text: "This place is awesome!",
rating: 5,
url: "www.yelp.com/rest_of_url"}]
"""
endpoint = API_ROOT + "businesses/{}/reviews".format(business_id)
response = requests.get(endpoint, headers=get_header())
information = response.json()
review_list = information['reviews']
i = 0
yelp_reviews = []
while i < len(review_list):
reviews = {}
name = review_list[i]['user']['name']
reviews['name'] = name
text = review_list[i]['text']
reviews['text'] = text
rating = review_list[i]['rating']
reviews['rating'] = rating
url = review_list[i]['url']
reviews['url'] = url
yelp_reviews.append(reviews)
i += 1
return yelp_reviews
def get_police_departments(center_lat=37.7749, center_lng=-122.4194, radius=100):
"""Get the business id for each business"""
endpoint = API_ROOT + "businesses/search"
data = {"categories": "policedepartments",
"latitude": 37.7749,
"longitude": -122.4194,
"limit": 50,
}
response = requests.get(endpoint, params=data, headers=get_header())
business = response.json()
result_list = []
for b in business['businesses']:
police_departments = b['id']
lat = b['coordinates']['latitude']
lng = b['coordinates']['longitude']
data = yelp_information(police_departments)
data['category'] = "police department"
data['lat'] = lat
data['lng'] = lng
result_list.append(data)
print result_list
return result_list
def get_self_defense(center_lat=37.7749, center_lng=-122.4194, radius=100):
"""Get self-defense studios in the area"""
endpoint = API_ROOT + "businesses/search"
data = {"categories": "martialarts",
"latitude": center_lat,
"longitude": center_lng,
"radius": 100,
"limit": 10,
}
response = requests.get(endpoint, params=data, headers=get_header())
business = response.json()
results = []
for b in business['businesses']:
self_defense = b['id']
lat = b['coordinates']['latitude']
lng = b['coordinates']['longitude']
data = yelp_information(self_defense)
data['lat'] = lat
data['lng'] = lng
data['category'] = "self-defense"
results.append(data)
return results
def get_bars(center_lat=37.7749, center_lng=-122.4194, radius=100):
"""Get bars in the area"""
endpoint = API_ROOT + "businesses/search"
data = {"categories": "bars",
"latitude": center_lat,
"longitude": center_lng,
"radius": 100,
"limit": 10,
}
response = requests.get(endpoint, params=data, headers=get_header())
business = response.json()
results = []
for b in business['businesses']:
bars = b['id']
lat = b['coordinates']['latitude']
lng = b['coordinates']['longitude']
data = yelp_information(bars)
data['lat'] = lat
data['lng'] = lng
data['category'] = "bar"
results.append(data)
return results
def get_restaurants(center_lat=37.7749, center_lng=-122.4194, radius=100):
"""Get restaurants in the area"""
endpoint = API_ROOT + "businesses/search"
data = {"categories": "restaurants",
"latitude": center_lat,
"longitude": center_lng,
"radius": 100,
"limit": 10,
}
response = requests.get(endpoint, params=data, headers=get_header())
business = response.json()
results = []
for b in business['businesses']:
bars = b['id']
lat = b['coordinates']['latitude']
lng = b['coordinates']['longitude']
data = yelp_information(bars)
data['lat'] = lat
data['lng'] = lng
data['category'] = "restaurants"
results.append(data)
return results
################################################################################
if __name__ == "__main__":
# from server import app
# connect_to_db(app)
obtain_bearer_token()
get_police_departments()
# get_self_defense()
get_bars()
get_restaurants()
# yelp_information(business_id)
# get_yelp_reviews(business_id)