-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbaUtils.py
367 lines (314 loc) · 9.03 KB
/
tbaUtils.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 4 23:12:28 2016
"""
import urllib.request
import json
from time import sleep
from datetime import date
URL = 'http://www.thebluealliance.com/api/v3/'
THISYEAR = date.today().year
'''
X-TBA-App-Id is required by the blue alliance api for tracking
Default User-Agent value causes 403 Forbidden so I pretend to be a browser.
'''
REQHEADERS = {'X-TBA-Auth-Key': 'noZ6Q6W3YhLxNHL7IQzCxTTDzSfCzeOPbcNN7lsRoUFyYbkfnoQXnFrGYM9Ecoeo',
'User-Agent': 'Mozilla/5.0'}
def get_request(fullurl):
request = urllib.request.Request(fullurl, headers=REQHEADERS)
response = urllib.request.urlopen(request)
jsonified = json.loads(response.read().decode("utf-8"))
return jsonified
def get_team(team_num):
'''
@param team_num: int
@return: dict containing basic team overview information
{'address': None,
'city': 'Kansas City',
'country': 'USA',
'gmaps_place_id': None,
'gmaps_url': None,
'key': 'frc1939',
'lat': None,
'lng': None,
'location_name': None,
'motto': None,
'name': 'The Barstow School&THE BARSTOW SCHOOL',
'nickname': 'THE KUHNIGITS',
'postal_code': '64114',
'rookie_year': 2006,
'school_name': 'THE BARSTOW SCHOOL',
'state_prov': 'Missouri',
'team_number': 1939,
'website': 'https://www.frcteam1939.com'}
'''
fullurl = URL + 'team/frc' + str(team_num)
result = get_request(fullurl)
return result
def get_team_bots(team_num):
'''
@param team_num:
@return: list of dicts containing key ('frc1939_2015'), robot name, team_key, and year
'''
fullurl = URL + 'team/frc' + str(team_num) + '/robots'
print(fullurl)
result = get_request(fullurl)
return result
def get_team_history(team_num):
'''
@param team_num:
@return: list of eventcodes
'''
fullurl = URL + 'team/frc' + str(team_num) + '/events/keys'
print(fullurl)
result = get_request(fullurl)
sleep(0.25)
return result
def get_award_history(team_num):
'''
@param team_num:
@return: list of award dicts each containing award_type, event key, name, recipient_list, and year
{'award_type': 29,
'event_key': '2022mokc',
'name': 'Innovation in Control Award',
'recipient_list': [{'awardee': None, 'team_key': 'frc1939'}],
'year': 2022},
{'award_type': 3,
'event_key': '2022mokc',
'name': 'Woodie Flowers Finalist Award',
'recipient_list': [{'awardee': 'Brandon Huizenga', 'team_key': 'frc1939'}],
'year': 2022}]
'''
fullurl = URL + 'team/frc' + str(team_num) + '/awards'
print(fullurl)
result = get_request(fullurl)
sleep(0.25)
return result
def get_team_year(team_num, year):
'''
@param team_num:
@param year:
@return: list of event dicts each containing location information, eventdates, event type (code and string),
time zone, week number, webcast locations, and host website.
'''
fullurl = URL + 'team/frc' + str(team_num) + '/events/' + str(year)
print(fullurl)
result = get_request(fullurl)
return result
def get_event_list(year=THISYEAR):
'''
@param year:
@return: list of event dicts each containing location information, eventdates, event type (code and string),
time zone, week number, webcast locations, and host website.
'''
fullurl = URL + 'events/' + str(year)
print(fullurl)
result = get_request(fullurl)
return result
def get_event_year_keys(year=THISYEAR):
'''
@param year:
@return:list of event keys
'''
fullurl = URL + 'events/' + str(year) + '/keys'
print(fullurl)
result = get_request(fullurl)
return result
def get_event_teams(event, year=THISYEAR):
'''
@param event:
@param year:
@return: schema
[
{
"key": "string",
"team_number": 0,
"nickname": "string",
"name": "string",
"school_name": "string",
"city": "string",
"state_prov": "string",
"country": "string",
"address": "string",
"postal_code": "string",
"gmaps_place_id": "string",
"gmaps_url": "string",
"lat": 0,
"lng": 0,
"location_name": "string",
"website": "string",
"rookie_year": 0,
"motto": "string",
"home_championship": {}
}
]
'''
event_key = str(year) + event
fullurl = URL + 'event/' + event_key + '/teams'
print(fullurl)
result = get_request(fullurl)
return result
def get_event_teams_simple(event, year=THISYEAR):
'''
@param event:
@param year:
@return: schema
[
{
"key": "string",
"team_number": 0,
"nickname": "string",
"name": "string",
"city": "string",
"state_prov": "string",
"country": "string"
}
]
'''
event_key = str(year) + event
fullurl = URL + 'event/' + event_key + '/teams/simple'
print(fullurl)
result = get_request(fullurl)
return result
def get_event_matches(event, year=THISYEAR):
'''
@param event:
@param year:
@return: schema
[
{
"key": "string",
"comp_level": "qm",
"set_number": 0,
"match_number": 0,
"alliances": {
"red": {
"score": 0,
"team_keys": [
"string"
],
"surrogate_team_keys": [
"string"
],
"dq_team_keys": [
"string"
]
},
"blue": {
"score": 0,
"team_keys": [
"string"
],
"surrogate_team_keys": [
"string"
],
"dq_team_keys": [
"string"
]
}
},
"winning_alliance": "red",
"event_key": "string",
"time": 0,
"actual_time": 0,
"predicted_time": 0,
"post_result_time": 0,
"score_breakdown": {},
"videos": [
{
"type": "string",
"key": "string"
}
]
}
]
'''
event_key = str(year) + event
fullurl = URL + 'event/' + event_key + '/matches'
print(fullurl)
result = get_request(fullurl)
return result
def get_one_match(key):
fullurl = URL + 'match/' + key
print(fullurl)
result = get_request(fullurl)
return result
def get_event_insights(event, year=THISYEAR):
# OPR, DPR, CCWM
event_key = str(year) + event
fullurl = URL + 'event/' + event_key + '/insights'
print(fullurl)
result = get_request(fullurl)
return result
def get_event_awards(event, year=THISYEAR):
'''
@param event:
@param year:
@return:
[
{
"name": "string",
"award_type": 0,
"event_key": "string",
"recipient_list": [
{
"team_key": "string",
"awardee": "string"
}
],
"year": 0
}
]
'''
# www.thebluealliance.com/api/v2/event/<event key>/awards
event_key = str(year) + event
fullurl = URL + 'event/' + event_key + '/awards'
print(fullurl)
result = get_request(fullurl)
return result
def get_event_rankings(event, year=THISYEAR):
'''
@param event:
@param year:
@return:
{'extra_stats_info': [{'name': 'Total Ranking Points', 'precision': 0}],
'rankings': [{'dq': 0,
'extra_stats': [36],
'matches_played': 12,
'qual_average': None,
'rank': 1,
'record': {'losses': 2, 'ties': 0, 'wins': 10},
'sort_orders': [3.0, 69.08, 19.08, 21.66, 0.0, 0.0],
'team_key': 'frc1825'},...],
'sort_order_info': [{'name': 'Ranking Score', 'precision': 2},
{'name': 'Avg Match', 'precision': 2},
{'name': 'Avg Hangar', 'precision': 2},
{'name': 'Avg Taxi + Auto Cargo', 'precision': 2}]}
'''
# OPR, DPR, CCWM
event_key = str(year) + event
fullurl = URL + 'event/' + event_key + '/rankings'
print(fullurl)
result = get_request(fullurl)
return result
def get_event_oprs(event, year=THISYEAR):
event_key = str(year) + event
fullurl = URL + 'event/' + event_key + '/oprs'
print(fullurl)
result = get_request(fullurl)
return result
def make_eventweekmtx(year=THISYEAR):
eventlist = get_event_list(year)
matrix = {}
for event in eventlist:
matrix[event['event_code']] = event['week']
weeklist = {}
for event in matrix:
if matrix[event] is None:
week = 0
else:
week = matrix[event] + 1
if week not in weeklist:
weeklist[week] = []
weeklist[week].append(event)
return matrix, weeklist