2026-09-16 18:56:17 +00:00
|
|
|
import logging
|
|
|
|
|
|
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
|
|
|
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.models.channel import Channel
|
2026-09-16 18:56:17 +00:00
|
|
|
from app.models.download_job import DownloadJob
|
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
|
|
|
from app.models.video import Video
|
2026-09-16 19:47:35 +00:00
|
|
|
from app.services.download_jobs import (
|
2026-09-16 20:01:41 +00:00
|
|
|
DeleteDidNotRemoveFile,
|
2026-09-16 19:47:35 +00:00
|
|
|
DeleteNotAllowed,
|
|
|
|
|
MeTubeRejected,
|
|
|
|
|
delete_local_copy,
|
|
|
|
|
get_latest_job,
|
2026-09-27 23:36:09 +00:00
|
|
|
job_is_stale,
|
|
|
|
|
reconcile_stale_job,
|
2026-09-16 19:47:35 +00:00
|
|
|
request_download,
|
|
|
|
|
)
|
2026-09-16 18:56:17 +00:00
|
|
|
from app.services.metube_client import MeTubeClient
|
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
|
|
|
from app.services.video_presentation import channel_categories_map, serialize_video
|
|
|
|
|
|
2026-09-16 18:56:17 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
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
|
|
|
router = APIRouter(dependencies=[Depends(require_session)])
|
|
|
|
|
|
|
|
|
|
|
2026-09-16 18:56:17 +00:00
|
|
|
def _get_video_or_404(db: Session, youtube_video_id: str) -> Video:
|
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
|
|
|
video = db.query(Video).filter(Video.youtube_video_id == youtube_video_id).one_or_none()
|
|
|
|
|
if video is None:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Video not found")
|
2026-09-16 18:56:17 +00:00
|
|
|
return video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _serialize_job(job: DownloadJob) -> dict:
|
|
|
|
|
return {
|
|
|
|
|
"status": job.status,
|
|
|
|
|
"progress_percent": job.progress_percent,
|
|
|
|
|
"media_url": job.media_url if job.status == "completed" else None,
|
|
|
|
|
"error_message": job.error_message,
|
|
|
|
|
"requested_at": job.requested_at,
|
|
|
|
|
"started_at": job.started_at,
|
|
|
|
|
"completed_at": job.completed_at,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/videos/{youtube_video_id}")
|
|
|
|
|
def get_video(youtube_video_id: str, db: Session = Depends(get_db)) -> dict:
|
|
|
|
|
video = _get_video_or_404(db, youtube_video_id)
|
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
|
|
|
|
|
|
|
|
channel = db.get(Channel, video.channel_id)
|
|
|
|
|
if channel is None:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Channel not found")
|
|
|
|
|
|
|
|
|
|
categories = channel_categories_map(db, [channel.id]).get(channel.id, [])
|
2026-09-16 18:56:17 +00:00
|
|
|
job = get_latest_job(db, video.id)
|
|
|
|
|
return serialize_video(video, channel, categories, job)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/videos/{youtube_video_id}/download")
|
|
|
|
|
def download_video(youtube_video_id: str, db: Session = Depends(get_db)) -> dict:
|
|
|
|
|
video = _get_video_or_404(db, youtube_video_id)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
job = request_download(db, video)
|
|
|
|
|
except MeTubeRejected as exc:
|
|
|
|
|
raise HTTPException(status_code=502, detail=f"MeTube rejected the download: {exc}")
|
|
|
|
|
except Exception:
|
|
|
|
|
logger.exception("Failed to enqueue download for %s", youtube_video_id)
|
|
|
|
|
raise HTTPException(status_code=502, detail="MeTube is unavailable")
|
2026-09-16 19:47:35 +00:00
|
|
|
|
|
|
|
|
return _serialize_job(job)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/videos/{youtube_video_id}/download")
|
|
|
|
|
def delete_download(youtube_video_id: str, db: Session = Depends(get_db)) -> dict:
|
|
|
|
|
video = _get_video_or_404(db, youtube_video_id)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
job = delete_local_copy(db, video)
|
|
|
|
|
except DeleteNotAllowed as exc:
|
|
|
|
|
raise HTTPException(status_code=400, detail=str(exc))
|
2026-09-16 20:01:41 +00:00
|
|
|
except DeleteDidNotRemoveFile:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=409,
|
|
|
|
|
detail=(
|
|
|
|
|
"MeTube убрал запись из своего списка, но файл остался на диске "
|
|
|
|
|
"(на mediaVM выключена настройка DELETE_FILE_ON_TRASHCAN)"
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-09-16 19:47:35 +00:00
|
|
|
except Exception:
|
|
|
|
|
logger.exception("Failed to delete local copy for %s", youtube_video_id)
|
|
|
|
|
raise HTTPException(status_code=502, detail="MeTube is unavailable")
|
2026-09-16 18:56:17 +00:00
|
|
|
|
|
|
|
|
return _serialize_job(job)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/videos/{youtube_video_id}/download-status")
|
|
|
|
|
def download_status(youtube_video_id: str, db: Session = Depends(get_db)) -> dict:
|
|
|
|
|
video = _get_video_or_404(db, youtube_video_id)
|
|
|
|
|
job = get_latest_job(db, video.id)
|
|
|
|
|
if job is None:
|
|
|
|
|
return {"status": "not_downloaded", "progress_percent": None, "media_url": None, "error_message": None}
|
2026-09-27 23:36:09 +00:00
|
|
|
# Lazy self-heal: an active job whose Socket.IO events stopped (e.g. the
|
|
|
|
|
# terminal 'completed' event was missed while we were down) is reconciled
|
|
|
|
|
# against MeTube history on poll -- at most once per stale window.
|
|
|
|
|
if job_is_stale(job):
|
|
|
|
|
try:
|
|
|
|
|
reconcile_stale_job(db, job)
|
|
|
|
|
except Exception:
|
|
|
|
|
logger.warning("Could not self-heal stale download job %s", job.id, exc_info=True)
|
|
|
|
|
db.rollback()
|
2026-09-16 18:56:17 +00:00
|
|
|
return _serialize_job(job)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/videos/{youtube_video_id}/recheck-local")
|
|
|
|
|
def recheck_local(youtube_video_id: str, db: Session = Depends(get_db)) -> dict:
|
|
|
|
|
video = _get_video_or_404(db, youtube_video_id)
|
|
|
|
|
job = get_latest_job(db, video.id)
|
|
|
|
|
if job is None or job.status != "completed":
|
|
|
|
|
raise HTTPException(status_code=400, detail="No completed download to recheck")
|
|
|
|
|
|
|
|
|
|
if job.media_url and MeTubeClient().check_media(job.media_url):
|
|
|
|
|
return _serialize_job(job)
|
|
|
|
|
|
|
|
|
|
job.status = "unknown"
|
|
|
|
|
job.media_url = None
|
|
|
|
|
db.commit()
|
|
|
|
|
return _serialize_job(job)
|