class AthleteWorker(object): def __init__(self, service): self.service = service self.client = ClientWrapper(service) def sync(self): self.sync_athlete() self.sync_activities() def sync_athlete(self): self.client.ensure_access() athlete = self.client.get_athlete() ds_util.client.put(Athlete.to_entity(athlete, parent=self.service.key)) def sync_activities(self): self.client.ensure_access() athlete = self.client.get_athlete() athlete_clubs = [club.id for club in athlete.clubs] # Track the clubs that these activities were a part of, by annotating # them with the athlete's clubs. for activity in self.client.get_activities(): activity_entity = Activity.to_entity(activity, parent=self.service.key) activity_entity['clubs'] = athlete_clubs ds_util.client.put(activity_entity) def _sync_activity(self, activity_id): """Gets additional info: description, calories and embed_token.""" activity = self.client.get_activity(activity_id) return ds_util.client.put( Activity.to_entity(activity, parent=self.service.key))
def _create_unfurls(event): strava = Service.get('strava', parent=Bot.key()) strava_client = ClientWrapper(strava) unfurls = {} for link in event['event']['links']: alt_url = _resolve_rewrite_link(link) unfurl = _unfurl(strava_client, link, alt_url) if unfurl: unfurls[link['url']] = unfurl logging.warning(f'_create_unfurls: {unfurls}') return unfurls
class ClubWorker(object): def __init__(self, club_id, service): self.club_id = club_id self.service = service self.client = ClientWrapper(service) def sync(self): self.sync_club() club = self.sync_activities() return club def sync_club(self): self.client.ensure_access() club = self.client.get_club(self.club_id) club_entity = Club.to_entity(club, parent=self.service.key) club_entity['members'] = [ Athlete.to_entity(member) for member in club.members ] ds_util.client.put(club_entity) return club_entity def sync_activities(self): self.client.ensure_access() with ds_util.client.transaction(): club = Club.get(self.club_id, parent=self.service.key) activity_query = ds_util.client.query(kind='Activity', ancestor=club.key) activity_query.keys_only() ds_util.client.delete_multi(activity.key for activity in activity_query.fetch()) for activity in self.client.get_club_activities(club.id): activity_entity = Activity.to_entity(activity, parent=club.key) ds_util.client.put(activity_entity) return club
def __init__(self, club_id, service): self.club_id = club_id self.service = service self.client = ClientWrapper(service)
def __init__(self, service): self.service = service self.client = ClientWrapper(service)
class Worker(object): def __init__(self, service): self.service = service self.client = ClientWrapper(service) def sync(self): self.sync_athlete() self.sync_activities() self.sync_routes() self.sync_segments() def sync_athlete(self): self.client.ensure_access() athlete = self.client.get_athlete() ds_util.client.put(Athlete.to_entity(athlete, parent=self.service.key)) def sync_activities(self): self.client.ensure_access() athlete = self.client.get_athlete() for activity in self.client.get_activities(): # Track full activity info (detailed), not returned by the normal # get_activities (summary) request. detailed_activity = self.client.get_activity(activity.id) activity_entity = Activity.to_entity(detailed_activity, detailed_athlete=athlete, parent=self.service.key) ds_util.client.put(activity_entity) # But also add all the user's best efforts. for segment_effort in detailed_activity.segment_efforts: segment_effort_entity = SegmentEffort.to_entity( segment_effort, parent=self.service.key) ds_util.client.put(segment_effort_entity) def sync_routes(self): self.client.ensure_access() for route in self.client.get_routes(): ds_util.client.put(Route.to_entity(route, parent=self.service.key)) def sync_segments(self): self.client.ensure_access() for segment in self.client.get_starred_segments(): # Track full segment info (detailed), not returned by the normal # get_starred_segments (summary) request. detailed_segment = self.client.get_segment(segment.id) elevations = self._fetch_segment_elevation(detailed_segment) segment_entity = Segment.to_entity(detailed_segment, elevations=elevations, parent=self.service.key) ds_util.client.put(segment_entity) def _fetch_segment_elevation(self, segment): return [{ 'location': { 'latitude': e['location']['lat'], 'longitude': e['location']['lng'], }, 'elevation': e['elevation'], 'resolution': e['resolution'], } for e in googlemaps_util.client.elevation_along_path( segment.map.polyline, samples=100)] def _sync_activity(self, activity_id): """Gets additional info: description, calories and embed_token.""" activity = self.client.get_activity(activity_id) return ds_util.client.put( Activity.to_entity(activity, parent=self.service.key))
def __init__(self, service, event): self.service = service self.event = event self.client = ClientWrapper(service)
class EventsWorker(object): def __init__(self, service, event): self.service = service self.event = event self.client = ClientWrapper(service) def sync(self): self.client.ensure_access() object_id = self.event.get('object_id') object_type = self.event.get('object_type') aspect_type = self.event.get('aspect_type') with ds_util.client.transaction(): logging.debug( 'StravaEvent: process_event_batch: %s, %s', object_id, self.event.key, ) if object_type == 'activity': if aspect_type == 'delete': activity_key = ds_util.client.key( 'Activity', object_id, parent=self.service.key ) ds_util.client.delete(activity_key) logging.info( 'StravaEvent: Deleted Activity: %s: %s', activity_key, self.event.key, ) else: athlete = self.client.get_athlete() activity = self.client.get_activity(object_id) activity_entity = Activity.to_entity( activity, detailed_athlete=athlete, parent=self.service.key ) ds_util.client.put(activity_entity) logging.info( 'StravaEvent: Created: %s: %s', activity_entity.key, self.event.key, ) elif object_type == 'athlete': athlete = self.client.get_athlete() athlete_entity = Athlete.to_entity(athlete, parent=self.service.key) ds_util.client.put(athlete_entity) logging.info( 'StravaEvent: Updated Athlete: %s: %s', athlete_entity.key, self.event.key, ) activities_query = ds_util.client.query( kind='Activity', ancestor=self.service.key ) for activity in activities_query.fetch(): activity['athlete'] = athlete ds_util.client.put(activity) logging.info( 'StravaEvent: Updated Activities: %s: %s', athlete_entity.key, self.event.key, ) else: logging.warning( 'StravaEvent: Update object_type %s not implemented: %s', object_type, self.event.key, )