update
This commit is contained in:
@@ -20,18 +20,6 @@ class VideoResponse(BaseModel):
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
@classmethod
|
||||
def fromModel(cls, video):
|
||||
return cls(
|
||||
id=video.id,
|
||||
title=video.title,
|
||||
youtuber=video.youtuber,
|
||||
thumbnailUrl=video.thumbnailUrl,
|
||||
youtubeUrl=video.youtubeUrl,
|
||||
isDownloaded=video.filePath is not None,
|
||||
profileIds=[p.id for p in video.profiles],
|
||||
)
|
||||
|
||||
|
||||
class CleanupRequest(BaseModel):
|
||||
profileId: int
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
@@ -7,11 +6,11 @@ from fastapi.responses import FileResponse, StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from api.schemas import CleanupRequest, VideoCreate, VideoResponse
|
||||
from database.database import SessionLocal, getDb
|
||||
from download.download_service import downloadVideo
|
||||
from database.database import getDb
|
||||
from download.download_service import downloadAsync
|
||||
from model.video import Video
|
||||
from notify.notify_clients import notifyClients
|
||||
from stream.stream_service import streamVideoLive
|
||||
from stream.stream_service import streamAndSave
|
||||
|
||||
router = APIRouter(prefix="/videos", tags=["videos"])
|
||||
|
||||
@@ -30,26 +29,21 @@ async def create(videoData: VideoCreate, db: Session = Depends(getDb)):
|
||||
|
||||
|
||||
@router.get("", response_model=list[VideoResponse])
|
||||
def getAll(
|
||||
profileId: Optional[int] = Query(None),
|
||||
db: Session = Depends(getDb),
|
||||
):
|
||||
videos = Video.getAll(db, profileId=profileId)
|
||||
return [VideoResponse.fromModel(v) for v in videos]
|
||||
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),
|
||||
):
|
||||
videos = Video.getDownloaded(db, profileId=profileId)
|
||||
return [VideoResponse.fromModel(v) for v in videos]
|
||||
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)):
|
||||
count = Video.deleteNotDownloaded(db, request.profileId, request.excludeIds or None)
|
||||
profileId = request.profileId
|
||||
excludeIds = request.excludeIds
|
||||
|
||||
count = Video.deleteNotDownloaded(db, profileId, excludeIds)
|
||||
return {"deleted": count}
|
||||
|
||||
|
||||
@@ -57,12 +51,11 @@ def cleanup(request: CleanupRequest, db: Session = Depends(getDb)):
|
||||
def download(videoId: int, db: Session = Depends(getDb)):
|
||||
video = Video.getById(db, videoId)
|
||||
if not video:
|
||||
raise HTTPException(status_code=404, detail="Video nicht gefunden")
|
||||
raise HTTPException(404, "Video nicht gefunden")
|
||||
if video.filePath:
|
||||
return {"status": "already_downloaded"}
|
||||
|
||||
thread = threading.Thread(target=downloadVideo, args=(video.id, video.youtubeUrl))
|
||||
thread.start()
|
||||
downloadAsync(videoId, video.youtubeUrl)
|
||||
return {"status": "download_started"}
|
||||
|
||||
|
||||
@@ -70,52 +63,27 @@ def download(videoId: int, db: Session = Depends(getDb)):
|
||||
def stream(videoId: int, db: Session = Depends(getDb)):
|
||||
video = Video.getById(db, videoId)
|
||||
if not video:
|
||||
raise HTTPException(status_code=404, detail="Video nicht gefunden")
|
||||
raise HTTPException(404, "Video nicht gefunden")
|
||||
|
||||
if not video.filePath:
|
||||
def streamAndSave():
|
||||
outputPath = f"/videos/{videoId}.mp4"
|
||||
yield from streamVideoLive(videoId, video.youtubeUrl)
|
||||
if Path(outputPath).exists():
|
||||
sdb = SessionLocal()
|
||||
try:
|
||||
Video.updateFilePath(sdb, videoId, outputPath)
|
||||
finally:
|
||||
sdb.close()
|
||||
|
||||
return StreamingResponse(streamAndSave(), media_type="video/mp4")
|
||||
return StreamingResponse(streamAndSave(videoId, video.youtubeUrl), media_type="video/mp4")
|
||||
|
||||
path = Path(video.filePath)
|
||||
if not path.exists():
|
||||
raise HTTPException(status_code=404, detail="Videodatei nicht gefunden")
|
||||
|
||||
raise HTTPException(404, "Videodatei nicht gefunden")
|
||||
return FileResponse(path, media_type="video/mp4")
|
||||
|
||||
|
||||
@router.get("/{videoId}/file")
|
||||
def getFile(videoId: int, db: Session = Depends(getDb)):
|
||||
video = Video.getById(db, videoId)
|
||||
if not video:
|
||||
raise HTTPException(status_code=404, detail="Video nicht gefunden")
|
||||
if not video.filePath:
|
||||
raise HTTPException(status_code=404, detail="Video noch nicht heruntergeladen")
|
||||
|
||||
path = Path(video.filePath)
|
||||
if not path.exists():
|
||||
Video.updateFilePath(db, videoId, None)
|
||||
raise HTTPException(status_code=404, detail="Video noch nicht heruntergeladen")
|
||||
|
||||
path, video = Video.getValidPath(db, videoId)
|
||||
if not path:
|
||||
raise HTTPException(404, "Video noch nicht heruntergeladen")
|
||||
return FileResponse(path, media_type="video/mp4", filename=f"{video.title}.mp4")
|
||||
|
||||
|
||||
@router.delete("/{videoId}/file")
|
||||
def deleteFile(videoId: int, db: Session = Depends(getDb)):
|
||||
video = Video.getById(db, videoId)
|
||||
if not video:
|
||||
raise HTTPException(status_code=404, detail="Video nicht gefunden")
|
||||
if video.filePath:
|
||||
path = Path(video.filePath)
|
||||
if path.exists():
|
||||
path.unlink()
|
||||
Video.updateFilePath(db, videoId, None)
|
||||
if not Video.deleteServerFile(db, videoId):
|
||||
raise HTTPException(404, "Video nicht gefunden")
|
||||
return {"status": "deleted"}
|
||||
|
||||
Reference in New Issue
Block a user