"""Artist Social Profile logic.""" from oto import response, status from social_analytics.logic import artist_search from social_analytics.models import artist_social_profile from social_analytics.models import metric def get_artist_social_profiles(): """Get all the artist social profiles. Returns: response.Response: containing a list of artist social profile dictionaries. """ return artist_social_profile.get_artist_social_profiles() def get_linked_artists(label_id): """Get social profile ids for a given label id . Args: label_id (int): the label id. Returns: response.Response: containing a list of artist_ids, with the social_profiles and total followers for each artist_id. """ profiles_response = ( artist_social_profile.get_social_profile_ids_by_label_id(label_id)) if not profiles_response or profiles_response.status == status.NOT_FOUND: return profiles_response artists_and_profiles = profiles_response.message['items'] social_profile_ids = _flatten_social_profile_ids(artists_and_profiles) metrics_response = metric.get_latest_metrics_by_social_profile_ids( social_profile_ids) if not metrics_response: return metrics_response metrics = metrics_response.message['items'] # Each item has an artist id and a list of social profiles for artist_profiles in artists_and_profiles: social_profiles = artist_profiles['social_profiles'] # For each social profile, a metric is returned for profile in social_profiles: profile_id = profile['social_profile_id'] metric_value = list( filter(lambda x: x['social_profile_id'] == profile_id, metrics)) if not metric_value: continue profile['metric'] = metric_value[0]['metric'] profile['metric_value'] = metric_value[0]['metric_value'] profile['platform'] = metric_value[0]['platform'] return response.Response({'items': artists_and_profiles}) def _flatten_social_profile_ids(artist_and_social_profiles): """Flatten social profile ids. Args: artist_and_social_profiles (list): A list containing artist_ids and for each artist id a list with its related social_profiles. Returns: list: a list with all the social_profile_ids """ social_profile_ids = [] for artist_profiles in artist_and_social_profiles: social_profiles = artist_profiles['social_profiles'] ids = [profile['social_profile_id'] for profile in social_profiles] social_profile_ids.extend(ids) return social_profile_ids def search_artists(params, headers): """Get the artist social profiles under a label id and artist id. Args: params (dict): A dict containing the following params: { 'context', 'token', 'term' } headers (dict) : The Grass headers to be passed to ows-search Returns: response.Response: Containing a list with all the artist social profiles. """ artists_response = artist_search.search_artist(params, headers) if not artists_response: return artists_response artists = artists_response.message['items'] artist_ids = [int(artist['artist_id']) for artist in artists] if not artist_ids: return response.create_not_found_response() linked_ids_response = ( artist_social_profile.get_linked_artist_social_profile_ids(artist_ids)) if not linked_ids_response: return linked_ids_response linked_ids = linked_ids_response.message['items'] results = [] for artist in artists: artist_id = int(artist['artist_id']) item = { 'artist_id': artist_id, 'artist_name': artist['artist_name'], 'is_linked': artist_id in linked_ids } results.append(item) return response.Response({'items': results}) def create_artist_social_profile(data): """Create an artist social profile. Args: data (dict): the data from which to create the artist social profile. Returns: response.Response: containing the created artist social profile dict. """ return artist_social_profile.create_artist_social_profile(data) def update_artist_social_profile(data): """Update an artist social profile. Args: data (dict): the data from which to create the artist social profile. Returns: response.Response: containing the updated artist social profile dict. """ return artist_social_profile.update_artist_social_profile(data) def delete_artist_social_profile(social_profile_id, artist_id): """Delete an artist social_profile. Args: social_profile_id (int): The social_profile_id. artist_id (int): The artist_id. Returns: response.Response: containing the deleted artist social profile dict, not found if no profile was deleted or Bad Params. """ return artist_social_profile.delete_artist_social_profile( social_profile_id, artist_id)