82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_db, get_current_active_user
|
|
from app.models.user import User
|
|
from app.schemas.song import Song
|
|
from app.schemas.artist import Artist
|
|
from app.services.song import get_song
|
|
from app.services.artist import get_artist
|
|
from app.services.recommendation import (
|
|
get_similar_songs,
|
|
get_recommended_songs_for_user,
|
|
get_popular_songs,
|
|
get_artist_recommendations,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/similar-songs/{song_id}", response_model=List[Song])
|
|
def similar_songs(
|
|
song_id: int,
|
|
limit: int = Query(10, ge=1, le=50),
|
|
db: Session = Depends(get_db),
|
|
) -> Any:
|
|
"""
|
|
Get similar songs to the given song ID.
|
|
"""
|
|
# Check if song exists
|
|
song = get_song(db, song_id=song_id)
|
|
if not song:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Song not found",
|
|
)
|
|
|
|
return get_similar_songs(db, song_id=song_id, limit=limit)
|
|
|
|
|
|
@router.get("/for-user", response_model=List[Song])
|
|
def recommendations_for_user(
|
|
limit: int = Query(10, ge=1, le=50),
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get personalized song recommendations for the current user.
|
|
"""
|
|
return get_recommended_songs_for_user(db, user_id=current_user.id, limit=limit)
|
|
|
|
|
|
@router.get("/popular", response_model=List[Song])
|
|
def popular_songs(
|
|
limit: int = Query(10, ge=1, le=50),
|
|
db: Session = Depends(get_db),
|
|
) -> Any:
|
|
"""
|
|
Get popular songs based on how many playlists they appear in.
|
|
"""
|
|
return get_popular_songs(db, limit=limit)
|
|
|
|
|
|
@router.get("/similar-artists/{artist_id}", response_model=List[Artist])
|
|
def similar_artists(
|
|
artist_id: int,
|
|
limit: int = Query(5, ge=1, le=20),
|
|
db: Session = Depends(get_db),
|
|
) -> Any:
|
|
"""
|
|
Get similar artists to the given artist ID.
|
|
"""
|
|
# Check if artist exists
|
|
artist = get_artist(db, artist_id=artist_id)
|
|
if not artist:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Artist not found",
|
|
)
|
|
|
|
return get_artist_recommendations(db, artist_id=artist_id, limit=limit) |