Repository navigation
Expand file tree
/
Copy pathseed.py
More file actions
55 lines (42 loc) · 1.71 KB
/
Copy pathseed.py
File metadata and controls
55 lines (42 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import asyncio
import re
from datetime import datetime, timezone
from pathlib import Path
from sqlalchemy import delete
from app.database import async_session
from app.models.processed_transcript import ProcessedTranscript
from app.models.transcript import Transcript
TRANSCRIPTS_DIR = Path(__file__).parent / "transcripts"
def parse_date_from_filename(filename: str) -> datetime | None:
"""Extract date from filename pattern like _2024_11_15.txt"""
match = re.search(r"(\d{4})_(\d{2})_(\d{2})\.txt$", filename)
if match:
year, month, day = int(match.group(1)), int(match.group(2)), int(match.group(3))
return datetime(year, month, day, tzinfo=timezone.utc)
return None
async def seed():
now = datetime.now(timezone.utc)
async with async_session() as session:
print("Seeding database...")
# Clear existing data (order matters due to foreign keys)
await session.execute(delete(ProcessedTranscript))
await session.execute(delete(Transcript))
await session.flush()
# Load transcript files
transcript_files = sorted(TRANSCRIPTS_DIR.glob("*.txt"))
for filepath in transcript_files:
content = filepath.read_text()
transcript_date = parse_date_from_filename(filepath.name)
session.add(
Transcript(
filename=filepath.name,
content=content,
transcript_date=transcript_date,
created_at=now,
)
)
await session.commit()
print(f" Loaded {len(transcript_files)} transcripts")
print("Database seeded successfully!")
if __name__ == "__main__":
asyncio.run(seed())