"""Utils for Track formatting.""" def map_fields_to_tracks_columns(data): """Map Fields to Tracks Columns. Args: data (dict): dict of track fields Returns: dict: the mapped tracks table column names as keys. """ mapped_values = {} if 'track_number' in data: mapped_values['track_id'] = data['track_number'] if 'track_name' in data: mapped_values['track_name'] = data['track_name'] if 'disc' in data: if data['disc']: mapped_values['cd'] = data['disc'] else: mapped_values['cd'] = 1 if 'isrc' in data: mapped_values['isrc'] = None if data['isrc']: mapped_values['isrc'] = data['isrc'].upper() if 'length' in data: mapped_values.update(format_length_data(data['length'])) if data.get('us_publishing_obligation'): mapped_values['us_publishing_obligation'] = data[ 'us_publishing_obligation'] if data.get('third_party_publisher'): mapped_values['third_party_publisher'] = data['third_party_publisher'] return mapped_values def format_length_data(length_data=None): """Format length data if provided or return default length. Args: length_data (str): "hh:mm:ss formatted time string" Returns: dict: containing time converted to minutes and seconds """ length = { 'length_minute': None, 'length_seconds': None } if not length_data: return length [h, m, s] = length_data.split(':') length['length_minute'] = int(h) * 60 + int(m) length['length_seconds'] = int(s) return length def fake_pagination(tracks): """Add filler pagination info. Args: tracks (list): list of tracks in response Returns: dict: list of tracks decorated with pagination info """ return { 'items': tracks, 'pagination': { 'type': 'standard', 'offset': 0, 'limit': len(tracks), 'total_records': len(tracks)}} def map_track(track_item): """Add formatted artists and track length to final track item. Args: track_item (tuple): track from database Returns: dict: with track data, performers and track length """ (track_id, track_number, track_name, side, isrc, disc, minutes, seconds, us_publishing_obligation, third_party_publisher, writer_name, performer, publishing_obligation) = track_item return { 'track_id': track_id, 'track_number': track_number, 'track_name': track_name, 'isrc': isrc, 'side': side, 'disc': disc, 'song_writers': writer_name.split(',') if writer_name else [], 'length': _map_track_length(minutes, seconds), 'performer': [performer] if performer else [], 'us_publishing_obligation': us_publishing_obligation, 'third_party_publisher': third_party_publisher, 'publisher_names': publishing_obligation.split(',') if publishing_obligation else [] } def map_tracks_response_to_fields_for_copy(tracks_response): """Convert tracks get response list to fields for creation.""" return [{ 'track_number': track['track_number'], 'track_name': track['track_name'], 'isrc': track['isrc'], 'side': track['side'], 'disc': track['disc'], 'length': track['length']['formatted'] if track['length'] else None, 'performer': track['performer'], 'us_publishing_obligation': track.get('us_publishing_obligation'), 'third_party_publisher': track.get('third_party_publisher'), 'publisher_names': track.get('publisher_names'), 'song_writers': track.get('song_writers') } for track in tracks_response.message.get('items')] def _map_track_length(minutes, seconds): """Map length data into dict with hours, minutes, seconds, hh:mm:ss string. Args: minutes (int): number of minutes of the track seconds (int): number of seconds of the track Returns: dict: with mapped track length data or None """ if minutes is None or seconds is None: return None hours = minutes // 60 minutes %= 60 return { 'minutes': minutes, 'seconds': seconds, 'hours': hours, 'formatted': '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds) } def map_track_publishing_obligation(publishing_obligation_item): """Compose final publishing_obligation item. Args: publishing_obligation_item (tuple): publishing_obligation from database Returns: dict: with publishing_obligation data for track """ (track_id, track_number, track_name, us_publishing_obligation, third_party_publisher, publishing_obligation) = publishing_obligation_item return { 'track_id': track_id, 'track_number': track_number, 'track_name': track_name, 'us_publishing_obligation': us_publishing_obligation, 'third_party_publisher': third_party_publisher, 'publisher_names': publishing_obligation.split(',') if publishing_obligation else [] }