ufc-predictor / image_service.py
zjpiazza's picture
Added image service
3f27d87
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}")