Skip to content

Commit 485b426

Browse files
authored
Merge branch 'master' into claire/basketballScoreBreakdown
2 parents e848140 + 59175e0 commit 485b426

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

src/services/game_service.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from src.models.game import Game
33
from src.services.team_service import TeamService
44
from src.utils.helpers import is_tournament_placeholder_team
5+
from pymongo.errors import DuplicateKeyError
56

67

78
class GameService:
@@ -36,8 +37,11 @@ def create_game(data):
3637
raise ValueError(f"Opponent team with id {opponent_id} does not exist.")
3738

3839
game = Game(**data)
39-
GameRepository.insert(game)
40-
return game
40+
try:
41+
GameRepository.insert(game)
42+
return game
43+
except DuplicateKeyError:
44+
return None
4145

4246
@staticmethod
4347
def delete_game(game_id):

src/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class YoutubeVideoType(ObjectType):
156156
- url: The URL to the video.
157157
- published_at: The date and time the video was published.
158158
- duration: The duration of the video (optional).
159+
- sportsType: The sport type extracted from the video title.
159160
"""
160161
id = String(required=False)
161162
title = String(required=True)
@@ -165,11 +166,19 @@ class YoutubeVideoType(ObjectType):
165166
url = String(required=True)
166167
published_at = String(required=True)
167168
duration = String(required=False)
169+
sportsType = String(required=False)
168170

169171
def __init__(self, **kwargs):
170172
for key, value in kwargs.items():
171173
setattr(self, key, value)
172174

175+
def resolve_sportsType(video, info):
176+
"""
177+
Resolver to extract sport type from the video title.
178+
"""
179+
from src.utils.helpers import extract_sport_from_title
180+
return extract_sport_from_title(video.title)
181+
173182
class ArticleType(ObjectType):
174183
"""
175184
A GraphQL type representing a news article.

src/utils/helpers.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from PIL import Image
44
from io import BytesIO
55
from collections import Counter
6+
import re
67

78

89
def get_dominant_color(image_url, white_threshold=200, black_threshold=50):
@@ -96,6 +97,53 @@ def is_cornell_loss(result: str):
9697
loss_indicators = ["L", "Loss", "loss", "Defeated", "defeated"]
9798
return any(indicator in result for indicator in loss_indicators)
9899

100+
def extract_sport_from_title(title):
101+
"""
102+
Extracts the sport type from a YouTube video title.
103+
104+
Args:
105+
title (str): The title of the YouTube video
106+
107+
Returns:
108+
str: The sport type if found, None otherwise
109+
"""
110+
if not title:
111+
return None
112+
113+
title_lower = title.lower()
114+
115+
sport_patterns = [
116+
# Ice Hockey
117+
(r"ice\s+hockey", "Ice Hockey"),
118+
(r"women'?s\s+ice\s+hockey", "Ice Hockey"),
119+
(r"men'?s\s+ice\s+hockey", "Ice Hockey"),
120+
# Field Hockey
121+
(r"field\s+hockey", "Field Hockey"),
122+
# Hockey
123+
(r"\bhockey\b", "Ice Hockey"),
124+
# Basketball
125+
(r"basketball", "Basketball"),
126+
# Football
127+
(r"\bfootball\b", "Football"),
128+
# Soccer
129+
(r"\bsoccer\b", "Soccer"),
130+
# Volleyball
131+
(r"volleyball", "Volleyball"),
132+
# Wrestling
133+
(r"wrestling", "Wrestling"),
134+
# Sprint Football
135+
(r"sprint\s+football", "Sprint Football"),
136+
]
137+
138+
for pattern, sport_name in sport_patterns:
139+
if re.search(pattern, title_lower):
140+
return sport_name
141+
142+
if "ice" in title_lower and ("hockey" in title_lower or "cornell" in title_lower):
143+
return "Ice Hockey"
144+
145+
return None
146+
99147
def extract_sport_type_from_title(title: str):
100148
"""
101149
Extract the sport type from an article title by matching against known sports.

0 commit comments

Comments
 (0)