myYouTube/backend/app/api/sync.py
vrubelroman 0ed20bb838 Implement Phases 1-5: skeleton, OAuth, categories, video sync/feed, playback
- FastAPI + PostgreSQL + Alembic + React/Vite skeleton, Docker Compose, healthcheck
- Google OAuth (single allowed account), encrypted refresh token storage
- Subscriptions sync with pagination, uploads playlist batch fetch
- Categories CRUD, many-to-many channel assignment, category filtering
- Video sync (playlistItems + videos.list batching), cached feed with cursor
  pagination, background scheduler (APScheduler)
- Video detail page with YouTube embed player
- SPA fallback routing, optimistic UI updates, client-side query caching

40 backend tests covering OAuth allow-list, sync idempotency, cascade deletes,
cursor pagination, and category filtering.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-16 18:44:30 +00:00

50 lines
1.9 KiB
Python

import logging
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.core.auth_dependency import require_session
from app.db import get_db
from app.services import sync
from app.services.google_oauth import OAuthNotConnected
from app.services.youtube_client import YouTubeAPIError, YouTubeQuotaExceeded
logger = logging.getLogger(__name__)
router = APIRouter(dependencies=[Depends(require_session)])
@router.post("/sync/subscriptions")
def sync_subscriptions(db: Session = Depends(get_db)) -> dict:
try:
return sync.sync_subscriptions(db)
except sync.SyncInProgress:
raise HTTPException(status_code=409, detail="Subscriptions sync already in progress")
except OAuthNotConnected:
raise HTTPException(status_code=400, detail="Google account is not connected")
except YouTubeQuotaExceeded:
raise HTTPException(status_code=503, detail="YouTube API quota exhausted")
except YouTubeAPIError as exc:
raise HTTPException(status_code=502, detail=f"YouTube API error: {exc}")
@router.post("/sync/videos")
def sync_videos(db: Session = Depends(get_db)) -> dict:
try:
return sync.sync_videos(db)
except sync.SyncInProgress:
raise HTTPException(status_code=409, detail="Videos sync already in progress")
except OAuthNotConnected:
raise HTTPException(status_code=400, detail="Google account is not connected")
except YouTubeQuotaExceeded:
raise HTTPException(status_code=503, detail="YouTube API quota exhausted")
except YouTubeAPIError as exc:
raise HTTPException(status_code=502, detail=f"YouTube API error: {exc}")
@router.get("/sync/status")
def sync_status(db: Session = Depends(get_db)) -> dict:
return {
"subscriptions": sync.get_subscriptions_sync_status(db),
"videos": sync.get_videos_sync_status(db),
}