-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
204 lines (182 loc) · 7.91 KB
/
admin.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
#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from django.utils import simplejson as json
from google.appengine.api import urlfetch
from gheatae.point import DataPoint
import urllib2
import oauth2
import logging
import time
import tile
import data
import datetime
import categories
import neighborhoods
import metros
import gmerc
import math
import gheatae.tile
yelp_apiv1_key = "piKGKH80o2c9pkXIsXaBmg"
consumer_key = "RPOw3zFwkJZlxyEWf-oozA"
consumer_secret = "zMOd-b6Pu4BGxlxkartDXJphbS8="
token = "nD8eT17aqYqnCrFSUeuRODR7JktmrwBu"
token_secret = "kUygrJkZkFoBbRIgqKYWyBc91Gg="
class ImageHandler(webapp.RequestHandler):
def get(self):
zoom = self.request.get('zoom')
x = self.request.get('x')
y = self.request.get('y')
result = db.GqlQuery("SELECT * FROM TileImage WHERE zoom = :1 AND x = :2 AND y = :3 AND categories = :4 LIMIT 1" , zoom, x, y, categories).fetch(1)
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(result[0].picture)
class GenTilesHandler(webapp.RequestHandler):
def get(self):
for metro_id, metro in metros.metro_list.items():
s = metro['lat']['start'] - .1
w = metro['lng']['start'] - .1
n = metro['lat']['end'] + .1
e = metro['lng']['end'] + .1
for zoom in [13, 14]:
x1, y1 = gmerc.ll2px(n, w, zoom)
x2, y2 = gmerc.ll2px(s, e, zoom)
startx = int(math.ceil(x1 / 256.0))
starty = int(math.ceil(y1 / 256.0))
endx = int(math.ceil(x2 / 256.0))
endy = int(math.ceil(y2 / 256.0))
for x in [startx, endx]:
for y in [starty, endy]:
genTile(zoom, x, y, metro_id)
self.response.out.write('Starting at: %d, %d, ending at: %d, %d for zoom: %d\n' % (startx, starty, endx, endy, zoom))
def genTile(zoom, x, y, metro_id):
for key, value in categories.category_list.items():
gheatae.tile.Tile(0, zoom, x, y, key, metro_id).image_out()
class DeleteHandler(webapp.RequestHandler):
def get(self):
if (self.request.get('points')):
q = db.GqlQuery("SELECT * FROM DataPoint")
results = q.fetch(10000)
for result in results:
result.delete()
if (self.request.get('tiles')):
q = db.GqlQuery("SELECT * FROM TileImage")
results = q.fetch(1000)
for result in results:
result.delete()
class GatherHandler(webapp.RequestHandler):
def get(self):
offset = int(self.request.get('start'))
finish = int(self.request.get('finish'))
metro_index = int(self.request.get('metro'))
# category_index = int(self.request.get('category'))
total = 0
while (finish > offset):
response = getBars(offset, metro_index) #, category_index)
if 'total' in response:
bars = response['businesses']
total = response['total']
else:
self.response.out.write(response)
break
for bar in bars:
#bar_categories = map(lambda value:value[0], bar['categories'])
#if (not getCategory(category_index) in bar_categories):
# logging.info(bar['name'] + ':' + getMetro(metro_index) + ' is not a ' + getCategory(category_index))
createPlace(bar, metro_index)
offset += 20
time.sleep(2)
self.response.out.write(total)
def getBars(offset, metro_index):
global token
args = { 'location': getMetro(metro_index), 'category_filter': 'nightlife', 'limit': 20, 'offset': offset }
url = 'http://api.yelp.com/v2/search?'
consumer = oauth2.Consumer(consumer_key, consumer_secret)
oauth_request = oauth2.Request('GET', url, args)
oauth_request.update({'oauth_nonce': oauth2.generate_nonce(),
'oauth_timestamp': oauth2.generate_timestamp(),
'oauth_token': token,
'oauth_consumer_key': consumer_key})
new_token = oauth2.Token(token, token_secret)
oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, new_token)
signed_url = oauth_request.to_url()
# logging.info('Signed URL: %s' % signed_url)
# Connect
try:
conn = urllib2.urlopen(signed_url, None)
try:
response = json.loads(conn.read())
finally:
conn.close()
except urllib2.HTTPError, error:
response = json.loads(error.read())
return response;
def createPlace(bar, metro_index):
if not 'neighborhoods' in bar['location'] or not 'categories' in bar:
return
lat = bar['location']['coordinate']['latitude']
lng = bar['location']['coordinate']['longitude']
neighborhood = bar['location']['neighborhoods'][0]
bar_categories = map(lambda value:value[0], bar['categories'])
rating = determineRating(bar['rating_img_url'])
weight = rating
the_range = 5.
if (not neighborhood in neighborhoods.inv_neighborhood_list):
neighborhood_index = -1
# logging.warn('Dumping neighborhood: %s' % neighborhood)
# return
else:
neighborhood_index = neighborhoods.inv_neighborhood_list[neighborhood]
for category in bar_categories:
if (category in categories.inv_category_list):
category_index = categories.inv_category_list[category]
new_data = DataPoint(location=db.GeoPt(float(lat), float(lng)),
time=datetime.datetime.now(),
weight=weight,
range=the_range,
category = category_index,
neighborhood = neighborhood_index,
url = bar['url'],
rating = rating,
placeName = bar['name'],
metro = metro_index)
# logging.info('Adding name: %s, category: %s, metro: %s' % (bar['name'], getCategory(category_index), getMetro(metro_index)))
new_data.update_location()
new_data.put()
def determineRating(url):
if ('stars_5' in url):
return 5.
if ('stars_4_half' in url):
return 4.5
if ('stars_4' in url):
return 4.
if ('stars_3_half' in url):
return 3.5
if ('stars_3' in url):
return 3.
if ('stars_2_half' in url):
return 2.5
if ('stars_2' in url):
return 2.
if ('stars_1_half' in url):
return 1.5
if ('stars_1' in url):
return 1.
if ('stars_0' in url):
return 0.
def getMetro(metro_index):
return metros.metro_list[metro_index]['name']
def getCategoryFilter(category_index):
return categories.category_list[category_index]['filter']
def getCategory(category_index):
return categories.category_list[category_index]['name']
def main():
application = webapp.WSGIApplication([
('/admin/generate/', GenTilesHandler),
('/admin/gather/', GatherHandler),
('/admin/delete/', DeleteHandler),
('/admin/image/', ImageHandler)])
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
# How is the category filter supposed to work? When I look for all business in San Francisco, CA with category filter "lounges" and no search term, the response states there are 2426 results. However, there are actually only about 150 - most results after that are not in the lounge category. Is this expected behavior?How is the category filter supposed to work? When I look for all business in San Francisco, CA with category filter "lounges" and no search term, the response states there are 2426 results. However, there are actually only about 150 - most results after that are not in the lounge category. Is this expected behavior?