def update_venues(): refresh_date = datetime.date.today() - datetime.timedelta(venue_refresh_days) venues = Venue.select( AND(Venue.q.approved != None, OR(Venue.q.batch_updated == None, Venue.q.batch_updated < refresh_date)) ) count = venues.count() if not util.is_production(): count = min(count, 10) for venue in venues[:count]: if venue.zip_code: area = ", " + venue.zip_code else: area = ", Portland, OR" if venue.address: try: lat, lon, zip_code = geo.get_geocode(venue.address + area) venue.geocode_lat = lat venue.geocode_lon = lon if not venue.zip_code: venue.zip_code = zip_code except IOError: pass venue.batch_updated = datetime.datetime.now() return count
def closest(cls, location, count=5, with_event=False): from operator import itemgetter import geo pdx_lat, pdx_lon = "45.511810", "-122.675680" try: # lat is +, lon is -, a bug if otherwise lat, lon, zip_code = geo.get_geocode(location) if not geo.is_within_radius(pdx_lat, pdx_lon, 15, lat, lon): return list() dist = 0.2 got_result = False while not got_result: hi_lat, lo_lat, hi_lon, lo_lon = \ geo.radius_to_ll_bounds(lat, lon, dist) venues = Venue.select(AND(Venue.q.geocode_lat >= lo_lat, Venue.q.geocode_lat <= hi_lat, Venue.q.geocode_lon >= lo_lon, Venue.q.geocode_lon <= hi_lon)) venues = list(venues) if with_event: venues = [v for v in venues if v.today_events.count()] if len(venues) >= count: got_result = True else: dist *= 2 except IOError: return list() # sort by distance lst = [(v, geo.distance(lat, lon, v.geocode_lat, v.geocode_lon)) for v in venues] lst.sort(key=itemgetter(1)) return lst[:count]