-
Notifications
You must be signed in to change notification settings - Fork 0
/
TBAData.py
161 lines (140 loc) · 6.09 KB
/
TBAData.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
import time
import pickle
import multiprocessing as multip
import numpy as np
import tbaapiv3client
from MatchData import MatchData
class TBAData:
def __init__(self):
# API Key Authorization
configuration = tbaapiv3client.Configuration(
host="https://www.thebluealliance.com/api/v3",
api_key={
"X-TBA-Auth-Key": ""
# TODO get api key from .env
},
)
# Get API instances
with tbaapiv3client.ApiClient(configuration) as api_client:
self.matches_api_instance = tbaapiv3client.EventApi(api_client)
self.match_api_instance = tbaapiv3client.MatchApi(api_client)
self.event_api_instance = tbaapiv3client.EventApi(api_client)
# Set constants
self.AT_EVENT_KEY = "2024cc"
self.CURRENT_EVENT_KEYS = ["2024cc"]
self.CURRENT_YEAR_KEY = "2024"
self.EVENT_KEYS = self.event_api_instance.get_events_by_year_keys(
int(self.CURRENT_YEAR_KEY)
)
# Initialize Fields
self.event_to_match_data = {}
self.current_event_data = []
self.at_event_data = []
def load_events(self, file):
print("Loading Events...")
currentTime = time.time()
eventsDone = 0
totalEvents = len(self.EVENT_KEYS)
match_to_thread = {}
# for eventKey in self.EVENT_KEYS:
# print("Requesting Event: " + eventKey)
# matches = self.get_event_matches(eventKey)
# for match in matches:
# match_to_thread[match.key] = self.match_api_instance.get_match_zebra(match.key, async_req=True)
# eventsDone = eventsDone + 1
# print(eventsDone / totalEvents * 100, "% of Events Requested")
# print("--------------------------------------------------------")
# print("Loading Event Matches Took: " + str(time.time() - currentTime))
# currentTime = time.time()
# eventsDone = 0
# for eventKey in self.EVENT_KEYS:
# self.event_to_match_data[eventKey] = {}
# for match in self.get_event_matches(eventKey):
# self.event_to_match_data[eventKey][match.key] = MatchData(match, match_to_thread[match.key].get())
# eventsDone = eventsDone + 1
# print(eventsDone / totalEvents * 100, "% of Events Loaded")
# for eventKey in self.EVENT_KEYS:
for eventKey in self.EVENT_KEYS:
print("Requesting Event: " + eventKey)
matches = self.get_event_matches(eventKey)
self.event_to_match_data[eventKey] = {}
for match in matches:
self.event_to_match_data[eventKey][match.key] = MatchData(
match, self.match_api_instance.get_match_zebra(match.key)
)
eventsDone = eventsDone + 1
print(eventsDone / totalEvents * 100, "% of Events Requested")
print("--------------------------------------------------------")
print("Loading Event Matches Took: " + str(time.time() - currentTime))
currentTime = time.time()
eventsDone = 0
for eventKey in self.EVENT_KEYS:
print("Loading Event: " + eventKey)
self.event_to_match_data[eventKey] = {}
for match in self.get_event_matches(eventKey):
self.event_to_match_data[eventKey][match.key] = MatchData(
match, match_to_thread[match.key].get()
)
eventsDone = eventsDone + 1
print(eventsDone / totalEvents * 100, "% of Events Loaded")
self.current_event_data = [
self.event_to_match_data[key] for key in self.CURRENT_EVENT_KEYS
]
self.at_event_data = self.event_to_match_data[self.AT_EVENT_KEY]
print(
"Loading Event Matches Took: " + str(time.time() - currentTime) + " Seconds"
)
print("Writing to File...")
self.write_to_file(file)
print("Writing to File Complete")
def get_event_matches(self, event_key):
matches = self.matches_api_instance.get_event_matches(event_key)
return matches
def write_to_file(self, file):
dataFile = open(file, "wb")
pickle.dump(self.event_to_match_data, dataFile)
dataFile.close()
def load_from_file(self, file):
currentTime = time.time()
print("Loading Data From File...")
dataFile = open(file, "rb")
self.event_to_match_data = pickle.load(dataFile)
dataFile.close()
self.current_event_data = [
self.event_to_match_data[key] for key in self.CURRENT_EVENT_KEYS
]
self.at_event_data = self.event_to_match_data[self.AT_EVENT_KEY]
print(
"Loading Data From File Complete Took: "
+ str(time.time() - currentTime)
+ " Seconds"
)
def update_current_event_data(self, file):
print("Updating Data...")
totalEvents = len(self.CURRENT_EVENT_KEYS)
currentTime = time.time()
eventsDone = 0
for eventKey in self.CURRENT_EVENT_KEYS:
self.event_to_match_data[eventKey] = {}
for match in self.get_event_matches(eventKey):
self.event_to_match_data[eventKey][match.key] = MatchData(match, None)
eventsDone = eventsDone + 1
print(eventsDone / totalEvents * 100, "% of Events Loaded")
self.current_event_data = [
self.event_to_match_data[key] for key in self.CURRENT_EVENT_KEYS
]
self.at_event_data = self.event_to_match_data[self.AT_EVENT_KEY]
print(
"Loading Event Matches Took: " + str(time.time() - currentTime) + " Seconds"
)
# print(self.event_to_match_data["2024pncmp"])
print("Writing to File...")
self.write_to_file(file)
print("Writing to File Complete")
print(
"Updated Current Event Data(took "
+ str(time.time() - currentTime)
+ " Seconds)"
)
def get_match_data(self, event_key, match_key):
return self.event_to_match_data[event_key][match_key]