File size: 878 Bytes
3f27d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from bs4 import BeautifulSoup
import logging

logger = logging.getLogger(__name__)

def get_fighter_image_url(fighter_name: str) -> str:
    """Get the UFC profile image URL for a fighter"""
    try:
        url = f'https://www.ufc.com/athlete/{fighter_name.replace(" ", "-").lower()}'
        response = requests.get(url)
        response.raise_for_status()

        soup = BeautifulSoup(response.content, 'html.parser')
        img_tags = soup.find_all('img', class_='hero-profile__image')
        
        if not img_tags:
            # Return a default image URL if no fighter image is found
            return "/default-fighter.png"
        
        return img_tags[0]['src']

    except requests.RequestException as e:
        print(f"Error fetching image for {fighter_name}: {str(e)}")
        raise Exception(f"Could not fetch image for {fighter_name}")