-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_pcr_tt_points_table.py
executable file
·161 lines (129 loc) · 5.69 KB
/
generate_pcr_tt_points_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
"""
PCR October SWCP TT results script.
Uses Strava API to get results within a given date range
(i.e., the TT weekend) and prints a table of results
which can be copied to main spreadsheet.
Requires developer API key, see https://strava.github.io/api/
Author: Dan Clewley
Date: 2017-10-02
"""
import argparse
import copy
from datetime import timedelta
import json
import sys
if sys.version_info.major < 3:
import urllib2 as request
import urllib as parse
else:
from urllib import parse
from urllib import request
HAVE_TABULATE = True
try:
import tabulate
except ImportError:
HAVE_TABULATE = False
# URL to get all info from a segment
# see https://strava.github.io/api/v3/segments/
SEGMENT_URL = "https://www.strava.com/api/v3/segments/{}/all_efforts"
# URL to get info about an athelete
# see https://strava.github.io/api/v3/athlete/
ATHLETE_URL = "https://www.strava.com/api/v3/athletes/{}"
# URL to display an activity (non API)
ACTIVITY_URL = "https://www.strava.com/activities/{}"
# Segment IDs
TT1_SEGMENT_ID = 10014031
TT2_SEGMENT_ID = 10014001
TT3_SEGMENT_ID = 10074233
TT321_SEGMENT_ID = 13052821
if __name__ == "__main__":
# Read input from command line
parser = argparse.ArgumentParser(description="Get PCR TT efforts within a given date range")
parser.add_argument("--start_date",
required=False,
help="Start date in format YY-MM-DD (assumes time is 00:00:00)",
default="2017-10-01")
parser.add_argument("--end_date",
required=False,
help="End date in format YY-MM-DD (assumes time is 23:59:59)",
default="2017-10-31")
parser.add_argument("--dev_key",
required=True,
help="Strava development key for authentication")
parser.add_argument("--pritty_print",
action="store_true",
default=False,
help="Format table for better output")
args = parser.parse_args()
all_segments = {"TT1" : TT1_SEGMENT_ID,
"TT2" : TT2_SEGMENT_ID,
"TT3" : TT3_SEGMENT_ID,
"Triple" : TT321_SEGMENT_ID}
segment_points = {"TT1" : 1,
"TT2" : 2,
"TT3" : 3,
"Triple" : 3}
# Results dictionary template - used for each athelete
results_dict = {"TT1" : 0, "TT2" : 0, "TT3" : 0,
"Triple" : 0, "PBs" : 0, "Total" : 0}
# Set up dictionary for atheletes, pairs are athletes : results_dict
results_athelete = {}
# Set date
date_data = parse.urlencode({"start_date_local" : "{}T00:00:00Z".format(args.start_date),
"end_date_local" : "{}T23:59:59Z".format(args.end_date),
"per_page" : "200"},)
for segment_name, segment_id in all_segments.items():
# Get all segment efforts
seg_req = request.Request(SEGMENT_URL.format(segment_id), date_data.encode(),
{"Authorization" : "Bearer {}".format(args.dev_key)},
method="GET")
seg_req_str = request.urlopen(seg_req).read()
seg_req_dict = json.loads(seg_req_str.decode())
for pos, effort in enumerate(seg_req_dict):
athlete_id = effort["athlete"]["id"]
activity_id = effort["activity"]["id"]
activity_url = ACTIVITY_URL.format(activity_id)
time_sec = effort["elapsed_time"]
time_hours = str(timedelta(seconds=(time_sec)))
date = effort["start_date_local"]
# Get athlete name and gender
athlete_req = request.Request(ATHLETE_URL.format(athlete_id), None,
{"Authorization" : "Bearer {}".format(args.dev_key)})
athlete_req_str = request.urlopen(athlete_req).read()
athlete_req_dict = json.loads(athlete_req_str.decode())
name = "{} {}".format(athlete_req_dict["firstname"], athlete_req_dict["lastname"])
sex = athlete_req_dict["sex"]
# Add to totals
try:
results_athelete[name][segment_name] += 1
results_athelete[name]["Total"] += segment_points[segment_name]
# If first attempt set up results structure and add points
except KeyError:
results_athelete[name] = copy.copy(results_dict)
results_athelete[name][segment_name] = 1
results_athelete[name]["Total"] += segment_points[segment_name]
# If segment was PB this
if effort["pr_rank"] == 1:
results_athelete[name]["PBs"] += 1
results_athelete[name]["Total"] += 5
# Print out table
header = ["Name", "TT1", "TT2", "TT3", "The Triple", "PBs", "Points"]
results_lines = []
for athelete, results in results_athelete.items():
tt1_attempts = results["TT1"]
tt2_attempts = results["TT2"]
tt3_attempts = results["TT3"]
triple_attempts = results["Triple"]
pb_attempts = results["PBs"]
total_points = results["Total"]
out_line = [athelete, tt1_attempts, tt2_attempts, tt3_attempts,
triple_attempts, pb_attempts, total_points]
results_lines.append(out_line)
if args.pritty_print and HAVE_TABULATE:
print(tabulate.tabulate(results_lines, header, tablefmt="fancy_grid"))
else:
print(",".join(header))
for line in results_lines:
line = [str(i) for i in line]
print(",".join(line))