Skip to content

Commit 140ea40

Browse files
Merge pull request #26 from cuappdev/skye/temp-fix-dupes
Fix dupe games
2 parents 6b13b26 + e7d7595 commit 140ea40

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

src/repositories/game_repository.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ def find_by_data(city, date, gender, location, opponent_id, sport, state, time):
7373
}
7474
)
7575
return Game.from_dict(game_data) if game_data else None
76+
77+
@staticmethod
78+
def find_by_key_fields(city, date, gender, location, opponent_id, sport, state):
79+
"""
80+
Find games without time for duplicate games
81+
"""
82+
game_collection = db["game"]
83+
games = list(game_collection.find({
84+
"city": city,
85+
"date": date,
86+
"gender": gender,
87+
"location": location,
88+
"opponent_id": opponent_id,
89+
"sport": sport,
90+
"state": state,
91+
}))
92+
93+
if not games:
94+
return None
95+
96+
if len(games) == 1:
97+
return Game.from_dict(games[0])
98+
99+
return [Game.from_dict(game) for game in games]
76100

77101
@staticmethod
78102
def find_by_sport(sport):

src/scrapers/games_scraper.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -192,41 +192,27 @@ def process_game_data(game_data):
192192
if game_time is None:
193193
game_time = "TBD"
194194

195-
curr_game = GameService.get_game_by_data(
195+
# finds any existing game with the same key fields regardless of time
196+
curr_game = GameService.get_game_by_key_fields(
196197
city,
197198
game_data["date"],
198199
game_data["gender"],
199200
location,
200201
team.id,
201202
game_data["sport"],
202-
state,
203-
game_time,
203+
state
204204
)
205205
if curr_game:
206-
if curr_game.result != game_data["result"]:
207-
GameService.update_game(curr_game.id, {"result": game_data["result"]})
208-
if curr_game.box_score != game_data["box_score"]:
209-
GameService.update_game(curr_game.id, {"box_score": game_data["box_score"]})
210-
GameService.update_game(curr_game.id, {"score_breakdown": game_data["score_breakdown"]})
211-
if utc_date_str:
212-
GameService.update_game(curr_game.id, {"utc_date": utc_date_str})
206+
updates = {
207+
"time": game_time,
208+
"result": game_data["result"],
209+
"box_score": game_data["box_score"],
210+
"score_breakdown": game_data["score_breakdown"],
211+
"utc_date": utc_date_str
212+
}
213+
GameService.update_game(curr_game.id, updates)
213214
return
214-
# if game time is not TBD, check for existing game with TBD time to update
215-
if game_time != "TBD":
216-
curr_game = GameService.get_game_by_data(
217-
city,
218-
game_data["date"],
219-
game_data["gender"],
220-
location,
221-
team.id,
222-
game_data["sport"],
223-
state,
224-
"TBD",
225-
)
226-
if curr_game:
227-
GameService.update_game(curr_game.id, {"time": game_time})
228-
GameService.update_game(curr_game.id, {"utc_date": utc_date_str})
229-
return
215+
230216
game_data = {
231217
"city": city,
232218
"date": game_data["date"],

src/services/game_service.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ def get_game_by_data(city, date, gender, location, opponent_id, sport, state, ti
5252
return GameRepository.find_by_data(
5353
city, date, gender, location, opponent_id, sport, state, time
5454
)
55+
56+
@staticmethod
57+
def get_game_by_key_fields(city, date, gender, location, opponent_id, sport, state):
58+
"""
59+
Retrieve a game by its essential fields, ignoring time
60+
"""
61+
return GameRepository.find_by_key_fields(
62+
city, date, gender, location, opponent_id, sport, state
63+
)
5564

5665
@staticmethod
5766
def get_games_by_sport(sport):

0 commit comments

Comments
 (0)