Exemple #1
0
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))
Exemple #2
0
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))
Exemple #3
0
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,
                )