Skip to content

Commit c6ddbde

Browse files
authored
Merge pull request #2627 from adafruit/espn_update
Adding more error catching
2 parents c205756 + 684bb6d commit c6ddbde

File tree

1 file changed

+41
-2
lines changed
  • Matrix_Portal_S3_ESPN_API/CircuitPython

1 file changed

+41
-2
lines changed

Matrix_Portal_S3_ESPN_API/CircuitPython/code.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def get_data(data, team, logo, group):
188188
names = []
189189
scores = []
190190
info = []
191+
index = 0
191192
# the team you are following's logo
192193
bitmap0 = displayio.OnDiskBitmap(logo)
193194
grid0 = displayio.TileGrid(bitmap0, pixel_shader=bitmap0.pixel_shader, x = 2)
@@ -227,11 +228,40 @@ def get_data(data, team, logo, group):
227228
# gets info on game
228229
info.append(event["status"]["type"]["shortDetail"])
229230
break
231+
if playing and len(names) != 2:
232+
print("did not get expected response, fetching full JSON..")
233+
try:
234+
resp.close()
235+
# pylint: disable=broad-except
236+
except Exception as e:
237+
print(f"{e}, continuing..")
238+
# pylint: disable=unnecessary-pass
239+
pass
240+
names.clear()
241+
scores.clear()
242+
info.clear()
243+
resp = requests.get(data)
244+
response_as_json = resp.json()
245+
for e in response_as_json["events"]:
246+
if team[0] in e["name"]:
247+
print(index)
248+
info.append(response_as_json["events"][0]["date"])
249+
names.append(response_as_json["events"][0]["competitions"]
250+
[0]["competitors"][0]["team"]["abbreviation"])
251+
names.append(response_as_json["events"][0]["competitions"]
252+
[0]["competitors"][1]["team"]["abbreviation"])
253+
scores.append(response_as_json["events"][0]["competitions"]
254+
[0]["competitors"][0]["score"])
255+
scores.append(response_as_json["events"][0]["competitions"]
256+
[0]["competitors"][1]["score"])
257+
info.append(response_as_json["events"][0]["status"]["type"]["shortDetail"])
258+
else:
259+
index += 1
230260
# debug printing
231261
print(names)
232262
print(scores)
233263
print(info)
234-
if playing:
264+
if playing and len(names) == 2:
235265
# pull out the date
236266
date = info[0]
237267
# convert it to be readable
@@ -301,7 +331,16 @@ def get_data(data, team, logo, group):
301331
group.append(vs_text)
302332
group.append(info_text)
303333
# close the response
304-
resp.close()
334+
try:
335+
# sometimes an OSError is thrown:
336+
# "invalid syntax for integer with base 16"
337+
# the code can continue depite it though
338+
resp.close()
339+
# pylint: disable=broad-except
340+
except Exception as e:
341+
print(f"{e}, continuing..")
342+
# pylint: disable=unnecessary-pass
343+
pass
305344
pixel.fill((0, 0, 0))
306345
# return that data was just fetched
307346
fetch_status = True

0 commit comments

Comments
 (0)