from apollo_utils.service.utils.market import fix_country_code_worldwide from typing import List from src.legacy.spotify.constants import SPOTIFY_PLAYLIST_IMAGE_URL_MASK def parse_playlists_data(data: List[dict], with_positions: bool = True) -> List[dict]: """Parse spotify playlists data. Args: data: Original DB response data. with_positions: Including positions. Returns: Parsed data. """ if not data: return [] owner_fields = ("account_name", "account_id", "category_id", "category_name") playlist_fields = ("is_personalized", "name", "link", "country_code") result = [] for index, item in enumerate(data): playlist_id = item["playlist_id"] result_item = { "playlist_id": playlist_id, "owner": { "country_code": fix_country_code_worldwide(item["owner_country_code"]), **{field: item[f"owner_{field}"] for field in owner_fields}, }, "image_url": SPOTIFY_PLAYLIST_IMAGE_URL_MASK.format(playlist_id=playlist_id), **{field: item[field] for field in playlist_fields}, } if with_positions: value = item["value"] change = item["change"] previous_value = value - change result_item.update( { "position": index + 1, "value": value, "change": change, "previous_value": previous_value, "trend": ( round(change / abs(previous_value) * 100) if value is not None and previous_value else None ), } ) result.append(result_item) return result