"""GraphQL utilities""" from connectors.logging import logger as log from constants import fields def format_label_participant( name, vendor_id, subaccount_id, spotify_id, apple_music_id, allow_nulls=False): """Format a ParticipantCreateInput type for mutation Args: name: (str) The Proper name of the participant vendor_id: (int) The associated label ID subaccount_id: (int) The associated subaccount ID spotify_id: (str) The participant's Spotify ID apple_music_id (str): The participant's Apple ID allow_nulls (bool): If True, allow null values for Spotify and Apple Id's Returns: dict: A dict of appropriately formatted key, value pairs. """ ret_data = { 'data': { fields.PARTICIPANT_NAME: name, }, fields.PARTICIPANT_VENDOR_ID: int(vendor_id), fields.PARTICIPANT_SUBACCOUNT_ID: int(subaccount_id) if subaccount_id else 0, } # Add ID's if they exist if spotify_id: ret_data['data'][fields.PARTICIPANT_SPOTIFY_ID] = spotify_id elif allow_nulls: ret_data['data'][fields.PARTICIPANT_SPOTIFY_ID] = None if apple_music_id: ret_data['data'][fields.PARTICIPANT_APPLE_MUSIC_ID] = \ str(apple_music_id) elif allow_nulls: ret_data['data'][fields.PARTICIPANT_APPLE_MUSIC_ID] = None return ret_data def transform_track_participants_isrc( track_list, participant_list, mapping_dict=None): """Transform a track_list to dict of ISRC's with lists of tuples of participant roles and names. Args: track_list (list): List of dicts of track data participant_list (list): List of tuples of participant data Example output: { 'USAT21803701': { 'vendor_id': 23213, 'subaccount_id': 2342, 'participations: [ { 'name': 'Bazzi', 'ddex_role': 'Recording Engineer', 'role': 'Recording Engineer', 'category': 'Studio Personnel', 'participant_id': 'a19d08be-b07f-4d22-a29b-0797014e3d48' }, { 'name': 'Bazzi', 'ddex_role': 'Programmer', 'role': 'Programmer', 'category': 'Studio Personnel', 'participant_id': '67bf9c19-66a1-449f-8049-9bc726acf365' }, { 'name': 'Kevin White', 'ddex_role': 'Composer', 'role': 'Arranger', 'category': 'Compositional', 'participant_id': '67bf9c19-66a1-449f-8049-9bc726acf365' }, { 'name': 'Mike Woods', 'ddex_role': 'Composer', 'role': 'A & R Coordinator', 'category': 'Label Personnel', 'participant_id': '67bf9c19-66a1-449f-8049-9bc726acf365' }, { 'name': 'Andrew Bazzi', 'ddex_role': 'Artist', 'role': 'String Engineer', 'category': 'Studio Personnel', 'participant_id': '67bf9c19-66a1-449f-8049-9bc726acf365' } ] } }""" if not mapping_dict: raise ValueError( 'Mapping dict is required. Set ROLE_MAPPING in env.') track_dict = {} for track in track_list: isrc = track[fields.ISRC] vendor_id = track[fields.VENDOR_ID] subaccount_id = track[fields.SUBACCOUNT_ID] if not track_dict.get(isrc): track_dict[isrc] = { fields.PARTICIPANT_VENDOR_ID: vendor_id, fields.PARTICIPANT_SUBACCOUNT_ID: subaccount_id, fields.PARTICIPATIONS: [], } # Get participant_id from participant_list for participant in participant_list: if ( track[fields.ARTIST_NAME] == participant[0] and vendor_id == participant[1] and subaccount_id == participant[2] ): participant_id = participant[3] break # <- Guaranteed single match as our iterable is a set # More elegant, but slower, and less readable # participant_id = [ # p[3] for p in participant_list if # p[0] == track[fields.ARTIST_NAME] and # p[1] == vendor_id and # p[2] == subaccount_id # ][0] # <- Single item like above # Map WMG roles to Orchard roles and categories mapped_role = mapping_dict.get(track[fields.ARTIST_ROLE]) if mapped_role: track_dict[isrc][fields.PARTICIPATIONS].append( { fields.PARTICIPANT_NAME: track[fields.ARTIST_NAME], fields.PARTICIPANT_DDEX_ROLE: track[fields.ARTIST_ROLE], fields.PARTICIPANT_ROLE: mapped_role['role'], fields.PARTICIPANT_CATEGORY: mapped_role['category'], fields.PARTICIPANT_ID: participant_id, } ) else: # log.error( # continue raise Exception( f"No Orchard role found for '{track[fields.ARTIST_ROLE]}'") if not track_dict[isrc][fields.PARTICIPATIONS]: log.warning(f"No participants found for ISRC: {isrc}") return track_dict def format_track_label_participations( isrc, vendor_id, subaccount_id, participations): """Format track participations for GraphQL mutation. Args: isrc (str): ISRC vendor_id (int): Vendor ID subaccount_id (int): Subaccount ID participations (list): List of dicts of participations. Example: [ { 'name': 'Ted Franklin', 'ddex_role': 'A&R', 'role': 'A&R Specialist', 'category': 'Label Personnel', 'participant_id': 'a19d08be-b07f-4d22-a29b-0797014e3d48' }, { 'name': 'Bill Johnson', 'ddex_role': 'Drum Mixer', 'role': 'Drum Mixer', 'category': 'Studio Personnel', 'participant_id': '67bf9c19-66a1-449f-8049-9bc726acf365' } ] Returns: dict: A dict of appropriately formatted key, value pairs. Example: { 'isrc': 'USAT21803701', 'vendorId': 23213, 'subaccountId': 2342, 'participations': [ { 'participantId': 'a19d08be-b07f-4d22-a29b-0797014e3d48', 'role': 'A&R Specialist', # 'category': 'Label Personnel', # 'name': 'Ted Franklin', 'sequenceNumber': 1 }, { 'participantId': '67bf9c19-66a1-449f-8049-9bc726acf365', 'role': 'Drum Mixer', # 'category': 'Studio Personnel', # 'name': 'Bill Johnson', 'sequenceNumber': 2 } ] } """ # Create a list of dicts of formatted participations formatted_participations = [] # TODO: participationRoleCategoryName is only available per track, not per # participation. This means that if a track has multiple participants # with different categories, we'll need to make multiple calls to # setLabelSoundRecordingParticipations, one per category. This is # inefficient, but I don't see a way around it. This means we need to pre- # process the data to group by category, then by track, with all # participants in a given category for a given track in a single call. # This is an arbitrary index number that determines some order... view? # I assume this sequence number must be handled if participation updates # are performed during release correction? sequence_num = 1 for participation in participations: formatted_participations.append( { 'labelParticipantId': participation[fields.PARTICIPATION_ID], 'participationRoleName': participation[fields.PARTICIPANT_ROLE], 'sequenceNumber': sequence_num, } ) sequence_num += 1 # Create a dict of formatted track participations formatted_track_participations = { 'isrc': isrc, 'vendorId': int(vendor_id), 'subaccountId': int(subaccount_id), 'participations': formatted_participations } return formatted_track_participations