-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_yelp.py
62 lines (46 loc) · 2.06 KB
/
request_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
import requests
import os
import json
import pprint
from random import choice
def get_restaurant(location):
token_url = 'https://api.yelp.com/oauth2/token'
payload = { 'grant_type': 'client_credentials',
'client_id': os.environ['YELP_CLIENT_ID'].rstrip(),
'client_secret': os.environ['YELP_CLIENT_SECRET'].rstrip()}
print payload
response = requests.post(token_url, data=payload)
if response.status_code != 200:
print "Error Status {} - {}".format(response.status_code, response.reason)
return;
token = response.json()
print response
access_token = token['access_token']
headers = {'Authorization': 'Bearer ' + access_token} # authentication information will be in header
params = dict(term='restaurants', location=location, radius=40000) # search conditions
response = ""
while response == "":
try:
response = requests.get('https://api.yelp.com/v3/businesses/search',
params=params,
headers=headers) # get businesses from keyword search
except:
time.sleep(5)
continue
result = response.json()
print result
result_chosen = choice(result['businesses'])
result_chosen_coordinates = result_chosen['coordinates']
i = 0
while result_chosen['coordinates']['latitude'] == None or result_chosen['coordinates']['longitude'] == None and i < 10:
result_chosen = choice(result['businesses'])
i += 1
if result_chosen['coordinates']['latitude'] == None or result_chosen['coordinates']['longitude'] == None:
result_chosen['coordinates']['latitude'] = 37.7916357455614
result_chosen['coordinates']['longitude'] = -122.403691572642
result_chosen_id = result_chosen['id']
result_chosen_name = result_chosen['name']
result_chosen_location = result_chosen['location']['display_address'][0]
result_chosen_location2 = result_chosen['location']['display_address'][1]
result_chosen_image = result_chosen['image_url']
return [result_chosen_id, result_chosen_name, result_chosen_location, result_chosen_location2, result_chosen_coordinates, result_chosen_image]