-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhockeyface.py
executable file
·291 lines (253 loc) · 11.7 KB
/
hockeyface.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
#!/usr/bin/env python3
# from setup_logger import logger
import logging
logger = logging.getLogger(__name__)
class hockeyface(object):
def get_events(self, teams, leagues):
import time
now = int(time.time())
if (self.last_updated + 3600) <= now:
logger.debug(f"Invalidating cache (last_updated: {self.last_updated})")
self.__get_events()
if not teams:
return self.__filter_events([], leagues)
else:
return self.__filter_events(teams, leagues)
def __get_events(self):
import time
import requests
logger.info("Fetching events from upstream")
events = []
for league in ["SHL", "HA", "SDHL"]:
logger.debug(f"Processing {league}")
for season in self.league_information[league]["seasonUuids"]:
for gametype in self.league_information[league]["gameTypeUuids"]:
# Example URL
# https://www.hockeyallsvenskan.se/api/sports-v2/game-schedule?seasonUuid=qcz-3NvSZ2Cmh&seriesUuid=qQ9-594cW8OWD&gameTypeUuid=qQ9-af37Ti40B&gamePlace=all&played=all
r = requests.get(
f"{self.league_information[league]['baseurl']}seasonUuid={season}&seriesUuid={self.league_information[league]['seriesUuid']}&gameTypeUuid={gametype}&gamePlace={self.league_information[league]['gamePlace']}&played={self.league_information[league]['played']}"
)
if r.headers["content-type"] == "application/json; charset=utf-8":
returned_json = r.json()
if "gameInfo" in returned_json:
for event in returned_json["gameInfo"]:
startDateTime = event["rawStartDateTime"]
if "names" in event["homeTeamInfo"]:
home = event["homeTeamInfo"]["names"]["code"]
else:
home = "TBD"
if "names" in event["awayTeamInfo"]:
away = event["awayTeamInfo"]["names"]["code"]
else:
away = "TBD"
venue = ""
if "venueInfo" in event["venueInfo"]:
if "name" in event["venueInfo"]:
venue = event["venueInfo"]["name"]
events.append(
{
"startDateTime": startDateTime,
"home": home,
"away": away,
"venue": venue,
"league": league,
}
)
else:
logger.debug(f"{league} {season} {gametype} did not (yet?) respond with a json containing gameInfo")
else:
logger.debug(f"{league} {season} {gametype} did not (yet?) respond with a json")
for league in ["CHL"]:
logger.debug(f"Processing {league}")
for season in self.league_information[league]["seasonUuids"]:
# Example URL
# https://www.championshockeyleague.com/api/s3?q=schedule-21ec9dad81abe2e0240460d0-384dfd08cf1b5e6e93cd19ba.json
r = requests.get(f"{self.league_information[league]['baseurl']}schedule-{season}.json")
returned_json = r.json()
for event in returned_json["data"]:
startDateTime = event["startDate"]
home = event["teams"]["home"]["name"]
away = event["teams"]["away"]["name"]
venue = ""
if "ventue" in event:
venue = event["venue"]["name"]
events.append(
{
"startDateTime": startDateTime,
"home": home,
"away": away,
"venue": venue,
"league": league,
}
)
self.last_updated = int(time.time())
return events
def __filter_events(self, teams, leagues):
events_to_return = []
for event in self.events:
if teams:
if event["league"] not in leagues:
continue
if event["home"] in teams:
events_to_return.append(event)
elif event["away"] in teams:
events_to_return.append(event)
else:
match = False
for team in self.teamdata:
if team["key"] not in teams:
continue
name = team["name"]
if name == event["home"] or name == event["away"]:
logger.warning(f"Match in name for {name}: {event}")
events_to_return.append(event)
match = True
break
for name in team["alternateNames"]:
if name == event["home"] or name == event["away"]:
if team["key"] in teams:
logger.warning(f"Match in alternativeNames for {name}: {event}")
events_to_return.append(event)
match = True
break
if match == True:
break
# if match == False:
# logger.debug(event)
else:
if event["league"] in leagues:
events_to_return.append(event)
return events_to_return
def __build_teamdata(self):
import json
team_data_file = "static/teamdata.json"
with open(team_data_file) as json_file:
file_contents = json_file.read()
team_data = json.loads(file_contents)
return team_data
def __pp_team_name(self, short):
for team in self.teamdata:
if team["key"] == short:
return team["name"]
if team["name"] == short:
return team["name"]
for name in team["alternateNames"]:
if name == short:
return team["name"]
logger.warn(f"No match for '{short}'")
return short
def __pp_cal_name(self, teams):
return_dict = {
"CALNAME": "Svensk hockey",
"CALDESC": "Spelschema för svensk hockey",
}
if len(teams) == 1:
for team in self.teamdata:
if team["key"] == teams[0]:
return_dict = {
"CALNAME": f"{team['name']}",
"CALDESC": f"Spelschema för {team['name']}",
}
return return_dict
def build_ical(self, events, teams, leagues):
import uuid
from datetime import datetime, timedelta
import pytz
from icalendar import Calendar, Event
cal = Calendar()
cal.add("prodid", "-//Hockey McHF//Hockey McHockeyFace//EN")
cal.add("version", "2.0")
cal.add("CALSCALE", "GREGORIAN")
dstamp = datetime.now()
desc = self.__pp_cal_name(teams)
cal.add("X-WR-CALNAME", desc["CALNAME"])
cal.add("X-WR-CALDESC", desc["CALDESC"])
for event in events:
home = self.__pp_team_name(event["home"])
away = self.__pp_team_name(event["away"])
event_start = datetime.fromisoformat(event["startDateTime"]).astimezone(pytz.utc)
event_end = event_start + timedelta(minutes=150)
ical_event = Event()
prefix_string = ""
if ("SHL" in leagues and "SDHL" in leagues) or event["league"] == "CHL":
prefix_string = f"{event['league']}: "
ical_event.add("summary", f"{prefix_string}{home} - {away}")
ical_event.add("uid", uuid.uuid4())
ical_event.add("dtstamp", dstamp)
ical_event.add("dtstart", event_start)
ical_event.add("dtend", event_end)
ical_event.add("location", event["venue"])
cal.add_component(ical_event)
return cal.to_ical().decode("utf-8")
def __init__(self) -> None:
logger.debug("Hockey McHockeyFace initiated")
self.last_updated = 0
self.league_information = {
"SHL": {
"baseurl": "https://www.shl.se/api/sports-v2/game-schedule?",
"seriesUuid": "qQ9-bb0bzEWUk",
"gameTypeUuids": [
"qQ9-af37Ti40B", # Seriematch
"qRf-347BaDIOc", # Kvalmatch nedflyttning
"qQ9-7debq38kX", # Slutspelsmatch
"qQ9-46aa140wUl", # Play in-match
],
"gamePlace": "all",
"played": "all",
"seasonUuids": [
"qeb-73bZkIm9A", # 2024/2025
"qcz-3NvSZ2Cmh", # 2023/2024
"qbN-XMFfjGVt", # 2022/2023
"qZl-8qa6OaFXf", # 2021/2022
],
},
"HA": {
"baseurl": "https://www.hockeyallsvenskan.se/api/sports-v2/game-schedule?",
"seriesUuid": "qQ9-594cW8OWD",
"gameTypeUuids": [
"qQ9-af37Ti40B", # Seriematch
"qRe-AJnJ12qqEc", # Seriefinalmatch
"qRe-AJkH2owyv", # Slutspelsseriematch
"qQ9-7debq38kX", # Slutspelsmatch
"qRe-AJog2gISz", # Kvalmatch uppflyttning
"qRf-347BaDIOc", # Kvalmatch nedflyttning
"qQ9-be68b0QHe", # Vänskapsmatch
],
"gamePlace": "all",
"played": "all",
"seasonUuids": [
"qeb-73bZkIm9A", # 2024/2025
"qcz-3NvSZ2Cmh", # 2023/2024
"qbN-XMFfjGVt", # 2022/2023
"qZl-8qa6OaFXf", # 2021/2022
],
},
"CHL": {
"baseurl": "https://www.championshockeyleague.com/api/s3?q=",
"seasonUuids": [
"21ec9dad81abe2e0240460d0-384dfd08cf1b5e6e93cd19ba", # 2023/2024
"21ec9dad81abe2e0240460d0-42d2f45345814558d4daff38", # 2022/2023
"21ec9dad81abe2e0240460d0-f73bbb143cc88c3ebe188d77", # 2021/2022
],
},
"SDHL": {
"baseurl": "https://www.sdhl.se/api/sports-v2/game-schedule?",
"seriesUuid": "qQ9-f438G8BXP",
"gameTypeUuids": [
"qQ9-af37Ti40B", # Seriematch
"qRe-AJog2gISz", # Kvalmatch uppflyttning
"qRf-347BaDIOc", # Kvalmatch nedflyttning
"qQ9-7debq38kX", # Slutspelsmatch
],
"gamePlace": "all",
"played": "all",
"seasonUuids": [
"qeb-73bZkIm9A", # 2024/2025
"qcz-3NvSZ2Cmh", # 2023/2024
"qbN-XMFfjGVt", # 2022/2023
"qZl-8qa6OaFXf", # 2021/2022
],
},
}
self.events = self.__get_events()
self.teamdata = self.__build_teamdata()