"""DDEX Project extended metadata generation.""" from typing import Dict import jmespath from .e import E def generate_project(source_data: Dict): """Generate a Project extended metadata node.""" return E( 'Project', E('Id', jmespath.search('project.projectId', source_data)), E('Name', jmespath.search('project.projectName', source_data)), generate_artist(source_data), E('Description', jmespath.search('project.description', source_data)), generate_marketing_highlights(source_data), ) def generate_artist(source_data: Dict): """Generate Artist node.""" spotify_id = jmespath.search('project.labelParticipant.spotifyId', source_data) apple_id = jmespath.search('project.labelParticipant.appleMusicId', source_data) spotify_element = None apple_element = None if spotify_id: spotify_element = E( 'ArtistId', {'Namespace': 'Spotify'}, 'spotify:artist:' + spotify_id ) if apple_id: apple_element = E('ArtistId', {'Namespace': 'iTunes'}, apple_id) return E( 'Artist', E('Name', source_data['project']['labelParticipant']['name']), spotify_element, apple_element, ) def generate_marketing_highlights(source_data: Dict): """Generate MarketingHighlights node.""" highlights_nodes = [] highlights = jmespath.search('project.marketingHighlights', source_data) country_specific_nodes = {'countries': [], 'highlight': ''} for highlight in highlights: # If we do not have a marketingHighlight, do not process this. if not highlight.get('marketingHighlight'): continue if highlight.get('store'): # store based territory highlights_nodes.append( E( 'MarketingHighlight', E('Store', E('Id', highlight.get('store').get('storeName'))), E('Highlight', highlight.get('marketingHighlight')), ) ) elif highlight.get('territory'): # global territory country_specific_nodes['countries'].append( highlight.get('territory').get('territoryCodeA2') ) country_specific_nodes['highlight'] = highlight.get('marketingHighlight') # noqa else: # worldwide territory worldwide_node = E( 'MarketingHighlight', E('TerritoryCode', 'Worldwide'), E('Highlight', highlight.get('marketingHighlight')), ) highlights_nodes.append(worldwide_node) if country_specific_nodes.get('countries'): highlights_nodes.append( E( 'MarketingHighlight', *[ E('TerritoryCode', country) for country in country_specific_nodes['countries'] ], # noqa E('Highlight', country_specific_nodes['highlight']), ) ) if highlights_nodes: return E('MarketingHighlights', *highlights_nodes) return None