""" FastAPI router for utility endpoints. """ from fastapi import APIRouter from fastapi.responses import JSONResponse from .. import logger from ..connectors import scrapers from ..responses import json_200_data logger = logger.new_logger(__name__) ROUTE: str = "/utils" def create_router(*args, **kwargs): """Create FastAPI router.""" app = APIRouter() @app.get("/artist_search", response_class=JSONResponse) async def artist_search(query: str): """Search artists on Last.fm and return a list of artist names that match the search query (fuzzy search). Args: query: The name of the artist / search term. Returns: A list of artist names that fuzzy match the search query. """ last_fm = scrapers.LastFM() return json_200_data(await last_fm.artist_search(query)) @app.get("/artist_photos", response_class=JSONResponse) async def get_artist_photos(artist_name: str, limit: int = 10, size: int = 300): """Get artist photos. Args: artist_name: The name of the artist. Must be an exact match (which can be previously obtained from the artist_search endpoint). limit: The maximum number of photos to return. Defaults to 10. size: The size of the square in pixels. Defaults to 300. Returns: A data list of URLs to the artist's photos. If no photos are found, an empty list will be returned. """ last_fm = scrapers.LastFM() return json_200_data(await last_fm.get_artist_photos(artist_name, limit, size)) return app