ingest activities.csv

This commit is contained in:
Davide Scaini
2026-04-11 08:13:27 +02:00
parent cbd5a98cd3
commit 01db4eb9ae
5 changed files with 367 additions and 79 deletions
+48 -7
View File
@@ -530,6 +530,12 @@ async def upload_activity(
store_original: bool = Form(False),
bincio_session: Optional[str] = Cookie(default=None),
) -> JSONResponse:
"""Accept FIT/GPX/TCX files and/or activities.csv, extract, update index, re-merge.
activities.csv (Strava export format) can be included in the batch to:
- Enrich activity files being uploaded in the same batch (matched by filename)
- Retroactively update sidecars for existing activities (matched by strava_id)
"""
from bincio.extract.ingest import ingest_parsed
from bincio.extract.parsers.factory import parse_file
from bincio.extract.writer import make_activity_id
@@ -540,13 +546,36 @@ async def upload_activity(
staging = dd / "_uploads"
staging.mkdir(exist_ok=True)
# Separate CSV files from activity files
csv_files: list[UploadFile] = []
activity_files: list[UploadFile] = []
for f in files:
fname = Path(f.filename or "").name.lower()
if fname.endswith(".csv"):
csv_files.append(f)
else:
activity_files.append(f)
# Build metadata from the first CSV found (activities.csv from Strava export)
metadata = None
if csv_files:
from bincio.extract.strava_csv import StravaMetadata
import tempfile
csv_bytes = await csv_files[0].read()
with tempfile.NamedTemporaryFile(suffix=".csv", delete=False) as tmp:
tmp.write(csv_bytes)
tmp_path = Path(tmp.name)
try:
metadata = StravaMetadata(tmp_path)
finally:
tmp_path.unlink(missing_ok=True)
results = []
any_added = False
for file in files:
for file in activity_files:
name = Path(file.filename or "upload.fit").name
p = Path(name.lower())
suffix = (p.stem.rsplit(".", 1)[-1].join([".", ".gz"]) if "." in p.stem else ".gz") if p.suffix == ".gz" else p.suffix
suffix = _file_suffix(name)
if suffix not in _SUPPORTED_SUFFIXES:
results.append({"name": name, "ok": False, "error": f"Unsupported file type '{suffix}'"})
continue
@@ -561,6 +590,11 @@ async def upload_activity(
kept = False
try:
activity = parse_file(staged)
# Enrich with CSV metadata when available (matched by filename)
if metadata is not None:
metadata.enrich(name, activity)
activity_id = make_activity_id(activity)
if (dd / "activities" / f"{activity_id}.json").exists():
results.append({"name": name, "ok": False, "error": "duplicate"})
@@ -573,18 +607,25 @@ async def upload_activity(
kept = True
results.append({"name": name, "ok": True, "id": activity_id})
any_added = True
except Exception as exc:
except Exception:
results.append({"name": name, "ok": False, "error": "Processing failed"})
finally:
if not kept:
staged.unlink(missing_ok=True)
if any_added:
# Retroactively update sidecars for existing activities matched by strava_id
csv_updates = 0
if metadata is not None:
from bincio.extract.strava_csv import apply_csv_to_data_dir
csv_updates = apply_csv_to_data_dir(dd, metadata)
if any_added or csv_updates:
merge_all(dd)
_trigger_rebuild(user.handle)
if any_added:
_trigger_rebuild(user.handle)
added = [r for r in results if r["ok"]]
return JSONResponse({"ok": True, "added": len(added), "results": results})
return JSONResponse({"ok": True, "added": len(added), "csv_updates": csv_updates, "results": results})
@app.post("/api/upload/strava-zip")