update
This commit is contained in:
0
backend/services/__init__.py
Normal file
0
backend/services/__init__.py
Normal file
BIN
backend/services/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
backend/services/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/services/__pycache__/download_service.cpython-312.pyc
Normal file
BIN
backend/services/__pycache__/download_service.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/services/__pycache__/video_service.cpython-312.pyc
Normal file
BIN
backend/services/__pycache__/video_service.cpython-312.pyc
Normal file
Binary file not shown.
27
backend/services/download_service.py
Normal file
27
backend/services/download_service.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import subprocess
|
||||
|
||||
from database import SessionLocal
|
||||
from services.video_service import get_video, update_file_path
|
||||
|
||||
VIDEOS_DIR = "/videos"
|
||||
|
||||
|
||||
def download_video(video_id: int, youtube_url: str):
|
||||
output_path = f"{VIDEOS_DIR}/{video_id}.mp4"
|
||||
|
||||
subprocess.run(
|
||||
[
|
||||
"yt-dlp",
|
||||
"-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]",
|
||||
"-o", output_path,
|
||||
"--merge-output-format", "mp4",
|
||||
youtube_url,
|
||||
],
|
||||
check=True,
|
||||
)
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
update_file_path(db, video_id, output_path)
|
||||
finally:
|
||||
db.close()
|
||||
31
backend/services/video_service.py
Normal file
31
backend/services/video_service.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models import Video
|
||||
from schemas import VideoCreate
|
||||
|
||||
|
||||
def create_video(db: Session, video_data: VideoCreate) -> Video:
|
||||
video = Video(**video_data.model_dump())
|
||||
db.add(video)
|
||||
db.commit()
|
||||
db.refresh(video)
|
||||
return video
|
||||
|
||||
|
||||
def get_all_videos(db: Session) -> list[Video]:
|
||||
return db.query(Video).order_by(Video.created_at.desc()).all()
|
||||
|
||||
|
||||
def get_downloaded_videos(db: Session) -> list[Video]:
|
||||
return db.query(Video).filter(Video.file_path.isnot(None)).order_by(Video.created_at.desc()).all()
|
||||
|
||||
|
||||
def get_video(db: Session, video_id: int) -> Video | None:
|
||||
return db.query(Video).filter(Video.id == video_id).first()
|
||||
|
||||
|
||||
def update_file_path(db: Session, video_id: int, path: str):
|
||||
video = get_video(db, video_id)
|
||||
if video:
|
||||
video.file_path = path
|
||||
db.commit()
|
||||
Reference in New Issue
Block a user