4therapy commited on
Commit
6ee808a
·
verified ·
1 Parent(s): 63a866c

Update openboxing_api.py

Browse files
Files changed (1) hide show
  1. 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" # official Open Boxing API base
5
 
6
- def get_fighters():
7
  """
8
- Fetch all fighters. Returns a list of fighter dicts.
 
9
  """
10
- url = f"{BASE_URL}/champions/all.json" # or correct endpoint
11
  response = requests.get(url)
12
  if response.status_code == 200:
13
- return response.json() # usually a list of fighters
14
  return []
15
 
16
- def search_fighter(fighter_name: str):
17
  """
18
- Search for a fighter by name. Returns first match or None.
 
19
  """
20
- fighters = get_fighters()
21
- for f in fighters:
22
- if fighter_name.lower() in f["name"].lower():
23
- return f
 
 
 
 
24
  return None
25
 
26
- def get_fights(fighter_id: str):
27
  """
28
- Fetch fight history for a given fighter.
29
- Note: Open Boxing returns bouts list; adjust as needed.
30
  """
31
  url = f"{BASE_URL}/bouts/all.json"
32
  response = requests.get(url)
33
- fights = []
34
- if response.status_code == 200:
35
- data = response.json()
36
- for bout in data:
37
- if fighter_id in [bout.get("red_id"), bout.get("blue_id")]:
38
- fights.append(bout)
39
- return fights
 
 
 
 
 
 
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