-
Notifications
You must be signed in to change notification settings - Fork 0
/
yelp_data.py
73 lines (47 loc) · 1.57 KB
/
yelp_data.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
"""
Client for using Yelp API
"""
import requests
import time
class YelpClient(object):
def __init__(self, access_token=None):
if access_token:
self.access_token = access_token
self.s = requests.Session()
self.s.headers.update({'Authorization': 'Bearer {}'.format(self.access_token)})
def create_access_token(self, client_id, client_secret):
"""
Create access token
"""
data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
}
r = requests.post('https://api.yelp.com/oauth2/token', data=data)
self.access_token_raw = r.json()
self.access_token = self.access_token_raw['access_token']
return self.access_token
def get_business_by_phone(self, phone_number):
"""
Search businesses by phone number
"""
print phone_number
r = self.s.get('https://api.yelp.com/v3/businesses/search/phone', params={'phone': phone_number})
time.sleep(5)
return r.json()
def get_business_by_location(self, name, latitude, longitude):
"""
Search businesses by term and location
"""
print name, latitude, longitude
params = {
'term': name,
'latitude': latitude,
'longitude': longitude,
}
r = self.s.get('https://api.yelp.com/v3/businesses/search', params=params)
time.sleep(5)
return r.json()
if __name__ == '__main__':
main()