8a3e7e2b53
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
32 lines
1011 B
Python
32 lines
1011 B
Python
import sqlite3
|
|
from datetime import datetime, timedelta
|
|
from pathlib import Path
|
|
from app.shared.config import Settings
|
|
|
|
|
|
def purge_old_clips(today: str, settings: Settings, db: sqlite3.Connection) -> None:
|
|
cutoff = datetime.strptime(today, "%Y-%m-%d") - timedelta(days=settings.clip_retention_days)
|
|
clips_root = Path(settings.clips_dir)
|
|
if not clips_root.exists():
|
|
return
|
|
for date_dir in clips_root.iterdir():
|
|
if not date_dir.is_dir():
|
|
continue
|
|
try:
|
|
dir_date = datetime.strptime(date_dir.name, "%Y-%m-%d")
|
|
except ValueError:
|
|
continue
|
|
if dir_date < cutoff:
|
|
for wav in date_dir.glob("*.wav"):
|
|
wav.unlink()
|
|
try:
|
|
date_dir.rmdir()
|
|
except OSError:
|
|
pass
|
|
cutoff_str = cutoff.strftime("%Y-%m-%d")
|
|
db.execute(
|
|
"UPDATE utterances SET audio_clip_path=NULL WHERE date(start_time) < ?",
|
|
(cutoff_str,),
|
|
)
|
|
db.commit()
|