Spaces:
Sleeping
Sleeping
Update openboxing_api.py
Browse files- openboxing_api.py +35 -21
openboxing_api.py
CHANGED
|
@@ -1,40 +1,54 @@
|
|
| 1 |
# openboxing_api.py
|
|
|
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
BASE_URL = "https://openboxing.org/api"
|
| 5 |
|
| 6 |
-
def
|
| 7 |
"""
|
| 8 |
-
Fetch all fighters
|
|
|
|
| 9 |
"""
|
| 10 |
-
url = f"{BASE_URL}/champions/all.json"
|
| 11 |
response = requests.get(url)
|
| 12 |
if response.status_code == 200:
|
| 13 |
-
return response.json()
|
| 14 |
return []
|
| 15 |
|
| 16 |
-
def
|
| 17 |
"""
|
| 18 |
-
Search for a fighter by name.
|
|
|
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return None
|
| 25 |
|
| 26 |
-
def
|
| 27 |
"""
|
| 28 |
-
Fetch
|
| 29 |
-
|
| 30 |
"""
|
| 31 |
url = f"{BASE_URL}/bouts/all.json"
|
| 32 |
response = requests.get(url)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
|
|
| 1 |
# openboxing_api.py
|
| 2 |
+
# openboxing_api.py
|
| 3 |
import requests
|
| 4 |
|
| 5 |
+
BASE_URL = "https://openboxing.org/api"
|
| 6 |
|
| 7 |
+
def get_all_champions():
|
| 8 |
"""
|
| 9 |
+
Fetch all champions/fighters from Open Boxing API.
|
| 10 |
+
Returns a list of fighter dictionaries.
|
| 11 |
"""
|
| 12 |
+
url = f"{BASE_URL}/champions/all.json"
|
| 13 |
response = requests.get(url)
|
| 14 |
if response.status_code == 200:
|
| 15 |
+
return response.json()
|
| 16 |
return []
|
| 17 |
|
| 18 |
+
def find_champion_by_name(name: str):
|
| 19 |
"""
|
| 20 |
+
Search for a fighter/champion by name.
|
| 21 |
+
Returns the first match dict or None if not found.
|
| 22 |
"""
|
| 23 |
+
all_champs = get_all_champions()
|
| 24 |
+
name_lower = name.lower()
|
| 25 |
+
for champ in all_champs:
|
| 26 |
+
first = champ['name'].get('first', '').lower()
|
| 27 |
+
last = champ['name'].get('last', '').lower()
|
| 28 |
+
full_name = f"{first} {last}".strip()
|
| 29 |
+
if name_lower in full_name:
|
| 30 |
+
return champ
|
| 31 |
return None
|
| 32 |
|
| 33 |
+
def get_bouts_for_champion(champion_id: int):
|
| 34 |
"""
|
| 35 |
+
Fetch all bouts/fights for a given champion ID.
|
| 36 |
+
Returns a list of bout dictionaries.
|
| 37 |
"""
|
| 38 |
url = f"{BASE_URL}/bouts/all.json"
|
| 39 |
response = requests.get(url)
|
| 40 |
+
if response.status_code != 200:
|
| 41 |
+
return []
|
| 42 |
+
|
| 43 |
+
bouts = response.json()
|
| 44 |
+
champ_bouts = []
|
| 45 |
+
|
| 46 |
+
for bout in bouts:
|
| 47 |
+
boxers = bout.get("boxers", {})
|
| 48 |
+
boxerA = boxers.get("boxerA", {})
|
| 49 |
+
boxerB = boxers.get("boxerB", {})
|
| 50 |
+
|
| 51 |
+
if boxerA.get("championId") == champion_id or boxerB.get("championId") == champion_id:
|
| 52 |
+
champ_bouts.append(bout)
|
| 53 |
|
| 54 |
+
return champ_bouts
|