BOXTRON-AI / openboxing_api.py
4therapy's picture
Update openboxing_api.py
443726d verified
Raw
History Blame Contribute Delete
1.6 kB
# openboxing_api.py
# openboxing_api.py
import requests
BASE_URL = "https://openboxing.org/api"
def get_all_champions():
"""
Fetch all champions/fighters from Open Boxing API.
Returns a list of fighter dictionaries.
"""
url = f"{BASE_URL}/champions/all.json"
response = requests.get(url)
if response.status_code == 200:
return response.json()
return []
def find_champion_by_name(all_champs, name: str):
"""
Search for a fighter/champion by name.
Returns the first match dict or None if not found.
"""
name_lower = name.lower().strip()
for champ in all_champs:
first = champ['name'].get('first', '').lower()
last = champ['name'].get('last', '').lower()
full_name = f"{first} {last}".strip()
if name_lower in full_name:
print(champ)
return champ
return None
def get_all_bouts():
url = f"{BASE_URL}/bouts/all.json"
response = requests.get(url)
if response.status_code != 200:
return []
bouts = response.json()
return bouts
def get_bouts_for_champion(bouts, champion_id: int):
"""
Fetch all bouts/fights for a given champion ID.
Returns a list of bout dictionaries.
"""
champ_bouts = []
for bout in bouts:
boxers = bout.get("boxers", {})
boxerA = boxers.get("boxerA", {})
boxerB = boxers.get("boxerB", {})
if boxerA.get("championId") == champion_id or boxerB.get("championId") == champion_id:
print(boxerA, boxerB)
champ_bouts.append(bout)
return champ_bouts