update
This commit is contained in:
@@ -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}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
from sqlalchemy.orm import Session, sessionmaker, declarative_base
|
||||
|
||||
DATABASE_URL = "sqlite:///videos/youtubeapp.db"
|
||||
|
||||
@@ -18,3 +21,6 @@ def getDb():
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
DbSession = Annotated[Session, Depends(getDb)]
|
||||
|
||||
@@ -11,5 +11,6 @@ class Profile(Base):
|
||||
name = Column(String, nullable=False, unique=True)
|
||||
|
||||
@classmethod
|
||||
def getAll(cls, db: Session) -> list["Profile"]:
|
||||
return db.query(cls).all()
|
||||
def getAll(cls, db: Session) -> list[dict]:
|
||||
profiles = db.query(cls).all()
|
||||
return [{"id": p.id, "name": p.name} for p in profiles]
|
||||
|
||||
@@ -72,13 +72,6 @@ class Video(Base):
|
||||
query = query.filter(cls.profiles.any(Profile.id == profileId))
|
||||
return query.order_by(cls.id.desc()).all()
|
||||
|
||||
@classmethod
|
||||
def getDownloaded(cls, db: Session, profileId: int | None = None) -> list["Video"]:
|
||||
query = db.query(cls).filter(cls.filePath.isnot(None))
|
||||
if profileId:
|
||||
query = query.filter(cls.profiles.any(Profile.id == profileId))
|
||||
return query.order_by(cls.id.desc()).all()
|
||||
|
||||
@classmethod
|
||||
def getById(cls, db: Session, videoId: int) -> "Video | None":
|
||||
return db.query(cls).filter(cls.id == videoId).first()
|
||||
|
||||
Reference in New Issue
Block a user