本文整理汇总了Python中stravalib.Client.get_segment_leaderboard方法的典型用法代码示例。如果您正苦于以下问题:Python Client.get_segment_leaderboard方法的具体用法?Python Client.get_segment_leaderboard怎么用?Python Client.get_segment_leaderboard使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stravalib.Client
的用法示例。
在下文中一共展示了Client.get_segment_leaderboard方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: massive_test
# 需要导入模块: from stravalib import Client [as 别名]
# 或者: from stravalib.Client import get_segment_leaderboard [as 别名]
def massive_test(access_token, athlete_id):
client = Client(access_token=access_token)
mysegments = {} # all segments a user has ridden
# get athlete activities
athlete_from_db = Athlete.objects.get(strava_id=athlete_id)
activities = client.get_activities(limit=5, before=athlete_from_db.oldest_activity_date) # API call
# per activity, get segment efforts
for activity in activities:
if activity.type not in ['Ride', 'ride']:
continue
try:
# if activity already exists in db, skip it
Activity.objects.get(strava_id=activity.id)
continue
except Activity.DoesNotExist:
new_activity = Activity()
new_activity.strava_id = activity.id
new_activity.start_lat = activity.start_latitude
new_activity.start_long = activity.start_longitude
new_activity.start_date = activity.start_date
new_activity.save()
# update newest / oldest activity dates
if athlete_from_db.newest_activity_date is None:
athlete_from_db.newest_activity_date = activity.start_date
athlete_from_db.oldest_activity_date = activity.start_date
else:
if activity.start_date > athlete_from_db.newest_activity_date:
athlete_from_db.newest_activity_date = activity.start_date
elif activity.start_date < athlete_from_db.oldest_activity_date:
athlete_from_db.oldest_activity_date = activity.start_date
athlete_from_db.save()
segment_efforts = client.get_activity(activity.id).segment_efforts # API call
# per segment effort
for segment in segment_efforts:
mysegments[segment.segment.id] = segment.segment # save to db
# check if segment leaderboard contains any friends
for key, segment in mysegments.iteritems():
leaderboard = client.get_segment_leaderboard(key, following=True).entries # API call (possibly lots, depends on number of segments)
# get friend with time < athlete time
for i, entry in enumerate(leaderboard):
if entry.athlete_id == athlete_id:
me = entry
if i == 0:
# I'm already the winner!
break
j = 1
while j <= i and leaderboard[i - j].elapsed_time == me.elapsed_time:
# check for ties, compare each entry from i to zero (possibly)
j += 1
if leaderboard[i - j].elapsed_time == me.elapsed_time:
# if they're still tied at the end of the loop, I don't want to see it
break
other = leaderboard[i - j]
try:
new_segment = ChallengedSegment.objects.get(my_id=athlete_id, segment_id=segment.id)
except ChallengedSegment.DoesNotExist:
new_segment = ChallengedSegment()
new_segment.my_id = athlete_id
new_segment.their_id = other.athlete_id
new_segment.their_name = other.athlete_name
new_segment.my_pr = me.activity_id
new_segment.their_pr = other.activity_id
new_segment.my_time = str(me.elapsed_time)
new_segment.their_time = str(other.elapsed_time)
new_segment.difference = str(me.elapsed_time - other.elapsed_time)
new_segment.segment_id = segment.id
new_segment.segment_name = segment.name
new_segment.segment_distance = str(unithelper.miles(segment.distance))
new_segment.save()
break # we already found my entry, why keep looking through the list?
示例2: main
# 需要导入模块: from stravalib import Client [as 别名]
# 或者: from stravalib.Client import get_segment_leaderboard [as 别名]
def main():
assert len(args.athlete_ids) == len(args.access_tokens)
logger.info("app id: %i, fetching activities for ids %s" % \
(args.id_strava_app, str(args.athlete_ids)))
for i in range( len(args.access_tokens) ): # for each athlete
client = Client()
client.access_token = args.access_tokens[i]
athlete_id = args.athlete_ids[i]
# get summary activities first (filterd before detailed activity call)
time.sleep(TIME_PAUSE)
activity_ids = get_user_activities(client)
# now fetch detailed versions, add to db
detailed_activites = []
activity_to_segments = {} # { act_id:
# { seg_id :
# { "distance" : x, "grade" : y }, }, }
segment_ranks = {} # { seg_id : { "auth_athlete_rank" : auth_rank,
# "other_athletes" : { other_id : other_rank, } } }
for act_id in activity_ids:
try:
activity_to_segments[act_id] = {}
time.sleep(TIME_PAUSE)
detailed_activity = client.get_activity( act_id )
detailed_activites.append( detailed_activity )
for seg_effort in detailed_activity.segment_efforts:
segment = seg_effort.segment
seg_id = int(segment.id)
seg_dist = float( unithelper.miles(segment.distance) )
seg_grade = segment.average_grade
seg_dct = { "distance" : seg_dist, "grade" : seg_grade }
activity_to_segments[act_id][seg_id] = seg_dct
if segment_ranks.has_key(seg_id): # already have ranks
continue # might be overlap between activities
else:
try: # some = hazardous = error
time.sleep(TIME_PAUSE) # now get ranks for this segment
leaderboard_entries = \
client.get_segment_leaderboard(seg_id,
top_results_limit=1).entries
segment_ranks[seg_id] = { "auth_athlete_rank" : -1,
"other_athletes" : {} }
for entry in leaderboard_entries:
if entry.athlete_id == athlete_id:
segment_ranks[seg_id]["auth_athlete_rank"] = entry.rank
continue
other_id = entry.athlete_id
other_rank = entry.rank
segment_ranks[seg_id]["other_athletes"][other_id] = other_rank
except Exception, e:
logger.warning("Error with segment_id %i, removing from activity,"\
" trace:\n %s" % (seg_id, traceback.print_exc()))
activity_to_segments[act_id].pop(seg_id)
continue
if len(activity_to_segments[act_id]) > 0:
add_activity(conn, athlete_id, detailed_activity) # if made it here, okay
else:
logger.info("No segments for activity %i, skipping" % act_id)
except Exception, e: # occurs with wrong privaleges, eg private activity
logger.warning("Error with activity %i for athlete %i, popping. tracebac:\n%s" % \
(act_id, athlete_id, traceback.print_exc()))
activity_to_segments.pop(act_id)