Updating file parsing
This commit is contained in:
@@ -29,13 +29,12 @@ class Importer:
|
||||
src_file = os.path.join(source_dir, filename)
|
||||
dest_file = os.path.join(dest_dir, filename)
|
||||
|
||||
self.parse_one_file(f"{source_dir}/{filename}")
|
||||
|
||||
try:
|
||||
shutil.copy(src_file, dest_file)
|
||||
print(f"Copied {filename} to {dest_dir}")
|
||||
except Exception as e:
|
||||
print(f"Failed to copy {filename}: {e}")
|
||||
if self.parse_one_file(f"{source_dir}/{filename}"):
|
||||
try:
|
||||
shutil.copy(src_file, dest_file)
|
||||
print(f"Copied {filename} to {dest_dir}")
|
||||
except Exception as e:
|
||||
print(f"Failed to copy {filename}: {e}")
|
||||
|
||||
def parse_one_file(self, filepath):
|
||||
bb_dict = {}
|
||||
@@ -44,15 +43,18 @@ class Importer:
|
||||
bb_dict = list(reader)
|
||||
|
||||
for game in bb_dict:
|
||||
self.populate_database_with_stats(game)
|
||||
if not self.populate_database_with_stats(game):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def populate_database_with_stats(self, game_stats):
|
||||
def populate_database_with_stats(self, game_stats) -> bool:
|
||||
parkid = game_stats["park-id"]
|
||||
park_data = self.database.select("SELECT latitude, longitude FROM parks WHERE park_id = ?", (game_stats["park-id"],))
|
||||
|
||||
if park_data is None:
|
||||
print(f"{parkid} is None")
|
||||
return
|
||||
return True
|
||||
|
||||
insert_game = """
|
||||
INSERT INTO games
|
||||
@@ -79,19 +81,6 @@ class Importer:
|
||||
)
|
||||
"""
|
||||
|
||||
game_data = [
|
||||
game_stats["date"], game_stats["num-of-game"], game_stats["day-of-week"],
|
||||
game_stats["length-in-outs"], game_stats["day-night"], game_stats["completion-info"],
|
||||
game_stats["forfeit"], game_stats["protest"], game_stats["park-id"],
|
||||
game_stats["attendance"], game_stats["length-in-min"], game_stats["home-plate-ump-id"],
|
||||
game_stats["home-plate-ump-name"], game_stats["1b-plate-ump-id"], game_stats["1b-plate-ump-name"],
|
||||
game_stats["2b-plate-ump-id"], game_stats["2b-plate-ump-name"], game_stats["3b-plate-ump-id"],
|
||||
game_stats["3b-plate-ump-name"], game_stats["lf-plate-ump-id"], game_stats["lf-plate-ump-name"],
|
||||
game_stats["rf-plate-ump-id"], game_stats["rf-plate-ump-name"],
|
||||
]
|
||||
|
||||
game_id = self.database.insert(insert_game, game_data)
|
||||
|
||||
insert_team_game = """
|
||||
INSERT INTO team_game
|
||||
(
|
||||
@@ -145,6 +134,53 @@ class Importer:
|
||||
)
|
||||
"""
|
||||
|
||||
insert_into_weather = """
|
||||
INSERT INTO weather
|
||||
(
|
||||
game_id, temperature, humidity,
|
||||
dew_point, apparent_temperature, air_pressure,
|
||||
precipitation, rain, snowfall,
|
||||
cloud_cover, wind_speed, wind_direction,
|
||||
wind_gusts, sun_rise, sun_set,
|
||||
moon_phase
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?
|
||||
)
|
||||
"""
|
||||
|
||||
hour = 15 if game_stats["day-night"] == "D" else 19
|
||||
historic_weather = get_weather(park_data[0], park_data[1], game_stats["date"], hour)
|
||||
|
||||
if "error" in historic_weather:
|
||||
print(f"Error: {historic_weather['error']}: Details: {historic_weather['details']}")
|
||||
return False
|
||||
|
||||
if "hourly" not in historic_weather:
|
||||
print(f"Failed to get weather: Full JSON: {historic_weather}")
|
||||
return False
|
||||
|
||||
historic_weather = historic_weather["hourly"]
|
||||
|
||||
game_data = [
|
||||
game_stats["date"], game_stats["num-of-game"], game_stats["day-of-week"],
|
||||
game_stats["length-in-outs"], game_stats["day-night"], game_stats["completion-info"],
|
||||
game_stats["forfeit"], game_stats["protest"], game_stats["park-id"],
|
||||
game_stats["attendance"], game_stats["length-in-min"], game_stats["home-plate-ump-id"],
|
||||
game_stats["home-plate-ump-name"], game_stats["1b-plate-ump-id"], game_stats["1b-plate-ump-name"],
|
||||
game_stats["2b-plate-ump-id"], game_stats["2b-plate-ump-name"], game_stats["3b-plate-ump-id"],
|
||||
game_stats["3b-plate-ump-name"], game_stats["lf-plate-ump-id"], game_stats["lf-plate-ump-name"],
|
||||
game_stats["rf-plate-ump-id"], game_stats["rf-plate-ump-name"],
|
||||
]
|
||||
|
||||
game_id = self.database.insert(insert_game, game_data)
|
||||
|
||||
visiting_team_data = [
|
||||
game_id, game_stats["visiting-team"], game_stats["visiting-game-num"],
|
||||
game_stats["visiting-score"], game_stats["visiting-line-scores"], game_stats["visiting-at-bats"],
|
||||
@@ -198,31 +234,6 @@ class Importer:
|
||||
self.database.insert(insert_team_game, visiting_team_data)
|
||||
self.database.insert(insert_team_game, home_team_data)
|
||||
|
||||
hour = 15 if game_stats["day-night"] == "D" else 19
|
||||
historic_weather = get_weather(park_data[0], park_data[1], game_stats["date"], hour)
|
||||
historic_weather = historic_weather["hourly"]
|
||||
|
||||
insert_into_weather = """
|
||||
INSERT INTO weather
|
||||
(
|
||||
game_id, temperature, humidity,
|
||||
dew_point, apparent_temperature, air_pressure,
|
||||
precipitation, rain, snowfall,
|
||||
cloud_cover, wind_speed, wind_direction,
|
||||
wind_gusts, sun_rise, sun_set,
|
||||
moon_phase
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?, ?, ?,
|
||||
?
|
||||
)
|
||||
"""
|
||||
|
||||
(sunrise_time, sunset_time, moonphase) = get_sun_and_moon_phase(park_data[0], park_data[1], game_stats["date"])
|
||||
|
||||
weather_data = [
|
||||
@@ -235,3 +246,5 @@ class Importer:
|
||||
]
|
||||
|
||||
self.database.insert(insert_into_weather, weather_data)
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user