-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyelp_api.py
More file actions
executable file
·92 lines (64 loc) · 2.31 KB
/
yelp_api.py
File metadata and controls
executable file
·92 lines (64 loc) · 2.31 KB
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
# -*- coding: utf-8 -*-
from urllib.error import HTTPError
from urllib.parse import quote
from urllib.parse import urlencode
import requests
#%%
# DEFINITIONS
# Insert own key and client values below
API_KEY = '#####'
Client_ID = '#####'
API_HOST = 'https://api.yelp.com'
SEARCH_PATH = '/v3/businesses/search'
BUSINESS_PATH = '/v3/businesses/'
#%%
# GitHub definitions:
# main request set-up, this formats the keys and is used in the next function to send the request
def request(host, path, api_key, url_params=None):
"""Given your API_KEY, send a GET request to the API.
Args:
host (str): The domain host of the API.
path (str): The path of the API after the domain.
API_KEY (str): Your API Key.
url_params (dict): An optional set of query parameters in the request.
Returns:
dict: The JSON response from the request.
Raises:
HTTPError: An error occurs from the HTTP request.
"""
url_params = url_params or {}
url = '{0}{1}'.format(host, quote(path.encode('utf8')))
headers = {
'Authorization': 'Bearer %s' % api_key,
}
print(u'Querying {0} ...'.format(url))
response = requests.request('GET', url, headers=headers, params=url_params)
return response.json()
# tweaked request for the business search
def business(venue):
url_params = {
'locale':'en_GB'
}
url = '{0}{1}{2}'.format(API_HOST, quote(BUSINESS_PATH.encode('utf8')), venue)
headers = {
'Authorization': 'Bearer %s' % API_KEY,
}
print(u'Querying {0} ...'.format(url))
response = requests.request('GET', url, headers=headers, params=url_params)
return response.json()
# business search
def search(location, category, limit, offset):
"""Query the Search API by a search term and location.
Args:
term (str): The search term passed to the API.
location (str): The search location passed to the API.
Returns:
dict: The JSON response from the request.
"""
url_params = {
'location': location.replace(' ', '+'),
'categories': category,
'limit': limit,
'offset': offset
}
return request(API_HOST, SEARCH_PATH, API_KEY, url_params=url_params)