Ejemplo n.º 1
0
def _get_bounds_for_fb_event(fb_event,
                             fb_event_attending_maybe,
                             check_places=False):
    # We don't need google-maps latlong accuracy. Let's cheat and use the fb_event for convenience if possible...
    location = fb_event['info'].get('place', {}).get('location', {})
    if location and location.get('latitude') is not None:
        latlong = (location['latitude'], location['longitude'])
        # TODO: Someday we'd like to use locations of Taiwan or Germany to grab a bunch of stuff in those bounds
        # but for now, FB doesn't send us proper lat-long-boxes for them, and I don't want to look up everything
        # just in case there are bigger bounds...so we accept the latlong as-is.
        bounds = math.expand_bounds((latlong, latlong),
                                    cities_db.NEARBY_DISTANCE_KM)
    else:
        logging.info('Looking up event %s LocationInfo',
                     fb_event['info']['id'])
        # Places textsearch lookups turn out to be 10x-expensive against our quota
        # So we disable them here, and instead just rely on the 99% good address searches
        # It should fallback to places on un-geocodable addresses too...
        # But at least it won't try Places *in addition* to geocode lookups.
        location_info = event_locations.LocationInfo(
            fb_event,
            fb_event_attending_maybe=fb_event_attending_maybe,
            check_places=check_places)
        if location_info.geocode:
            bounds = math.expand_bounds(location_info.geocode.latlng_bounds(),
                                        cities_db.NEARBY_DISTANCE_KM)
        else:
            bounds = None
    return bounds
Ejemplo n.º 2
0
def get_nearby_cities(latlng, country=None, distance=None):
    # We shrink it by two:
    # An event in Palo Alto could be thrown into a San Jose bucket
    # But an event in San Francisco, looking for "people who would go to SF event",
    # would want to include Palo Alto in its search radius....so would need to go 2x to San Jose
    # So instead of searching 200km in popular people for cities...let's try to be more specific about which person goes to which city
    distance = distance or NEARBY_DISTANCE_KM / 2
    southwest, northeast = math.expand_bounds((latlng, latlng), distance)
    nearby_cities = get_contained_cities((southwest, northeast), country=country)
    nearby_cities = [x for x in nearby_cities if x.closer_than(latlng, distance)]
    return nearby_cities
def _get_bounds_for_fb_event(fb_event, fb_event_attending_maybe, check_places=False):
    # We don't need google-maps latlong accuracy. Let's cheat and use the fb_event for convenience if possible...
    location = fb_event['info'].get('place', {}).get('location', {})
    if location and location.get('latitude') is not None:
        latlong = (location['latitude'], location['longitude'])
        # TODO: Someday we'd like to use locations of Taiwan or Germany to grab a bunch of stuff in those bounds
        # but for now, FB doesn't send us proper lat-long-boxes for them, and I don't want to look up everything
        # just in case there are bigger bounds...so we accept the latlong as-is.
        bounds = math.expand_bounds((latlong, latlong), cities_db.NEARBY_DISTANCE_KM)
    else:
        logging.info('Looking up event %s LocationInfo', fb_event['info']['id'])
        # Places textsearch lookups turn out to be 10x-expensive against our quota
        # So we disable them here, and instead just rely on the 99% good address searches
        # It should fallback to places on un-geocodable addresses too...
        # But at least it won't try Places *in addition* to geocode lookups.
        location_info = event_locations.LocationInfo(fb_event, fb_event_attending_maybe=fb_event_attending_maybe, check_places=check_places)
        if location_info.geocode:
            bounds = math.expand_bounds(location_info.geocode.latlng_bounds(), cities_db.NEARBY_DISTANCE_KM)
        else:
            bounds = None
    return bounds
Ejemplo n.º 4
0
def _generate_results_for(city, week_start):
    start_time = week_start
    end_time = start_time + datetime.timedelta(days=8)

    latlng_bounds = ((city.latitude, city.longitude), (city.latitude, city.longitude))
    city_bounds = math.expand_bounds(latlng_bounds, cities_db.NEARBY_DISTANCE_KM)
    search_query = search_base.SearchQuery(
        time_period=search_base.TIME_ALL_FUTURE, start_date=start_time, end_date=end_time, bounds=city_bounds
    )
    searcher = search.Search(search_query)
    search_results = searcher.get_search_results(full_event=True)
    return search_results
