/
api_business.py
163 lines (148 loc) · 4.6 KB
/
api_business.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
from common_word_list import blacklist
from datetime import datetime
from google.appengine.api import taskqueue
from google.appengine.ext import db
import api_utils
import geo.geohash as geohash
import json
import levr_classes as levr
import logging
import random
import webapp2
from tasks import INCREMENT_DEAL_VIEW_URL
class FindABusinessHandler(api_utils.BaseClass):
'''
A handler for searching for a specific venue
'''
@api_utils.validate(None, 'param',
user = False,
levrToken = False,
# fields to identify a business
foursquareID = False,
businessName = False,
vicinity = False,
geoPoint = True,
radius = False,
)
def get(self,*args,**kwargs):
'''
@return: <business>
'''
logging.info(kwargs)
business_name = kwargs.get('businessName')
vicinity = kwargs.get('vicinity')
geo_point = kwargs.get('geoPoint')
radius = kwargs.get('radius') # TODO: implement radius to search
try:
# create a set of geo hashes to search based on the radius
search = api_utils.Search(True)
precision = 5
ghash_list,bounding_box = search.create_ghash_list(geo_point, precision)
logging.info(ghash_list)
# get deal keys
for ghash in ghash_list:
business_keys = levr.Business.all(keys_only=True
).filter('geo_hash_prefixes',ghash
).fetch(None)
# fetch all businesses
businesses = db.get(business_keys)
# filter the businesses by the tags
search_tags = set([])
if business_name:
search_tags.update(levr.create_tokens(business_name))
if vicinity:
search_tags.update(levr.create_tokens(vicinity))
logging.info(search_tags)
# map the quality of the match by the number of tags that match the business
for business in businesses:
business_tags = business.tags
business.rank = 0
for tag in business_tags:
if tag in search_tags:
business.rank += 1
# assure that a business was found
assert businesses, 'Could not find a business'
# sort the businesses by their quality
ranks = [b.rank for b in businesses]
toop = zip(ranks,businesses)
toop.sort()
ranks,businesses = zip(*toop)
# get the highest ranking business
business = businesses[0]
# get all deals from that business
deals = levr.Deal.all(keys_only=True
).ancestor(business.key()
).filter('deal_status',levr.DEAL_STATUS_ACTIVE
).fetch(None)
packaged_deals = api_utils.package_deal_multi(deals)
packaged_business = api_utils.package_business(business)
response = {
'business' : packaged_business,
'deals' : packaged_deals
}
self.send_response(response)
except AssertionError,e:
self.send_error(e)
except Exception,e:
levr.log_error()
self.send_error()
class ViewABusinessHandler(api_utils.BaseClass):
'''
A handler to view the deals at a specific venue
'''
@api_utils.validate(None, 'param',
user = False,
levrToken = False,
business = True
)
def get(self,*args,**kwargs):
user = kwargs.get('actor')
business = kwargs.get('business')
development = kwargs.get('development',False)
try:
deals = api_utils.fetch_all_businesses_deals(business, development)
# TODO: take another look at the business packaging for the api
# package.
packaged_deals = api_utils.package_deal_multi(deals, False)
packaged_business = api_utils.package_business(business)
# respond
response = {
'deals' : packaged_deals,
'business' : packaged_business
}
self.send_response(response)
except:
levr.log_error()
self.send_error()
class FindABusinessFromFourquareHandler(api_utils.BaseClass):
@api_utils.validate(None, 'param',
user = False,
levrToken = False,
foursquareID = True
)
def get(self,*args,**kwargs):
user = kwargs.get('actor')
levrToken = kwargs.get('levrToken')
foursquare_id = kwargs.get('foursquareID')
development = kwargs.get('development')
try:
business = levr.Business.all().filter('foursquare_id',foursquare_id)
deals = api_utils.fetch_all_businesses_deals(business, development)
# package.
packaged_deals = api_utils.package_deal_multi(deals, False)
packaged_business = api_utils.package_business(business)
# respond
response = {
'deals' : packaged_deals,
'business' : packaged_business
}
self.send_response(response)
except Exception,e:
levr.log_error(e)
self.send_error()
FIND_A_BUSINESS_URL = '/api/business/find'
VIEW_A_BUSINESS_URL = '/api/business/view'
app = webapp2.WSGIApplication([
(FIND_A_BUSINESS_URL, FindABusinessHandler),
(VIEW_A_BUSINESS_URL, ViewABusinessHandler),
],debug=True)