-
Notifications
You must be signed in to change notification settings - Fork 0
/
zomatopy.py
309 lines (232 loc) · 11 KB
/
zomatopy.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import requests
import ast
base_url = "https://developers.zomato.com/api/v2.1/"
def initialize_app(config):
return Zomato(config)
class Zomato:
def __init__(self, config):
self.user_key = config["user_key"]
def get_categories(self):
"""
Takes no input.
Returns a dictionary of IDs and their respective category names.
"""
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "categories", headers=headers).content).decode("utf-8")
a = ast.literal_eval(r)
self.is_key_invalid(a)
self.is_rate_exceeded(a)
categories = {}
for category in a['categories']:
categories.update({category['categories']['id'] : category['categories']['name']})
return categories
def get_city_ID(self, city_name):
"""
Takes City Name as input.
Returns the ID for the city given as input.
"""
if city_name.isalpha() == False:
raise ValueError('InvalidCityName')
city_name = city_name.split(' ')
city_name = '%20'.join(city_name)
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "cities?q=" + city_name, headers=headers).content).decode("utf-8")
a = ast.literal_eval(r)
self.is_key_invalid(a)
self.is_rate_exceeded(a)
if len(a['location_suggestions']) == 0:
raise Exception('invalid_city_name')
elif 'name' in a['location_suggestions'][0]:
city_name = city_name.replace('%20', ' ')
if str(a['location_suggestions'][0]['name']).lower() == str(city_name).lower():
return a['location_suggestions'][0]['id']
else:
raise ValueError('InvalidCityId')
def get_city_name(self, city_ID):
"""
Takes City ID as input.
Returns the name of the city ID given as input.
"""
self.is_valid_city_id(city_ID)
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "cities?city_ids=" + str(city_ID), headers=headers).content).decode("utf-8")
a = ast.literal_eval(r)
self.is_key_invalid(a)
self.is_rate_exceeded(a)
if a['location_suggestions'][0]['country_name'] == "":
raise ValueError('InvalidCityId')
else:
temp_city_ID = a['location_suggestions'][0]['id']
if temp_city_ID == str(city_ID):
return a['location_suggestions'][0]['name']
def get_collections(self, city_ID, limit=None):
"""
Takes City ID as input. limit parameter is optional.
Returns dictionary of Zomato restaurant collections in a city and their respective URLs.
"""
#self.is_valid_city_id(city_ID)
headers = {'Accept': 'application/json', 'user-key': self.user_key}
if limit == None:
r = (requests.get(base_url + "collections?city_id=" + str(city_ID), headers=headers).content).decode("utf-8")
else:
if str(limit).isalpha() == True:
raise ValueError('LimitNotInteger')
else:
r = (requests.get(base_url + "collections?city_id=" + str(city_ID) + "&count=" + str(limit), headers=headers).content).decode("utf-8")
a = ast.literal_eval(r)
self.is_key_invalid(a)
self.is_rate_exceeded(a)
collections = {}
for collection in a['collections']:
collections.update({collection['collection']['title'] : collection['collection']['url']})
return collections
def get_cuisines(self, city_ID):
"""
Takes City ID as input.
Returns a sorted dictionary of all cuisine IDs and their respective cuisine names.
"""
self.is_valid_city_id(city_ID)
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "cuisines?city_id=" + str(city_ID), headers=headers).content).decode("utf-8")
a = ast.literal_eval(r)
self.is_key_invalid(a)
self.is_rate_exceeded(a)
if len(a['cuisines']) == 0:
raise ValueError('InvalidCityId')
temp_cuisines = {}
cuisines = {}
for cuisine in a['cuisines']:
temp_cuisines.update({cuisine['cuisine']['cuisine_id'] : cuisine['cuisine']['cuisine_name']})
for cuisine in sorted(temp_cuisines):
cuisines.update({cuisine : temp_cuisines[cuisine]})
return cuisines
def get_establishment_types(self, city_ID):
"""
Takes City ID as input.
Returns a sorted dictionary of all establishment type IDs and their respective establishment type names.
"""
self.is_valid_city_id(city_ID)
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "establishments?city_id=" + str(city_ID), headers=headers).content).decode("utf-8")
a = ast.literal_eval(r)
self.is_key_invalid(a)
self.is_rate_exceeded(a)
temp_establishment_types = {}
establishment_types = {}
if 'establishments' in a:
for establishment_type in a['establishments']:
temp_establishment_types.update({establishment_type['establishment']['id'] : establishment_type['establishment']['name']})
for establishment_type in sorted(temp_establishment_types):
establishment_types.update({establishment_type : temp_establishment_types[establishment_type]})
return establishment_types
else:
raise ValueError('InvalidCityId')
def get_nearby_restaurants(self, latitude, longitude):
"""
Takes the latitude and longitude as inputs.
Returns a dictionary of Restaurant IDs and their corresponding Zomato URLs.
"""
try:
float(latitude)
float(longitude)
except ValueError:
raise ValueError('InvalidLatitudeOrLongitude')
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "geocode?lat=" + str(latitude) + "&lon=" + str(longitude), headers=headers).content).decode("utf-8")
a = ast.literal_eval(r)
nearby_restaurants = {}
for nearby_restaurant in a['nearby_restaurants']:
nearby_restaurants.update({nearby_restaurant['restaurant']['id'] : nearby_restaurant['restaurant']['url']})
return nearby_restaurants
def get_restaurant(self, restaurant_ID):
"""
Takes Restaurant ID as input.
Returns a dictionary of restaurant details.
"""
self.is_valid_restaurant_id(restaurant_ID)
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "restaurant?res_id=" + str(restaurant_ID), headers=headers).content).decode("utf-8")
a = ast.literal_eval(r)
if 'code' in a:
if a['code'] == 404:
raise('InvalidRestaurantId')
restaurant_details = {}
restaurant_details.update({"name" : a['name']})
restaurant_details.update({"url" : a['url']})
restaurant_details.update({"location" : a['location']['address']})
restaurant_details.update({"city" : a['location']['city']})
restaurant_details.update({"city_ID" : a['location']['city_id']})
restaurant_details.update({"user_rating" : a['user_rating']['aggregate_rating']})
restaurant_details = DotDict(restaurant_details)
return restaurant_details
def restaurant_search(self, query="", latitude="", longitude="", cuisines="", limit=5):
"""
Takes either query, latitude and longitude or cuisine as input.
Returns a list of Restaurant IDs.
"""
cuisines = "%2C".join(cuisines.split(","))
if str(limit).isalpha() == True:
raise ValueError('LimitNotInteger')
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "search?q=" + str(query) + "&count=" + str(limit) + "&lat=" + str(latitude) + "&lon=" + str(longitude) + "&cuisines=" + str(cuisines), headers=headers).content).decode("utf-8")
return r#a = ast.literal_eval(r)
def get_location(self, query="", limit=5):
"""
Takes either query, latitude and longitude or cuisine as input.
Returns a list of Restaurant IDs.
"""
if str(limit).isalpha() == True:
raise ValueError('LimitNotInteger')
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "locations?query=" + str(query) + "&count=" + str(limit), headers=headers).content).decode("utf-8")
return r
def restaurant_search_by_keyword(self, query="", cuisines="", limit=5):
"""
Takes either query, latitude and longitude or cuisine as input.
Returns a list of Restaurant IDs.
"""
cuisines = "%2C".join(cuisines.split(","))
if str(limit).isalpha() == True:
raise ValueError('LimitNotInteger')
headers = {'Accept': 'application/json', 'user-key': self.user_key}
r = (requests.get(base_url + "search?q=" + str(query) + "&count=" + str(limit) + "&cuisines=" + str(cuisines), headers=headers).content).decode("utf-8")
return r
def is_valid_restaurant_id(self, restaurant_ID):
"""
Checks if the Restaurant ID is valid or invalid.
If invalid, throws a InvalidRestaurantId Exception.
"""
restaurant_ID = str(restaurant_ID)
if restaurant_ID.isnumeric() == False:
raise ValueError('InvalidRestaurantId')
def is_valid_city_id(self, city_ID):
"""
Checks if the City ID is valid or invalid.
If invalid, throws a InvalidCityId Exception.
"""
city_ID = str(city_ID)
if city_ID.isnumeric() == False:
return True# raise ValueError('InvalidCityId')
def is_key_invalid(self, a):
"""
Checks if the API key provided is valid or invalid.
If invalid, throws a InvalidKey Exception.
"""
if 'code' in a:
if a['code'] == 403:
raise ValueError('InvalidKey')
def is_rate_exceeded(self, a):
"""
Checks if the request limit for the API key is exceeded or not.
If exceeded, throws a ApiLimitExceeded Exception.
"""
if 'code' in a:
if a['code'] == 440:
raise Exception('ApiLimitExceeded')
class DotDict(dict):
"""
Dot notation access to dictionary attributes
"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__