from aiohttp import web from aiohttp_apispec import docs, querystring_schema from apollo_utils.core.constants.dsp import DSP from server.constants import BASE_API_PREFIX, cache_keys from server.constants.playlists.delphi.by_api.track_positions_previous_playlists import TrackPositionsPreviousPlaylists from server.constants.playlists.delphi.core.include import StreamsForPeriod from server.scenarios.playlists.by_track.previous.ago.common import get_previous_playlists_dsp_by_track_ago from server.scenarios.playlists.by_track.previous.ap.apple import get_previous_playlists_apple_by_track_ap from server.schemas.playlists.apple.by_track_history import V1PlaylistAppleByTrackHistory, V3PlaylistAppleByTrackHistory from server.utils.pagination import paginate from server.utils.response import dump_response_schema router = web.RouteTableDef() @router.get(BASE_API_PREFIX + "/v1/playlists/apple/by-track/history/") @docs( tags=["playlist", "apple", "history"], summary="(AP) Get apple historical playlists identified by passed ISRC.", ) @querystring_schema(V1PlaylistAppleByTrackHistory.Request) @dump_response_schema(V1PlaylistAppleByTrackHistory.Response) @paginate(cache_keys.V1_PLAYLISTS_APPLE_BY_TRACK_HISTORY, extend_original_dict=True) async def get(request: web.Request) -> web.Response: params = request["querystring"] return await get_previous_playlists_apple_by_track_ap( isrc=params["isrc_list"], country_code=params["country"], meta_country_code=params["meta_country_code"], delphi_include=( [TrackPositionsPreviousPlaylists.INCLUDE.STREAMS_FOR_PERIOD] if StreamsForPeriod.STREAMS_FOR_PERIOD in params["include_list"] else [] ), streams_country_code=[ TrackPositionsPreviousPlaylists.STREAMS_COUNTRY_CODE.WORLDWIDE.value, TrackPositionsPreviousPlaylists.STREAMS_COUNTRY_CODE.LOCAL.value, ], ) @router.get(BASE_API_PREFIX + "/v3/playlists/apple/by-track/history/") @docs( tags=["playlist", "apple", "history"], summary="Get spotify historical playlists by passed isrc. (v3)", ) @querystring_schema(V3PlaylistAppleByTrackHistory.Request) @dump_response_schema(V3PlaylistAppleByTrackHistory.Response) @paginate(cache_keys.V3_PLAYLISTS_APPLE_BY_TRACK_HISTORY, response_kwargs=True) async def get(request: web.Request) -> web.Response: params = request["querystring"] result = await get_previous_playlists_dsp_by_track_ago(dsp=DSP.APPLE, **params) return result["items"], dict(categories=result["categories"])