-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
27 lines (21 loc) · 972 Bytes
/
util.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
from base64 import b64encode
import requests
import models
def get_rankings(season: int, district: str, api_key: str) -> list[models.DivisionTeam]:
rankings = []
current_page = 1
total_pages = 99
api_headers = {
"Authorization": f"Basic {b64encode(api_key.encode()).decode()}",
"Accept": "application/json"
}
while current_page <= total_pages:
resp = requests.get(f"https://frc-api.firstinspires.org/v3.0/{season}/rankings/district?districtCode={district}&page={current_page}", headers=api_headers)
if not resp.ok:
raise Exception("Got a bad response from the FRC API: " + str(resp.status_code) + " - " + resp.text)
resp = resp.json()
if current_page == 1:
total_pages = resp["pageTotal"]
rankings += [models.DivisionTeam(t["teamNumber"], t["totalPoints"], t["qualifiedDistrictCmp"]) for t in resp["districtRanks"]]
current_page += 1
return rankings