This commit is contained in:
Marek Lenczewski
2026-04-08 08:51:59 +02:00
parent 375a9cd386
commit a0c8ecaf27
12 changed files with 75 additions and 92 deletions

View File

@@ -1,13 +1,38 @@
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from fastapi import APIRouter
from api.schemas import ProfileResponse
from database.database import getDb
from api.schemas import CleanupRequest, VideoCreate, VideoResponse
from database.database import DbSession
from model.profile import Profile
from model.video import Video
from notify.notify_clients import notifyClients
router = APIRouter(prefix="/profiles", tags=["profiles"])
router = APIRouter()
@router.get("", response_model=list[ProfileResponse])
def getAll(db: Session = Depends(getDb)):
@router.get("/profiles")
def getAll(db: DbSession):
return Profile.getAll(db)
@router.post("/profiles/{profileId}/videos", status_code=204)
async def createVideo(profileId: int, videoData: VideoCreate, db: DbSession):
title = videoData.title
youtuber = videoData.youtuber
thumbnailUrl = videoData.thumbnailUrl
youtubeUrl = videoData.youtubeUrl
Video.deleteIfExists(db, youtubeUrl, profileId)
Video.create(db, title, youtuber, thumbnailUrl, youtubeUrl, profileId)
await notifyClients(profileId)
@router.get("/profiles/{profileId}/videos", response_model=list[VideoResponse])
def getVideos(profileId: int, db: DbSession):
return Video.getAll(db, profileId=profileId)
@router.post("/profiles/{profileId}/videos/cleanup")
def cleanupVideos(profileId: int, request: CleanupRequest, db: DbSession):
excludeIds = request.excludeIds
count = Video.deleteNotDownloaded(db, profileId, excludeIds)
return {"deleted": count}

View File

@@ -6,7 +6,6 @@ class VideoCreate(BaseModel):
youtuber: str
thumbnailUrl: str
youtubeUrl: str
profileId: int | None = None
class VideoResponse(BaseModel):
@@ -22,12 +21,4 @@ class VideoResponse(BaseModel):
class CleanupRequest(BaseModel):
profileId: int
excludeIds: list[int] = []
class ProfileResponse(BaseModel):
id: int
name: str
model_config = ConfigDict(from_attributes=True)

View File

@@ -1,54 +1,18 @@
from pathlib import Path
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi import APIRouter, HTTPException
from fastapi.responses import FileResponse, StreamingResponse
from sqlalchemy.orm import Session
from api.schemas import CleanupRequest, VideoCreate, VideoResponse
from database.database import getDb
from database.database import DbSession
from download.download_service import downloadAsync
from model.video import Video
from notify.notify_clients import notifyClients
from stream.stream_service import streamAndSave
router = APIRouter(prefix="/videos", tags=["videos"])
@router.post("", status_code=204)
async def create(videoData: VideoCreate, db: Session = Depends(getDb)):
title = videoData.title
youtuber = videoData.youtuber
thumbnailUrl = videoData.thumbnailUrl
youtubeUrl = videoData.youtubeUrl
profileId = videoData.profileId
Video.deleteIfExists(db, youtubeUrl, profileId)
Video.create(db, title, youtuber, thumbnailUrl, youtubeUrl, profileId)
await notifyClients(profileId)
@router.get("", response_model=list[VideoResponse])
def getAll(profileId: Optional[int] = Query(None), db: Session = Depends(getDb)):
return Video.getAll(db, profileId=profileId)
@router.get("/downloaded", response_model=list[VideoResponse])
def getDownloaded(profileId: Optional[int] = Query(None), db: Session = Depends(getDb)):
return Video.getDownloaded(db, profileId=profileId)
@router.post("/cleanup")
def cleanup(request: CleanupRequest, db: Session = Depends(getDb)):
profileId = request.profileId
excludeIds = request.excludeIds
count = Video.deleteNotDownloaded(db, profileId, excludeIds)
return {"deleted": count}
router = APIRouter(prefix="/videos")
@router.post("/{videoId}/download")
def download(videoId: int, db: Session = Depends(getDb)):
def download(videoId: int, db: DbSession):
video = Video.getById(db, videoId)
if not video:
raise HTTPException(404, "Video nicht gefunden")
@@ -60,7 +24,7 @@ def download(videoId: int, db: Session = Depends(getDb)):
@router.get("/{videoId}/stream")
def stream(videoId: int, db: Session = Depends(getDb)):
def stream(videoId: int, db: DbSession):
video = Video.getById(db, videoId)
if not video:
raise HTTPException(404, "Video nicht gefunden")
@@ -75,7 +39,7 @@ def stream(videoId: int, db: Session = Depends(getDb)):
@router.get("/{videoId}/file")
def getFile(videoId: int, db: Session = Depends(getDb)):
def getFile(videoId: int, db: DbSession):
path, video = Video.getValidPath(db, videoId)
if not path:
raise HTTPException(404, "Video noch nicht heruntergeladen")
@@ -83,7 +47,7 @@ def getFile(videoId: int, db: Session = Depends(getDb)):
@router.delete("/{videoId}/file")
def deleteFile(videoId: int, db: Session = Depends(getDb)):
def deleteFile(videoId: int, db: DbSession):
if not Video.deleteServerFile(db, videoId):
raise HTTPException(404, "Video nicht gefunden")
return {"status": "deleted"}