Ejemplo n.º 1
0
def get_athlete_details(athlete_id, month=None):
    now = datetime.now()

    if month is None:
        month = now.month
    else:
        month = int(month)

    date_range = calendar.monthrange(now.year, month)

    start_date = '%s-%s-%s' % (now.year, month, 1)
    end_date = '%s-%s-%s' % (now.year, month, date_range[1])

    api = StravaApi()

    # Get the athlete's rides.
    athlete = {}
    rides = api.rides(athlete_id=athlete_id, start_date=start_date, end_date=end_date)

    total_climbed = 0
    average_climbed = 0
    total_distance = 0
    average_distance = 0
    number_rides = 0

    if len(rides) > 0:
        # Need to make sure we get them all. Not many riders will have > 50 rides in a month, though.
        iteration = 1
        while len(rides) % 50 == 0:
            offset = 50 * iteration
            iteration += 1
            rides.extend(api.rides(athlete_id=athlete_id, start_date=start_date, end_date=end_date, offset=offset))

        # We need the details for each ride as well.
        for ride in rides:
            ride['details'] = api.ride_details(ride['id'])
            total_climbed += ride['details']['elevationGain'] * METERS_TO_FEET_RATIO
            total_distance += ride['details']['distance'] * METERS_TO_MILES_RATIO

        number_rides = len(rides)
        average_climbed = total_climbed / number_rides
        average_distance = total_distance / number_rides

    athlete['id'] = athlete_id
    athlete['number_rides'] = number_rides
    athlete['total_climbed'] = { 'display': round(total_climbed, 1), 'raw': total_climbed }
    athlete['average_climbed'] = { 'display': round(average_climbed, 1), 'raw': average_climbed }
    athlete['total_distance'] = { 'display': round(total_distance, 1), 'raw': total_distance }
    athlete['average_distance'] = { 'display': round(average_distance, 1), 'raw': average_distance }

    return athlete
Ejemplo n.º 2
0
def get_club_details(club_id, load_athlete_details=False):
    api = StravaApi()

    # Get the club.
    club = api.club_details(club_id)

    # Get the members for the club.
    club['members'] = api.club_members(club_id)

    if load_athlete_details:
        for member in club['members']:
            member['details'] = get_athlete_details(member['id'])

    return club
Ejemplo n.º 3
0
def get_club_details(club_id, load_athlete_details=False):
    api = StravaApi()

    # Get the club.
    club = api.club_details(club_id)

    # Get the members for the club.
    club['members'] = api.club_members(club_id)

    if load_athlete_details:
        for member in club['members']:
            member['details'] = get_athlete_details(member['id'])

    return club
Ejemplo n.º 4
0
def get_athlete_details(athlete_id, month=None):
    now = datetime.now()

    if month is None:
        month = now.month
    else:
        month = int(month)

    date_range = calendar.monthrange(now.year, month)

    start_date = '%s-%s-%s' % (now.year, month, 1)
    end_date = '%s-%s-%s' % (now.year, month, date_range[1])

    api = StravaApi()

    # Get the athlete's rides.
    athlete = {}
    rides = api.rides(athlete_id=athlete_id,
                      start_date=start_date,
                      end_date=end_date)

    total_climbed = 0
    average_climbed = 0
    total_distance = 0
    average_distance = 0
    number_rides = 0

    if len(rides) > 0:
        # Need to make sure we get them all. Not many riders will have > 50 rides in a month, though.
        iteration = 1
        while len(rides) % 50 == 0:
            offset = 50 * iteration
            iteration += 1
            rides.extend(
                api.rides(athlete_id=athlete_id,
                          start_date=start_date,
                          end_date=end_date,
                          offset=offset))

        # We need the details for each ride as well.
        for ride in rides:
            ride['details'] = api.ride_details(ride['id'])
            total_climbed += ride['details'][
                'elevationGain'] * METERS_TO_FEET_RATIO
            total_distance += ride['details'][
                'distance'] * METERS_TO_MILES_RATIO

        number_rides = len(rides)
        average_climbed = total_climbed / number_rides
        average_distance = total_distance / number_rides

    athlete['id'] = athlete_id
    athlete['number_rides'] = number_rides
    athlete['total_climbed'] = {
        'display': round(total_climbed, 1),
        'raw': total_climbed
    }
    athlete['average_climbed'] = {
        'display': round(average_climbed, 1),
        'raw': average_climbed
    }
    athlete['total_distance'] = {
        'display': round(total_distance, 1),
        'raw': total_distance
    }
    athlete['average_distance'] = {
        'display': round(average_distance, 1),
        'raw': average_distance
    }

    return athlete
