from typing import Any, Dict, List, Mapping, Union from server.dna.constants import ARTIST, TRACK from server.artist.utils import ImageService as ArtistImageService from server.track.utils import ImageService as TrackImageService from server.dna.helpers.category import CatgoryHelper IMAGE_SERVICE_MAPPER: Mapping = {ARTIST: ArtistImageService, TRACK: TrackImageService} async def make_cover_image(category_id: int, entities: Dict[int, List[Dict[str, Any]]]) -> Union[str, None]: # type: ignore if category_id not in entities: return None for entity in entities[category_id]: entity_id = entity["entity_id"] entity_type = entity["entity_type"] spotify_entity_id = await CatgoryHelper.get_spotify_entity_id(entity_id, entity_type) if not spotify_entity_id: res = await CatgoryHelper.get_entities(entities_ids=[entity_id], entity_type=entity_type) if res: return res[0]["artwork_url"] continue return IMAGE_SERVICE_MAPPER[entity_type].get_img_url(spotify_entity_id)