from aiohttp import web from aiohttp_apispec import docs, querystring_schema from server.client import services from server.constants import BASE_API_PREFIX from server.domains.artists.streams import calc_artist_markets_top from server.scenarios.artists.streams import calc_artist_streams_totals, get_artist_gras_id, get_artist_streams from server.schemas.artists.streams import ArtistsDemographics, ArtistsMarketsTop, ArtistsStreams, ArtistsStreamsTotals from server.utils.response import dump_response_schema router = web.RouteTableDef() @router.get(BASE_API_PREFIX + "/artists/streams/") @docs(tags=["artists", "streams"], summary="Get artist streams") @querystring_schema(ArtistsStreams.Request) @dump_response_schema(ArtistsStreams.Response(many=True), apply=False) async def get_artists_streams(request: web.Request) -> web.Response: params = request["querystring"] gras_artist_id = await get_artist_gras_id(params.pop("spotify_artist_id")) if not gras_artist_id: return [] return await get_artist_streams(gras_artist_id, **params) @router.get(BASE_API_PREFIX + "/artists/streams/totals/") @docs(tags=["artists", "streams"], summary="Get artist streams sum") @querystring_schema(ArtistsStreamsTotals.Request) @dump_response_schema(ArtistsStreamsTotals.Response, apply=False) async def get_artists_streams_totals(request: web.Request) -> web.Response: params = request["querystring"] gras_artist_id = await get_artist_gras_id(params.pop("spotify_artist_id")) if not gras_artist_id: return {} end_date = await services.dsp.get_streams_latest_date() return await calc_artist_streams_totals(gras_artist_id, end_date=end_date, **params) @router.get(BASE_API_PREFIX + "/artists/demographics/") @docs(tags=["artists", "demographics"], summary="Get artist demographics sum for dates range") @querystring_schema(ArtistsDemographics.Request) @dump_response_schema(ArtistsDemographics.Response, apply=False) async def get_artists_demographics(request: web.Request) -> web.Response: params = request["querystring"] gras_artist_id = await get_artist_gras_id(params.pop("spotify_artist_id")) if not gras_artist_id: return {} return await services.dsp.get_streams( artist_id=gras_artist_id, dsp=params["dsp_list"], start_date=params["start_date"], end_date=params["end_date"], country_code=params["country_code_list"], include=params["include_list"], group_by=("date" if params["per_day"] else None), sort_by=params["sort_by"], sort_order=params["sort_order"], ) @router.get(BASE_API_PREFIX + "/artists/markets/top/") @docs(tags=["artists", "markets"], summary="Get artist top markets.") @querystring_schema(ArtistsMarketsTop.Request) @dump_response_schema(ArtistsMarketsTop.Response) async def get(request: web.Request) -> web.Response: params = request["querystring"] gras_artist_id = await get_artist_gras_id(params.pop("spotify_artist_id")) if not gras_artist_id: return {} response = await services.dsp.get_streams( artist_id=gras_artist_id, start_date=params["start_date"], end_date=params["end_date"], dsp=params["dsp_list"] ) return web.json_response( calc_artist_markets_top(response, params["combine"], params["include_worldwide"], params["limit"]) )