-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadcomps.py
executable file
·86 lines (63 loc) · 2.01 KB
/
loadcomps.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
#!/usr/bin/env python3
import json
import requests
from google.cloud import firestore
from functools import lru_cache
from datetime import datetime
"""
Script to push data from The Blue Alliance into the Trisonic's Firestore.
We'll take a competition name and populate the available teams and match
numbers that they're in.
"""
@lru_cache(maxsize=2)
def _get_read_key():
with open('tbaread.key') as f:
key = f.read().strip()
return key
def _tba_url():
return 'https://www.thebluealliance.com/api/v3'
def get_headers():
api_headers = {'X-TBA-Auth-Key': _get_read_key()}
return api_headers
def get_events(year):
# JJB: Ick. Fix this.
full_url = f"{_tba_url()}/events/2020"
d = requests.get(full_url, headers=get_headers())
return d.json()
def get_teams_at_event(event_key):
full_url = f"{_tba_url()}/event/{event_key}/teams"
d = requests.get(full_url, headers=get_headers())
return d.json()
def get_matches_at_event(event_key):
full_url = f"{_tba_url()}/event/{event_key}/matches"
d = requests.get(full_url, headers=get_headers())
return d.json()
def pretty_json(json_data):
return json.dumps(json_data, indent=4, sort_keys=True)
def load_event(year, compname):
year = str(year)
compname = str(compname)
db = firestore.Client()
comp_col = db.collection('competitions')\
.document(year)\
.collection(compname)
teams = get_teams_at_event(f'{year}{compname}')
for t in teams:
num = t['team_number']
name = t['nickname']
school = t['school_name']
print(f"{num} / {name} / {school}")
tdata = {}
tdata['team_name'] = name
tdata['school_name'] = school
tdata['sort_order'] = int(num)
comp_col.document(str(num)).set(tdata)
def show_events():
events = get_events(2020)
print(events)
if __name__ == '__main__':
import sys
year = datetime.now().year
show_events()
compname = sys.argv[1] or ''
load_event(year, compname)