Upload ml/02_seed_events.py with huggingface_hub
Browse files- ml/02_seed_events.py +54 -0
ml/02_seed_events.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Stage 2: Seed the historical_events table from config.
|
| 4 |
+
|
| 5 |
+
Idempotent — uses ON CONFLICT to upsert.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import json
|
| 9 |
+
import logging
|
| 10 |
+
import sys
|
| 11 |
+
from datetime import date
|
| 12 |
+
|
| 13 |
+
from config import HISTORICAL_EVENTS
|
| 14 |
+
from db import get_conn
|
| 15 |
+
|
| 16 |
+
logging.basicConfig(
|
| 17 |
+
level=logging.INFO,
|
| 18 |
+
format="%(asctime)s %(levelname)-8s %(message)s",
|
| 19 |
+
handlers=[logging.StreamHandler(sys.stdout)],
|
| 20 |
+
)
|
| 21 |
+
log = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def seed_events():
|
| 25 |
+
conn = get_conn()
|
| 26 |
+
with conn.cursor() as cur:
|
| 27 |
+
for event in HISTORICAL_EVENTS:
|
| 28 |
+
cur.execute(
|
| 29 |
+
"""INSERT INTO historical_events
|
| 30 |
+
(event_name, start_date, end_date, category, keywords)
|
| 31 |
+
VALUES (%s, %s, %s, %s, %s)
|
| 32 |
+
ON CONFLICT (event_name) DO UPDATE SET
|
| 33 |
+
start_date = EXCLUDED.start_date,
|
| 34 |
+
end_date = EXCLUDED.end_date,
|
| 35 |
+
category = EXCLUDED.category,
|
| 36 |
+
keywords = EXCLUDED.keywords
|
| 37 |
+
""",
|
| 38 |
+
(
|
| 39 |
+
event["name"],
|
| 40 |
+
date.fromisoformat(event["start"]),
|
| 41 |
+
date.fromisoformat(event["end"]) if event.get("end") else None,
|
| 42 |
+
event.get("category"),
|
| 43 |
+
json.dumps(event.get("keywords", [])),
|
| 44 |
+
),
|
| 45 |
+
)
|
| 46 |
+
log.info(f" Seeded: {event['name']}")
|
| 47 |
+
|
| 48 |
+
conn.commit()
|
| 49 |
+
conn.close()
|
| 50 |
+
log.info(f"Seeded {len(HISTORICAL_EVENTS)} historical events.")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
seed_events()
|