def runTest(self): self.assertEqual( 'US', gmaps_api.get_geocode(address='San Francisco').country()) self.assertEqual('JP', gmaps_api.get_geocode(address='Tokyo').country()) # Really long byte-string self.assertEqual( 'RU', gmaps_api.get_geocode( address= u"г.Сочи , ул.Навагинская 9 / 3 этаж...Молодёжный Творческий Центр им.Артура Тумасяна, Творческий Клуб \" Чип Бар \"" ).country())
def get_bounds(self): bounds = None if self.location.data: geocode = gmaps_api.get_geocode(address=self.location.data, language=self.locale.data) bounds = math.expand_bounds(geocode.latlng_bounds(), self.distance_in_km()) return bounds
def get_ranking_location(location): if location is None: return "Unknown" geocode = gmaps_api.get_geocode(address=location) if geocode is None: return "Unknown" point = geocode.latlng() return get_ranking_location_latlng(point)
def create_from_location(cls, location): if location: geocode = gmaps_api.get_geocode(address=location) if not geocode: raise search_base.SearchException("Did not understand location: %s" % location) bounds = math.expand_bounds(geocode.latlng_bounds(), 200) # HACK? else: bounds = None self = cls(bounds=bounds) return self
def runTest(self): for addresses, reformatted_addresses in grouping_lists: logging.info("Formatting addresses: %s", addresses) logging.info("Intended reformatted addresses: %r", reformatted_addresses) geocodes = [ gmaps_api.get_geocode(address=address) for address in addresses ] reformatted_parts = formatting.format_geocodes( geocodes, include_neighborhood=True) logging.info("Reformatted addresses: %r", reformatted_parts) self.assertEqual(reformatted_parts, reformatted_addresses)
def __init__(self, fb_event=None, db_event=None, debug=False): self.exact_from_event = False self.overridden_address = None self.fb_address = None self.remapped_address = None self.final_address = None has_overridden_address = db_event and db_event.address if not has_overridden_address and not fb_event: logging.warning( "Passed a db_event without an address, and no fb_event to pull from: %s" % db_event.id) if (not has_overridden_address and fb_event) or debug: self.final_latlng = _get_latlng_from_event(fb_event) if self.final_latlng: self.exact_from_event = True self.geocode = gmaps_api.get_geocode(latlng=self.final_latlng) self.fb_address = formatting.format_geocode(self.geocode) self.remapped_address = None else: self.fb_address = get_address_for_fb_event(fb_event) self.remapped_address = _get_remapped_address_for( self.fb_address) if self.remapped_address: logging.info("Checking remapped address, which is %r", self.remapped_address) self.final_address = self.remapped_address or self.fb_address if has_overridden_address: self.overridden_address = db_event.address self.final_address = self.overridden_address if not self.exact_from_event: logging.info("Final address is %r", self.final_address) if self.online: self.geocode = None elif self.final_address is not None: self.geocode = gmaps_api.get_geocode( address=self.final_address) else: self.geocode = None
def compute_derived_properties(self, fb_data): if fb_data: if fb_data['empty']: # only update these when we have feed data self.fb_info = {} else: self.fb_info = fb_data['info'] if 'likes' in fb_data['info']: self.graph_type = GRAPH_TYPE_FANPAGE elif 'locale' in fb_data['info'] or 'first_name' in fb_data[ 'info']: self.graph_type = GRAPH_TYPE_PROFILE elif 'groups.facebook.com' in fb_data['info'].get('email', []): self.graph_type = GRAPH_TYPE_GROUP elif 'start_time' in fb_data['info']: self.graph_type = GRAPH_TYPE_EVENT else: logging.info("cannot classify object type for id %s", fb_data['info']['id']) if 'name' not in fb_data['info']: logging.error( 'cannot find name for fb event data: %s, cannot update source data...', fb_data) return self.name = fb_data['info']['name'] feed = fb_data['feed']['data'] if len(feed): dt = datetime.datetime.strptime(feed[-1]['created_time'], '%Y-%m-%dT%H:%M:%S+0000') td = datetime.datetime.now() - dt total_seconds = td.seconds + td.days * 24 * 3600 self.feed_history_in_seconds = total_seconds #logging.info('feed time delta is %s', self.feed_history_in_seconds) else: self.feed_history_in_seconds = 0 location = fb_data['info'].get('location') if location: if location.get('latitude'): self.latitude = float(location.get('latitude')) self.longitude = float(location.get('longitude')) else: component_names = [ 'street', 'city', 'state', 'zip', 'region', 'country' ] components = [ location.get(x) for x in component_names if location.get(x) ] address = ', '.join(components) geocode = gmaps_api.get_geocode(address=address) if geocode: self.latitude, self.longitude = geocode.latlng()
def runTest(self): for address, final_address in formatting_reg_data.iteritems(): logging.info('%s should be formatted as %s', address, final_address) formatted_address = formatting.format_geocode( gmaps_api.get_geocode(address=address), include_neighborhood=True) if formatted_address != final_address: logging.error('formatted address for %r is %r, should be %r', address, formatted_address, final_address) logging.error( '%s', gmaps_api._fetch_geocode_as_json(address=address)) self.assertEqual(final_address, formatted_address)
def compute_derived_properties(self, fb_user): self.full_name = fb_user['profile'].get('name') self.first_name = fb_user['profile'].get('first_name') self.last_name = fb_user['profile'].get('last_name') self.email = fb_user['profile'].get('email') self.locale = fb_user['profile'].get('locale') try: self.timezone_offset = float(fb_user['profile'].get('timezone')) except (datastore_errors.BadValueError, TypeError) as e: logging.error("Failed to save timezone %s: %s", fb_user['profile'].get('timezone'), e) self.location_country = None if self.location: geocode = gmaps_api.get_geocode(address=self.location) if geocode: self.location_country = geocode.country()
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.get_geocode(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!")
def geocodable_location(form, field): if field.data: geocode = gmaps_api.get_geocode(address=field.data) if not geocode: raise wtforms.ValidationError("Did not understand location: %s" % field.data)
def email_for_user(user, fbl, should_send=True): if not user.send_email or not user.email: return if user.weekly_email_send_date and user.weekly_email_send_date > datetime.datetime.now( ) - datetime.timedelta(days=3): logging.warning( "Skipping user %s (%s) because last weekly email was sent on %s", user.fb_uid, user.full_name, user.weekly_email_send_date) # Sent the weekly email too recently, let's abort return fb_user = fbl.fetched_data(fb_api.LookupUser, fbl.fb_uid) if not 'profile' in fb_user: 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.get_geocode(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) fb_user = fbl.fetched_data(fb_api.LookupUser, fbl.fb_uid) search_results = search.Search(query).get_search_results() # Don't send email... if not search_results: return friends.decorate_with_friends(fbl, search_results) rsvp.decorate_with_rsvps(fbl, search_results) past_results, present_results, grouped_results = search.group_results( search_results, include_all=True) display = {} display['user'] = user display['fb_user'] = fb_user week_events = grouped_results[0] # Only send emails if we have upcoming events if not week_events.results: return None display['results'] = week_events.results jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader("templates")) jinja_env.filters['date_human_format'] = user.date_human_format jinja_env.globals['dd_event_url'] = urls.dd_event_url jinja_env.globals['raw_fb_event_url'] = urls.raw_fb_event_url jinja_env.globals['CHOOSE_RSVPS'] = rsvp.CHOOSE_RSVPS rendered = jinja_env.get_template('html_mail_summary.html').render(display) d = datetime.date.today() d = d - datetime.timedelta(days=d.weekday()) # round down to last monday logging.info("Rendered HTML:\n%s", rendered) message = mail.EmailMessage( sender="DanceDeets Events <*****@*****.**>", subject="Dance events for %s" % d.strftime('%b %d, %Y'), to=user.email, html=rendered) if should_send: # Update the last-sent-time here, so we any retryable errors don't cause emails to be multi-sent user = users.User.get_by_id(user.fb_uid) user.weekly_email_send_date = datetime.datetime.now() user.put() # And send the message now. message.send() return message
def _inner_make_event_findable_for_web_event(db_event, json_body, update_geodata): logging.info("Making web_event %s findable." % db_event.id) db_event.web_event = json_body db_event.fb_event = None db_event.owner_fb_uid = None db_event.attendee_count = 0 # Maybe someday set attending counts when we fetch them? db_event.start_time = datetime.datetime.strptime(json_body['start_time'], DATETIME_FORMAT) if json_body.get('end_time'): db_event.end_time = datetime.datetime.strptime(json_body['end_time'], DATETIME_FORMAT) else: db_event.end_time = None db_event.search_time_period = _event_time_period(db_event) db_event.event_keywords = event_classifier.relevant_keywords(db_event) db_event.auto_categories = [ x.index_name for x in categories.find_styles(db_event) + categories.find_event_types(db_event) ] geocode = None if json_body.get('location_address'): logging.info("Have location address, checking if it is geocodable: %s", json_body.get('location_address')) geocode = gmaps_api.get_geocode(address=json_body['location_address']) if geocode is None: logging.warning( "Received a location_address that was not geocodeable, treating as empty: %s", json_body['location_address']) if geocode is None: results = None if json_body.get('geolocate_location_name'): logging.info( "Have magic geolocate_location_name, checking if it is a place: %s", json_body.get('geolocate_location_name')) results = gmaps_api.fetch_place_as_json( query=json_body['geolocate_location_name']) if not results or results['status'] == 'ZERO_RESULTS': if json_body.get('location_name'): logging.info( "Have regular location_name, checking if it is a place: %s", json_body.get('location_name')) results = gmaps_api.fetch_place_as_json( query=json_body['location_name']) if results and results['status'] == 'OK': result = results['results'][0] json_body['location_name'] = result['name'] json_body['location_address'] = result['formatted_address'] logging.info("Found an address: %s", json_body['location_address']) # BIG HACK!!! if 'Japan' not in json_body[ 'location_address'] and 'Korea' not in json_body[ 'location_address']: logging.error("Found incorrect address for venue!") latlng = result['geometry']['location'] json_body['latitude'] = latlng['lat'] json_body['longitude'] = latlng['lng'] db_event.address = json_body.get('location_address') if db_event.image_url: try: mimetype, image_data = fetch.fetch_data(db_event.image_url) image = images.Image(image_data) json_body['photo_width'] = image.width json_body['photo_height'] = image.height logging.info("Found image width size %sx%s", image.width, image.height) except urllib2.HTTPError: logging.warning("Couldn't find image: %s", db_event.image_url) if update_geodata: # Don't use cached/stale geocode when constructing the LocationInfo here db_event.location_geocode = None location_info = event_locations.LocationInfo(db_event=db_event) _update_geodata(db_event, location_info)