Ejemplo n.º 5
0
def index(request, ride_id=None):
    if ride_id is None:
        ride_id = 6206631

    api = StravaApi()

    ride_stream = api.ride_stream(ride_id)

    points = []

    altitudes = ride_stream['altitude']
    #altitude_originals = ride_stream['altitude_original']
    distances = ride_stream['distance']
    grade_smooths = ride_stream['grade_smooth']
    latlngs = ride_stream['latlng']
    movings = ride_stream['moving']
    outliers = ride_stream['outlier']
    times = ride_stream['time']
    velocity_smooths = ride_stream['velocity_smooth']
    watts_calcs = ride_stream['watts_calc']

    number_of_points = len(altitudes)

    for i in range(number_of_points):
        altitude = altitudes[i]
        #altitude_original = altitude_originals[i]
        distance = distances[i]
        grade_smooth = grade_smooths[i]
        latlng = latlngs[i]
        moving = movings[i]
        outlier = outliers[i]
        time = times[i]
        velocity_smooth = velocity_smooths[i]
        watts_calc = watts_calcs[i]

        point = {
            'altitude': altitude * METERS_TO_FEET_RATIO,
            #'altitude_original': altitude_original * METERS_TO_FEET_RATIO,
            'distance': distance * METERS_TO_MILES_RATIO,
            'grade_smooth': grade_smooth,
            'latlng': latlng,
            'moving': moving,
            'outlier': outlier,
            'time': time,
            'velocity_smooth': velocity_smooth * METERS_PER_SECOND_TO_MILES_PER_HOUR_RATIO,
            'watts_calc': watts_calc,
        }

        points.append(point)

    stats = {
        'altitude': get_stats(altitudes, 'ft', 'Altitude'),
        'grade_smooth': get_stats(grade_smooths, '%', 'Grade'),
        'velocity_smooth': get_stats(velocity_smooths, 'mi/hr', 'Speed'),
        'watts_calc': get_stats(watts_calcs, 'w', 'Power'),
    }

    stats['altitude']['min_lat_lng'] = latlngs[stats['altitude']['min_index']]
    stats['altitude']['max_lat_lng'] = latlngs[stats['altitude']['max_index']]
    stats['velocity_smooth']['min_lat_lng'] = latlngs[stats['velocity_smooth']['min_index']]
    stats['velocity_smooth']['max_lat_lng'] = latlngs[stats['velocity_smooth']['max_index']]

    stats['altitude']['min'] *= METERS_TO_FEET_RATIO
    stats['altitude']['average'] *= METERS_TO_FEET_RATIO
    stats['altitude']['max'] *= METERS_TO_FEET_RATIO
    stats['velocity_smooth']['min'] *= METERS_PER_SECOND_TO_MILES_PER_HOUR_RATIO
    stats['velocity_smooth']['average'] *= METERS_PER_SECOND_TO_MILES_PER_HOUR_RATIO
    stats['velocity_smooth']['max'] *= METERS_PER_SECOND_TO_MILES_PER_HOUR_RATIO

    ride_distance = max(distances) * METERS_TO_MILES_RATIO

    context = RequestContext(request)
    return render_to_response('dhmadness/index.html',
        {
            'points': points,
            'stats': stats,
            'ratios': {
                'seconds_to_minutes': SECONDS_TO_MINUTES_RATIO,
            },
            'ride_distance': ride_distance
        },
        context_instance=context)