Skip to content

Commit ccf363f

Browse files
committed
Merge pull request #7 from relwell/search
Add support for searching yelp
2 parents e19ec41 + f4270b6 commit ccf363f

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

yelp.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ def GetBusiness(self, id):
9191
response = json.loads(response)
9292
return Business.NewFromJsonDict(response)
9393

94+
def Search(self,
95+
#term=None,
96+
#limit=None,
97+
#offset=None,
98+
#sort=None,
99+
#category_filter=None,
100+
#radius_filter=None,
101+
#deals_filter=None,
102+
#location=None,
103+
**kwargs):
104+
response = self._FetchUrl(url="http://" + self.host + '/v2/search?' +urllib.urlencode(kwargs))
105+
response = json.loads(response)
106+
return SearchResultSet.NewFromJsonDict(response)
107+
94108
def _FetchUrl(self,
95109
url,
96110
post_data=None,
@@ -167,6 +181,24 @@ def _FetchUrl(self,
167181
# Always return the latest version
168182
return response
169183

184+
class SearchResultSet(object):
185+
186+
""" TODO make iterable, subscriptable """
187+
188+
def __init__(self,
189+
region=None,
190+
total=None,
191+
businesses=None):
192+
self.region = region
193+
self.total = total
194+
self.businesses = businesses
195+
196+
@staticmethod
197+
def NewFromJsonDict(data):
198+
return SearchResultSet(region=data.get('region', None),
199+
total=data.get('total', 0),
200+
businesses=map(Business.NewFromJsonDict, data.get('businesses', [])))
201+
170202

171203
class Business(object):
172204

@@ -231,7 +263,7 @@ def NewFromJsonDict(data):
231263
rating_img_large=data.get("rating_img_large", None),
232264
rating_img_small=data.get("rating_img_small", None),
233265
review_count=data.get("review_count", None),
234-
reviews=[Review.NewFromJsonDict(x) for x in data.get("reviews", None)],
266+
reviews=map(Review.NewFromJsonDict, data.get('reviews', [])),
235267
snippet_image_url=data.get("snippet_image_url", None),
236268
snippet_text=data.get("snippet_text", None),
237269
url=data.get("url", None))
@@ -334,17 +366,17 @@ def __init__(self,
334366
important_restrictions=None,
335367
additional_restrictions=None,
336368
options=None):
337-
self.id = id
338-
self.title = title
339-
self.url = url
340-
self.currency_code = currency_code
341-
self.time_start = time_start
342-
self.time_end = time_end
343-
self.is_popular = is_popular
344-
self.what_you_get = what_you_get
345-
self.important_restrictions = important_restrictions
346-
self.additional_restrictions = additional_restrictions
347-
self.options = options
369+
self.id = id
370+
self.title = title
371+
self.url = url
372+
self.currency_code = currency_code
373+
self.time_start = time_start
374+
self.time_end = time_end
375+
self.is_popular = is_popular
376+
self.what_you_get = what_you_get
377+
self.important_restrictions = important_restrictions
378+
self.additional_restrictions = additional_restrictions
379+
self.options = options
348380

349381
@staticmethod
350382
def NewFromJsonDict(data):

0 commit comments

Comments
 (0)