sync strava data from web ui
This commit is contained in:
+33
-6
@@ -20,11 +20,17 @@ console = Console()
|
||||
help="URL of the Astro dev server (for the Back link).")
|
||||
@click.option("--config", "config_path", default=None,
|
||||
help="Path to extract_config.yaml (reads output.dir from it).")
|
||||
@click.option("--strava-client-id", default=None, envvar="STRAVA_CLIENT_ID",
|
||||
help="Strava API client ID (enables Strava sync in the UI). Also reads STRAVA_CLIENT_ID env var.")
|
||||
@click.option("--strava-client-secret", default=None, envvar="STRAVA_CLIENT_SECRET",
|
||||
help="Strava API client secret. Also reads STRAVA_CLIENT_SECRET env var.")
|
||||
def edit(
|
||||
data_dir: Optional[str],
|
||||
port: int,
|
||||
site_url: str,
|
||||
config_path: Optional[str],
|
||||
strava_client_id: Optional[str],
|
||||
strava_client_secret: Optional[str],
|
||||
) -> None:
|
||||
"""Start a local web UI for editing activity sidecar files.
|
||||
|
||||
@@ -46,6 +52,13 @@ def edit(
|
||||
)
|
||||
|
||||
data = _resolve_data_dir(data_dir, config_path)
|
||||
|
||||
# Fall back to extract_config.yaml for Strava credentials
|
||||
if not strava_client_id or not strava_client_secret:
|
||||
cfg_strava = _load_config(config_path).get("import", {}).get("strava", {})
|
||||
strava_client_id = strava_client_id or str(cfg_strava.get("client_id") or "")
|
||||
strava_client_secret = strava_client_secret or str(cfg_strava.get("client_secret") or "")
|
||||
|
||||
console.print(f"Data dir: [cyan]{data}[/cyan]")
|
||||
console.print(f"Edit UI: [cyan]http://localhost:{port}/edit/<activity-id>[/cyan]")
|
||||
console.print(f"Site URL: [cyan]{site_url}[/cyan]")
|
||||
@@ -54,20 +67,34 @@ def edit(
|
||||
import bincio.edit.server as srv
|
||||
srv.data_dir = data
|
||||
srv.site_url = site_url
|
||||
srv.strava_client_id = strava_client_id or ""
|
||||
srv.strava_client_secret = strava_client_secret or ""
|
||||
|
||||
if strava_client_id:
|
||||
console.print(f"Strava sync: [green]enabled[/green] (client {strava_client_id})")
|
||||
else:
|
||||
console.print("Strava sync: [yellow]disabled[/yellow] (pass --strava-client-id to enable)")
|
||||
|
||||
uvicorn.run(srv.app, host="127.0.0.1", port=port, log_level="warning")
|
||||
|
||||
|
||||
def _load_config(config_path: Optional[str]) -> dict:
|
||||
"""Load extract_config.yaml — explicit path first, then cwd auto-discovery."""
|
||||
import yaml
|
||||
for cfg in filter(None, [config_path and Path(config_path), Path("extract_config.yaml")]):
|
||||
if Path(cfg).exists():
|
||||
return yaml.safe_load(Path(cfg).read_text()) or {}
|
||||
return {}
|
||||
|
||||
|
||||
def _resolve_data_dir(explicit: Optional[str], config_path: Optional[str]) -> Path:
|
||||
if explicit:
|
||||
return Path(explicit).expanduser().resolve()
|
||||
|
||||
if config_path and Path(config_path).exists():
|
||||
import yaml
|
||||
raw = yaml.safe_load(Path(config_path).read_text()) or {}
|
||||
out = raw.get("output", {}).get("dir")
|
||||
if out:
|
||||
return Path(out).expanduser().resolve()
|
||||
raw = _load_config(config_path)
|
||||
out = raw.get("output", {}).get("dir")
|
||||
if out:
|
||||
return Path(out).expanduser().resolve()
|
||||
|
||||
default = Path.cwd() / "bincio_data"
|
||||
if default.exists():
|
||||
|
||||
+116
-1
@@ -5,16 +5,19 @@ from __future__ import annotations
|
||||
import json
|
||||
import re
|
||||
import shutil
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import FastAPI, File, HTTPException, UploadFile
|
||||
from fastapi import FastAPI, File, HTTPException, Request, UploadFile
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
|
||||
|
||||
# Populated by the CLI before uvicorn starts
|
||||
data_dir: Path | None = None
|
||||
site_url: str = "http://localhost:4321"
|
||||
strava_client_id: str = ""
|
||||
strava_client_secret: str = ""
|
||||
|
||||
app = FastAPI(title="BincioActivity Edit Server", docs_url=None, redoc_url=None)
|
||||
|
||||
@@ -618,3 +621,115 @@ async def delete_image(activity_id: str, filename: str) -> JSONResponse:
|
||||
if not any(target.parent.iterdir()):
|
||||
shutil.rmtree(target.parent)
|
||||
return JSONResponse({"ok": True})
|
||||
|
||||
|
||||
# ── Strava sync ───────────────────────────────────────────────────────────────
|
||||
|
||||
@app.get("/api/strava/status")
|
||||
async def strava_status() -> JSONResponse:
|
||||
"""Return whether Strava is configured and whether a token is stored."""
|
||||
dd = _get_data_dir()
|
||||
from bincio.extract.strava_api import load_token
|
||||
token = load_token(dd)
|
||||
return JSONResponse({
|
||||
"configured": bool(strava_client_id),
|
||||
"connected": token is not None,
|
||||
"last_sync": token.get("last_sync_at") if token else None,
|
||||
})
|
||||
|
||||
|
||||
@app.get("/api/strava/auth-url")
|
||||
async def strava_auth_url(request: Request) -> JSONResponse:
|
||||
"""Return the Strava OAuth URL the browser should open."""
|
||||
if not strava_client_id:
|
||||
raise HTTPException(400, "Strava client ID not configured. Pass --strava-client-id to bincio edit.")
|
||||
redirect_uri = str(request.url_for("strava_callback"))
|
||||
from bincio.extract.strava_api import auth_url
|
||||
return JSONResponse({"url": auth_url(strava_client_id, redirect_uri)})
|
||||
|
||||
|
||||
@app.get("/api/strava/callback", name="strava_callback")
|
||||
async def strava_callback(code: str = "", error: str = "") -> RedirectResponse:
|
||||
"""Strava OAuth callback — exchange code for token then redirect to the site."""
|
||||
if error or not code:
|
||||
return RedirectResponse(f"{site_url}?strava=error")
|
||||
if not strava_client_id or not strava_client_secret:
|
||||
return RedirectResponse(f"{site_url}?strava=error")
|
||||
dd = _get_data_dir()
|
||||
from bincio.extract.strava_api import StravaError, exchange_code, save_token
|
||||
try:
|
||||
token = exchange_code(strava_client_id, strava_client_secret, code)
|
||||
except StravaError:
|
||||
return RedirectResponse(f"{site_url}?strava=error")
|
||||
# Stamp last_sync_at at connect time so the first sync only fetches new activities
|
||||
token.setdefault("last_sync_at", int(time.time()))
|
||||
save_token(dd, token)
|
||||
return RedirectResponse(f"{site_url}?strava=connected")
|
||||
|
||||
|
||||
@app.post("/api/strava/sync")
|
||||
async def strava_sync() -> JSONResponse:
|
||||
"""Fetch new Strava activities since last sync and add them to the data store."""
|
||||
if not strava_client_id or not strava_client_secret:
|
||||
raise HTTPException(400, "Strava not configured. Pass --strava-client-id and --strava-client-secret to bincio edit.")
|
||||
dd = _get_data_dir()
|
||||
|
||||
from bincio.extract.strava_api import (
|
||||
StravaError, ensure_fresh, fetch_activities, fetch_streams,
|
||||
save_token, strava_to_parsed,
|
||||
)
|
||||
try:
|
||||
token = ensure_fresh(dd, strava_client_id, strava_client_secret)
|
||||
except StravaError as e:
|
||||
raise HTTPException(502, str(e))
|
||||
|
||||
after: int | None = token.get("last_sync_at")
|
||||
try:
|
||||
activities = fetch_activities(token["access_token"], after=after)
|
||||
except StravaError as e:
|
||||
raise HTTPException(502, str(e))
|
||||
|
||||
from bincio.extract.metrics import compute
|
||||
from bincio.extract.writer import build_summary, make_activity_id, write_activity, write_index
|
||||
from bincio.extract.strava_api import strava_meta_to_partial
|
||||
from bincio.render.merge import merge_all
|
||||
|
||||
# Load existing index once
|
||||
index_path = dd / "index.json"
|
||||
if index_path.exists():
|
||||
index_data = json.loads(index_path.read_text(encoding="utf-8"))
|
||||
else:
|
||||
index_data = {"owner": {"handle": "unknown"}, "activities": []}
|
||||
owner = index_data.get("owner", {})
|
||||
summaries: dict[str, dict] = {s["id"]: s for s in index_data.get("activities", [])}
|
||||
|
||||
imported = 0
|
||||
skipped = 0
|
||||
errors: list[str] = []
|
||||
|
||||
for meta in activities:
|
||||
try:
|
||||
# Compute ID from meta alone (no API call) to skip already-known activities
|
||||
activity_id = make_activity_id(strava_meta_to_partial(meta))
|
||||
if (dd / "activities" / f"{activity_id}.json").exists():
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
# Only fetch streams for genuinely new activities
|
||||
streams = fetch_streams(token["access_token"], meta["id"])
|
||||
parsed = strava_to_parsed(meta, streams)
|
||||
metrics = compute(parsed)
|
||||
write_activity(parsed, metrics, dd, privacy="public", rdp_epsilon=0.0001)
|
||||
summaries[activity_id] = build_summary(parsed, metrics, activity_id, "public")
|
||||
imported += 1
|
||||
except Exception as exc:
|
||||
errors.append(f"{meta.get('id')}: {type(exc).__name__}")
|
||||
|
||||
if imported:
|
||||
write_index(list(summaries.values()), dd, owner)
|
||||
merge_all(dd)
|
||||
|
||||
token["last_sync_at"] = int(time.time())
|
||||
save_token(dd, token)
|
||||
|
||||
return JSONResponse({"ok": True, "imported": imported, "skipped": skipped, "errors": errors[:5]})
|
||||
|
||||
Reference in New Issue
Block a user