fixing paths in ingest, forgot to commit statements

This commit is contained in:
2026-01-12 14:24:58 -07:00
parent c6aa52772d
commit 123ccb68b6

View File

@@ -15,15 +15,16 @@ def fetchHourlyForecasts():
response.raise_for_status() response.raise_for_status()
current_date = datetime.now() current_date = datetime.now()
if not os.path.exists(f"../data/{current_date.strftime('%Y-%m-%d')}"): if not os.path.exists(f"data/{current_date.strftime('%Y-%m-%d')}"):
os.makedirs(f"../data/{current_date.strftime('%Y-%m-%d')}", exist_ok=True) os.makedirs(f"data/{current_date.strftime('%Y-%m-%d')}", exist_ok=True)
hourly_filename = f"../data/{current_date.strftime('%Y-%m-%d')}/hourly_{current_date.strftime('%H')}.json" hourly_filename = f"data/{current_date.strftime('%Y-%m-%d')}/hourly_{current_date.strftime('%H')}.json"
with open(hourly_filename, 'w') as json_file: with open(hourly_filename, 'w') as json_file:
json.dump(response.json(), json_file, indent=4) json.dump(response.json(), json_file, indent=4)
db = get_db() db = get_db()
cursor = db.cursor()
insert_sql = """ insert_sql = """
INSERT INTO `current_forecasts` INSERT INTO `current_forecasts`
( (
@@ -44,24 +45,25 @@ def fetchHourlyForecasts():
) )
""" """
for period_report in response.json()['properties']['periods']: for forecast in response.json()['properties']['periods']:
# TODO: this should be a transaction with rollback, pretty hacky right now # TODO: this should be a transaction with rollback, pretty hacky right now
db.execute(insert_sql, ( cursor.execute(insert_sql, (
datetime.strptime(period_report['startTime'], "%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d %H:%M:%S"), datetime.strptime(forecast['startTime'], "%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d %H:%M:%S"),
datetime.strptime(period_report['endTime'], "%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d %H:%M:%S"), datetime.strptime(forecast['endTime'], "%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d %H:%M:%S"),
period_report['isDaytime'], forecast['isDaytime'],
period_report['temperature'], forecast['temperature'],
period_report['probabilityOfPrecipitation']['value'], forecast['probabilityOfPrecipitation']['value'],
period_report['relativeHumidity']['value'], forecast['relativeHumidity']['value'],
period_report['windSpeed'], forecast['windSpeed'],
period_report['windDirection'], forecast['windDirection'],
period_report['icon'], forecast['icon'],
period_report['shortForecast'], forecast['shortForecast'],
period_report['detailedForecast'], forecast['detailedForecast'],
datetime.now().strftime("%Y-%m-%d %H:%M:%S") datetime.now().strftime("%Y-%m-%d %H:%M:%S")
)) ))
db.commit()
close_db()
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
return str(e) return str(e)
@@ -77,14 +79,15 @@ def fetchDailyForecasts():
response.raise_for_status() response.raise_for_status()
current_date = datetime.now() current_date = datetime.now()
if not os.path.exists(f"../data/{current_date.strftime('%Y-%m-%d')}"): if not os.path.exists(f"data/{current_date.strftime('%Y-%m-%d')}"):
os.makedirs(f"../data/{current_date.strftime('%Y-%m-%d')}", exist_ok=True) os.makedirs(f"data/{current_date.strftime('%Y-%m-%d')}", exist_ok=True)
daily_filename = f"../data/{current_date.strftime('%Y-%m-%d')}/daily_{current_date.strftime('%H')}.json" daily_filename = f"data/{current_date.strftime('%Y-%m-%d')}/daily_{current_date.strftime('%H')}.json"
with open(daily_filename, 'w') as json_file: with open(daily_filename, 'w') as json_file:
json.dump(response.json(), json_file, indent=4) json.dump(response.json(), json_file, indent=4)
db = get_db() db = get_db()
cursor = db.cursor()
insert_sql = """ insert_sql = """
INSERT INTO `daily_forecasts` INSERT INTO `daily_forecasts`
( (
@@ -99,18 +102,19 @@ def fetchDailyForecasts():
) )
""" """
for period_report in response.json()['properties']['periods']: for forecast in response.json()['properties']['periods']:
if period_report['isDaytime']: if forecast['isDaytime']:
# TODO: this should be a transaction with rollback # TODO: this should be a transaction with rollback
db.execute(insert_sql, ( cursor.execute(insert_sql, (
datetime.strptime(period_report['startTime'], "%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d %H:%M:%S"), datetime.strptime(forecast['startTime'], "%Y-%m-%dT%H:%M:%S%z").strftime("%Y-%m-%d %H:%M:%S"),
period_report['temperature'], forecast['temperature'],
period_report['probabilityOfPrecipitation']['value'], forecast['probabilityOfPrecipitation']['value'],
period_report['icon'], forecast['icon'],
period_report['shortForecast'], forecast['shortForecast'],
datetime.now().strftime("%Y-%m-%d %H:%M:%S"), datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
)) ))
db.commit()
close_db()
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
return str(e) return str(e)