"""Genre mapping.""" from typing import Dict from ddex_ingester_common.constants import genre_mapping from ddex_ingester_common.lambda_exceptions import ( InvalidGenreException, InvalidSubgenreException ) def get_orchard_genre_mapping_for_itunes(genre_code): """Get orchard genre mapping for iTunes. Args: genre_code (str): iTunes genre code Returns: dict: Orchard genre_id and subgenre_id """ return genre_mapping.ITUNES_GENRE_MAPPING.get(genre_code) # This version of the helper uses a constant instead of a GraphQL call def get_orchard_genre_mapping( genre: str, subgenre: str, raise_exception: bool = True) -> Dict: """Get orchard genre mapping.""" if not genre and raise_exception: raise InvalidGenreException('Missing genre') if not subgenre and raise_exception: raise InvalidSubgenreException('Missing subgenre') orchard_genres = genre_mapping.ORCHARD_GENRE_MAPPING genre_id = None orchard_subgenres = [] for orchard_genre in orchard_genres: if genre == orchard_genre['name']: genre_id = orchard_genre['id'] orchard_subgenres = orchard_genre['subgenres'] break if not genre_id and raise_exception: raise InvalidGenreException('Invalid genre') subgenre_id = None for orchard_subgenre in orchard_subgenres: if subgenre == orchard_subgenre['name']: subgenre_id = orchard_subgenre['id'] break if not subgenre_id and raise_exception: raise InvalidSubgenreException(f'Invalid subgenre for genre {genre}') return { 'genre_id': genre_id, 'subgenre_id': subgenre_id }