Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions apps/f1results/f1_results.star
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Using different API lookup to check race calendar. This is to see how long since
Back to one API lookup now
Added handling for when not all 20 drivers start the race, eg Stroll in Spain 2025
Removed hard coding of Perez's name, not a driver anymore

1.6
Updated for 2026 season
"""

load("encoding/json.star", "json")
Expand All @@ -31,6 +34,8 @@ load("render.star", "render")
load("schema.star", "schema")
load("time.star", "time")

DEFAULT_TIMEZONE = "Australia/Adelaide"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The constant DEFAULT_TIMEZONE is defined but never used. Unused code should be removed to improve clarity and avoid confusion.


# Alternate URL thanks to @jvivona for the hosting :)
F1_URL = "https://raw.githubusercontent.com/jvivona/tidbyt-data/refs/heads/main/formula1/"

Expand All @@ -49,21 +54,21 @@ def main(config):
MyRaceTime = ""

timezone = time.tz()
now = time.now().in_location(timezone)
now = time.now()
Year = now.format("2006")

RACELIST_URL = F1_URL + "races.json"
GetLast = get_cachable_data(RACELIST_URL, 86400)
F1_LAST_JSON = json.decode(GetLast)
TotalRaces = len(F1_LAST_JSON["MRData"]["RaceTable"]["Races"])

# iterate through the race calendar
for x in range(0, len(F1_LAST_JSON["MRData"]["RaceTable"]["Races"]), 1):
for x in range(0, TotalRaces, 1):
LocalRaceDate = F1_LAST_JSON["MRData"]["RaceTable"]["Races"][x]["date"]
LocalRaceTime = F1_LAST_JSON["MRData"]["RaceTable"]["Races"][x]["time"]
RaceDate_Time = LocalRaceDate + " " + LocalRaceTime
FormatRTime = time.parse_time(RaceDate_Time, format = "2006-01-02 15:04:00Z").in_location(timezone)
RTimeDiff = FormatRTime - now
#print(RTimeDiff.hours)

# if we're more than 2hrs but less than 48hrs after the last race start get the race results, and break
# or if time next race is more than 0hrs, lets look ahead, and break when we find something
Expand Down Expand Up @@ -174,7 +179,7 @@ def main(config):
if Session == "R":
if ShowGap == True:
if ShowGrid == True:
for z in range(0, 20, 4):
for z in range(0, 22, 4):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The loop range is hardcoded with the magic number 22. This makes the code brittle if the number of drivers changes. It's better to get this number dynamically from the data. Since this loop is within if Session == "R":, you can use the length of the race results.

Suggested change
for z in range(0, 22, 4):
for z in range(0, len(F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"]), 4):

renderCategory.extend([
render.Column(
expanded = True,
Expand All @@ -198,7 +203,7 @@ def main(config):
),
])
elif ShowGrid == False:
for z in range(0, 20, 4):
for z in range(0, 22, 4):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The loop range is hardcoded with the magic number 22. This should be made dynamic based on the number of drivers in the results to make the code more robust against changes in the number of competitors.

Suggested change
for z in range(0, 22, 4):
for z in range(0, len(F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"]), 4):

renderCategory.extend([
render.Column(
expanded = True,
Expand All @@ -214,7 +219,7 @@ def main(config):

elif ShowGap == False:
if ShowGrid == True:
for z in range(0, 20, 4):
for z in range(0, 22, 4):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The loop range is hardcoded with the magic number 22. This should be made dynamic based on the number of drivers in the results to make the code more robust against changes in the number of competitors.

Suggested change
for z in range(0, 22, 4):
for z in range(0, len(F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"]), 4):

renderCategory.extend([
render.Column(
expanded = True,
Expand All @@ -228,7 +233,7 @@ def main(config):
),
])
elif ShowGrid == False:
for z in range(0, 20, 4):
for z in range(0, 22, 4):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The loop range is hardcoded with the magic number 22. This should be made dynamic based on the number of drivers in the results to make the code more robust against changes in the number of competitors.

Suggested change
for z in range(0, 22, 4):
for z in range(0, len(F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"]), 4):

renderCategory.extend([
render.Column(
expanded = True,
Expand All @@ -243,7 +248,7 @@ def main(config):
])

if Session == "Q":
for z in range(0, 20, 4):
for z in range(0, 22, 4):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The loop range is hardcoded with the magic number 22. Since this loop is for qualifying results (Session == "Q"), it's better to get the number of drivers dynamically from the qualifying results data to make the code more robust.

Suggested change
for z in range(0, 22, 4):
for z in range(0, len(F1_JSON["MRData"]["RaceTable"]["Races"][0]["QualifyingResults"]), 4):

renderCategory.extend([
render.Column(
expanded = True,
Expand Down Expand Up @@ -305,17 +310,19 @@ def getDriver(z, F1_JSON, Session):
output.extend(TitleRow)

for i in range(0, 4):
if i + z < len(F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"]):
if i + z < len(F1_JSON["MRData"]["RaceTable"]["Races"][0][SessionCode]):
DriverFont = "#fff"
Pos = F1_JSON["MRData"]["RaceTable"]["Races"][0][SessionCode][i + z]["position"]
DriverID = F1_JSON["MRData"]["RaceTable"]["Races"][0][SessionCode][i + z]["Driver"]["driverId"]

Driver = F1_JSON["MRData"]["RaceTable"]["Races"][0][SessionCode][i + z]["Driver"]["familyName"]
if DriverID == "perez":
Driver = "Perez"
elif DriverID == "hulkenberg":
Driver = "Hulkenberg"
else:
Driver = F1_JSON["MRData"]["RaceTable"]["Races"][0][SessionCode][i + z]["Driver"]["familyName"]
Comment on lines +318 to +323
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This if/elif/else block to handle specific driver names is not easily scalable. If more drivers need special name handling, you'll have to keep adding elif clauses. A more maintainable approach is to use a dictionary for these overrides.

For example:

DRIVER_NAME_OVERRIDES = {
    "perez": "Perez",
    "hulkenberg": "Hulkenberg",
}
Driver = DRIVER_NAME_OVERRIDES.get(DriverID, F1_JSON["MRData"]["RaceTable"]["Races"][0][SessionCode][i + z]["Driver"]["familyName"])

The DRIVER_NAME_OVERRIDES dictionary could be defined once as a constant outside the loop.

ConstructorID = F1_JSON["MRData"]["RaceTable"]["Races"][0][SessionCode][i + z]["Constructor"]["constructorId"]

# If its a Haas, use black color
if ConstructorID == "haas" or ConstructorID == "sauber":
DriverFont = "#000"

TeamColor = Team_Color(ConstructorID)

driver = render.Row(
Expand Down Expand Up @@ -371,7 +378,7 @@ def getDriverGaps(z, F1_JSON, Session):
output.extend(TitleRow)

for i in range(0, 4):
if i + z < 20:
if i + z < 22:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check i + z < 22 uses a hardcoded magic number 22. This should be based on the actual number of results from the API to avoid issues if the number of drivers changes.

Suggested change
if i + z < 22:
if i + z < len(F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"]):

DriverFont = "#fff"
Pos = F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["position"]
DriverCode = F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["Driver"]["code"]
Expand All @@ -382,6 +389,10 @@ def getDriverGaps(z, F1_JSON, Session):
Time = "DNF"
elif F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["positionText"] == "D":
Time = "DQ"
elif F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["positionText"] == "W":
Time = "DNS"
elif F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["status"] == "Lapped":
Time = "LAP"
else:
Time = F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["status"]
Time = Time[:4]
Expand All @@ -397,10 +408,6 @@ def getDriverGaps(z, F1_JSON, Session):
Time = ""
ConstructorID = F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["Constructor"]["constructorId"]

# If its a Haas, use black color
if ConstructorID == "haas":
DriverFont = "#000"

TeamColor = Team_Color(ConstructorID)

driver = render.Row(
Expand Down Expand Up @@ -471,7 +478,7 @@ def getDriverGrid(z, F1_JSON, Session):
output.extend(TitleRow)

for i in range(0, 4):
if i + z < 20:
if i + z < 22:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check i + z < 22 uses a hardcoded magic number 22. This should be based on the actual number of results from the API to avoid issues if the number of drivers changes.

Suggested change
if i + z < 22:
if i + z < len(F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"]):

DriverFont = "#fff"
Pos = F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["position"]
DriverCode = F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["Driver"]["code"]
Expand All @@ -489,10 +496,6 @@ def getDriverGrid(z, F1_JSON, Session):

ConstructorID = F1_JSON["MRData"]["RaceTable"]["Races"][0]["Results"][i + z]["Constructor"]["constructorId"]

# If its a Haas, use black color
if ConstructorID == "haas":
DriverFont = "#000"

TeamColor = Team_Color(ConstructorID)

driver = render.Row(
Expand Down Expand Up @@ -556,16 +559,18 @@ def Team_Color(ConstructorID):
return ("#0f1c2c")
if ConstructorID == "mclaren":
return ("#fd8000")
if ConstructorID == "sauber":
return ("#00df00")
if ConstructorID == "audi":
return ("#901900")
if ConstructorID == "aston_martin":
return ("#015850")
if ConstructorID == "haas":
return ("#f7f7f7")
return ("#818b8e")
if ConstructorID == "rb":
return ("#022948")
if ConstructorID == "williams":
return ("#041e41")
if ConstructorID == "cadillac":
return ("#4a4a4c")
else:
return ("#fff")

Expand Down