"""Track logic.""" from oto import response from owsrequest import request as requests from promo_player.constants import error from promo_player.constants import service_name def get_tracks(product_id, promo_player_tracks, correlation_id=None): """Get the tracks for a promo player. Args: product_id (int): the product ID. promo_player_tracks ([dict]): the list of promo player tracks. correlation_id (str): the correlation ID. Returns: response.Response: containing a dict with the list of tracks. """ if not product_id: return response.Response({'items': promo_player_tracks}) product_tracks_response = get_product_tracks(product_id, correlation_id) if not promo_player_tracks or not product_tracks_response: return product_tracks_response product_tracks = product_tracks_response.message['items'] promo_player_track_ids = [ppt['track_id'] for ppt in promo_player_tracks] tracks = [] for product_track in product_tracks: if product_track['tuid'] in promo_player_track_ids: tracks.append(product_track) return response.Response({'items': tracks}) def get_product_tracks(product_id, correlation_id=None): """Get the tracks of a product from ows-product. Args: product_id (int): the product ID. correlation_id (str): the correlation ID. Returns: response.Response: containing a dict with the list of tracks. """ path = '/product/{product_id}/tracks'.format(product_id=product_id) result = requests.get( service_name.OWS_TRACK, path, correlation_id=correlation_id) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=error.ERROR_CODE_OWS_TRACK_REQUEST, message=result.json()) return response.Response(result.json()) def get_by_track_id(track_id): """Get a track by track_id from ows-track. Args: track_id (int): the track ID. Returns: response.Response: containing the track dict. """ path = '/track/{track_id}'.format(track_id=track_id) result = requests.get(service_name.OWS_TRACK, path) if result.status_code != 200: return response.create_error_response( status=result.status_code, code=error.ERROR_CODE_OWS_TRACK_REQUEST, message=result.json()) return response.Response(result.json()) def get_artist_name(track): """Extract the artist name from the track. Args: track (dict): the track Returns: str: the artist name or N/A if not found. """ artists = track.get('artists', []) performer = [] featuring = [] for artist in artists: artist_type = artist.get('type', None) name = artist.get('name', None) if name and artist_type == 'performer': performer.append(name) elif name and artist_type == 'featuring': featuring.append(name) performer = ' & '.join(performer) featuring = ' & '.join(featuring) if performer and featuring: return '{0} ft. {1}'.format(performer, featuring) elif performer: return performer return 'N/A' def map_tracks(tracks, assets): """Map a list of tracks with their durations. Args: tracks ([dict]): the list of tracks. assets ([dict]): the list of assets containing the tracks durations. Returns: [dict]: the mapped list of tracks. """ mapped_tracks = [] for track in tracks: duration = 0 for asset in assets: if 'track_unique_id' not in asset or \ asset['track_unique_id'] != track['tuid']: continue if 'stream' not in asset or not isinstance(asset['stream'], dict): continue if 'duration' in asset['stream']: duration = asset['stream']['duration'] break trackName = track['track_name'] if track['version']: trackName = trackName + ' (' + track['version'] + ')' mapped_tracks.append({ 'tuid': track['tuid'], 'track_number': track['track_number'], 'track_name': trackName, 'track_duration': duration, 'artist_name': get_artist_name(track) }) return mapped_tracks