Ejemplo n.º 5
0
def _generate_results_for(city, week_start):
    start_time = week_start
    end_time = start_time + datetime.timedelta(days=8)

    latlng_bounds = ((city.latitude, city.longitude), (city.latitude,
                                                       city.longitude))
    city_bounds = math.expand_bounds(latlng_bounds,
                                     cities_db.NEARBY_DISTANCE_KM)
    search_query = search_base.SearchQuery(
        time_period=search_base.TIME_ALL_FUTURE,
        start_date=start_time,
        end_date=end_time,
        bounds=city_bounds)
    searcher = search.Search(search_query)
    search_results = searcher.get_search_results(full_event=True)
    return search_results
Ejemplo n.º 6
0
 def build_query(self, start_end_query=False):
     bounds = None
     country_code = None
     if self.location.data:
         geocode = gmaps_api.lookup_address(self.location.data, language=self.locale.data)
         if geocode.is_country_geocode():
             country_code = geocode.country()
         else:
             bounds = math.expand_bounds(geocode.latlng_bounds(), self.distance_in_km())
     keywords = _get_parsed_keywords(self.keywords.data)
     common_fields = dict(
         bounds=bounds,
         min_attendees=self.min_attendees.data,
         min_worth=self.min_worth.data,
         keywords=keywords,
         country_code=country_code
     )
     query = SearchQuery(start_date=self.start.data, end_date=self.end.data, **common_fields)
     return query
 def build_query(self, start_end_query=False):
     bounds = None
     country_code = None
     if self.location.data:
         geocode = gmaps_api.lookup_address(self.location.data,
                                            language=self.locale.data)
         if geocode.is_country_geocode():
             country_code = geocode.country()
         else:
             bounds = math.expand_bounds(geocode.latlng_bounds(),
                                         self.distance_in_km())
     keywords = _get_parsed_keywords(self.keywords.data)
     common_fields = dict(bounds=bounds,
                          min_attendees=self.min_attendees.data,
                          min_worth=self.min_worth.data,
                          keywords=keywords,
                          country_code=country_code)
     query = SearchQuery(start_date=self.start.data,
                         end_date=self.end.data,
                         **common_fields)
     return query
def promote_events_to_user(user):
    # TODO: Adjust when we have iphone notifications
    if not android.can_notify(user):
        return

    logging.info("Promoting new events to user %s", user.fb_uid)
    # Only send notifications for Mike for now
    user = users.User.get_by_id(user.fb_uid)
    if not user:
        logging.error("No user found: %s", user.fb_uid)
        return
    if user.expired_oauth_token:
        logging.info("User has expired token, aborting: %s", user.fb_uid)
        return

    user_location = user.location
    if not user_location:
        return
    distance_in_km = user.distance_in_km()
    min_attendees = user.min_attendees

    # search for relevant events
    geocode = gmaps_api.lookup_address(user_location)
    if not geocode:
        return None
    bounds = math.expand_bounds(geocode.latlng_bounds(), distance_in_km)
    query = search_base.SearchQuery(time_period=search_base.TIME_UPCOMING, bounds=bounds, min_attendees=min_attendees)

    one_day_ago = time.mktime((datetime.datetime.now() - datetime.timedelta(hours=24)).timetuple())

    search_query = search.Search(query)
    search_query.extra_fields = ['creation_time']
    search_results = search_query._get_candidate_doc_events()
    # TODO: can we move this filter into the search query itself??
    recent_events = [x.doc_id for x in search_results if x.field('creation_time').value > one_day_ago]

    logging.info("Found %s search_results, %s new events", len(search_results), len(recent_events))
    for event_id in recent_events:
        if android.add_notify(user, event_id):
            logging.info("Sent notification!")
Ejemplo n.º 9
0
def get_center_and_bounds(geocode, distance):
    southwest, northeast = math.expand_bounds(geocode.latlng_bounds(), distance)
    return geocode.latlng(), southwest, northeast
Ejemplo n.º 10
0
def get_center_and_bounds(geocode, distance):
    southwest, northeast = math.expand_bounds(geocode.latlng_bounds(),
                                              distance)
    return geocode.latlng(), southwest, northeast