import re from typing import List, Optional from server.constants import DSP class Market: US = "us" GLOBAL = "global" WORLDWIDE = "worldwide" GL = "_gl" ALL = "all" DEFAULT_PER_DSP = {DSP.APPLE.value: US, DSP.SPOTIFY.value: ""} ALL_GLOBAL = (GLOBAL, WORLDWIDE, GL) @staticmethod def convert_global(value: List[str] or str, replace_with: str): if isinstance(value, str): if value in Market.ALL_GLOBAL: value = replace_with else: for index, item in enumerate(value): if item in Market.ALL_GLOBAL: value[index] = replace_with return value # AP provides the same logic on the frontend side ISRC_MARKET_PREFIX_MAPPING = {"QM": "US", "QZ": "US", "UK": "GB", "BX": "BR", "FX": "FR", "DG": "TC", "TC": ""} def isrc_to_country_code(isrc: Optional[str]) -> str: """Parse country code from isrc prefix.""" code = re.sub(r"[^a-zA-Z0-9]", "", isrc or "")[:2] return ISRC_MARKET_PREFIX_MAPPING.get(code.upper(), code).lower()