from typing import Dict, Iterable, List, Set from smelog.factory import BoundLogger from apollo_playlists_messages.config import FULL_APP_NAME from apollo_playlists_messages.utils.settings import SETTINGS_VERSION_TO_PARSER def parse_favorites_accounts( entities: List[dict], logger: BoundLogger, entity_types: Iterable[str] = None, type_to_id_to_accounts: Dict[str, Dict[str, Set[int]]] = None, type_to_dsp_to_accounts: Dict[str, Dict[str, Set[int]]] = None, type_to_country_codes_to_accounts: Dict[str, Dict[str, Set[int]]] = None, account_id_to_devices: Dict[int, Iterable[dict]] = None, ): for entity in entities: entity_type, entity_id = entity["entity_type"], entity["entity_id"] if entity_types and (entity_type not in entity_types): continue for account in entity.get("accounts", []): account_id = account["account_id"] settings_version = account["settings"]["version"] settings_parser = SETTINGS_VERSION_TO_PARSER.get(settings_version) if not settings_parser: logger.error( f"{FULL_APP_NAME}.parse_entities_accounts got unsupported version={settings_version}" f"of settings[{account['settings']['id']}] for account[{account_id}], skipped." ) continue is_interested, entity_type_to_active_dsp, entity_type_to_active_markets = settings_parser( account["settings"]["data"], entity_types=entity_types ) if not is_interested: continue if account_id_to_devices is not None: account_id_to_devices[account_id] = [ {k: d[k] for k in ("id", "expo_token")} for d in account["devices"] ] if type_to_id_to_accounts is not None: type_to_id_to_accounts[entity_type][entity_id].add(account_id) if type_to_dsp_to_accounts is not None: for entity_type, dsp_set in entity_type_to_active_dsp.items(): for dsp in dsp_set: type_to_dsp_to_accounts[entity_type][dsp].add(account_id) if type_to_country_codes_to_accounts is not None: for entity_type, markets in entity_type_to_active_markets.items(): for market in markets: type_to_country_codes_to_accounts[entity_type][market].add(account_id)