"""Social Profile logic.""" import time from oto import response from oto import status from social_analytics.constants import collectors from social_analytics.constants import error from social_analytics.models import artist_url from social_analytics.models import social_profile def get_social_profiles(): """Get all the social profiles. Returns: response.Response: containing a list of social profile dictionaries. """ return social_profile.get_social_profiles() def get_social_profiles_by_ids(social_profile_ids): """Get all social profiles with the given social_profile_id. Args: social_profile_ids (list): the ids of the social profiles to search. Returns: response.Response: containing a list of social profile dictionaries with the given social_profile_ids. """ return social_profile.get_social_profiles_by_ids(social_profile_ids) def get_social_profiles_by_artist_id(artist_id): """Get all the social profiles for a given artist_id. Args: artist_id (str): the artist id for which to query social profiles. Returns: response.Response: containing a list of social profile dicts with the given artist_id. """ artist_urls_response = ( artist_url.get_artist_urls_with_artist_id(artist_id)) if not artist_urls_response: return artist_urls_response artist_urls = artist_urls_response.message['items'] profile_ids = [url['social_profile_id'] for url in artist_urls] return social_profile.get_social_profiles_by_ids(profile_ids) def create_or_update_social_profile(data): """Create or update a social profile. This function would be responsible to create or update a social profile. Its logic would support the following scenarios: 1) Create a social profile and an artist social profile. 2) A social profile already exists but we need to create a reference to artist social profile. Args: data (dict): the data from which to create the social profile. Returns: response.Response: containing the created social profile dict. """ required_fields = ['platform_id', 'platform'] if not data or not all(field in data for field in required_fields): return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) found_profile_response = ( social_profile.get_social_profile_with_platform_id_and_platform( data['platform_id'], data['platform'])) # If an old artist social profile exists, delete it if 'old_social_profile_id' in data: deleted_profile_response = ( artist_url.clear_social_profile( data['old_social_profile_id'], data['artist_id'])) data.pop('old_social_profile_id', None) if not deleted_profile_response: return deleted_profile_response if not found_profile_response: return _create_social_profile_and_artist_url(data) else: return _create_artist_url(data, found_profile_response) def delete_social_profile(social_profile_id): """Delete a social profile. This function will delete a social profile based on a given social_profile_id. It will also delete the corresponding artist urls. Args: social_profile_id (int): the social profile id. Returns: response.Response: containing the deleted social profile and artist urls. """ if not social_profile_id: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) deleted_profile_response = ( social_profile.delete_social_profile(social_profile_id)) if not deleted_profile_response: return response.create_not_found_response() # Add deleted social profiles to response. items = {'social_profile': deleted_profile_response.message} deleted_artist_urls_response = ( artist_url.clear_social_profile(social_profile_id)) # Add deleted artist urls to response. if deleted_artist_urls_response: items['artist_social_profiles'] = ( deleted_artist_urls_response.message['items']) return response.Response({'items': items}) def _create_social_profile_and_artist_url(data): """Create social profile and artist url. Args: data (dict): the data from which to create the social profile. Returns: response.Response: containing the deleted social profile dict. """ artist_id = data['artist_id'] data.pop('artist_id', None) data['collection_scheduled_time'] = time.strftime('%H:%M:%S') created_profile_response = (social_profile.create_social_profile(data)) if not created_profile_response: return response.create_error_response( status.BAD_REQUEST, error.ERROR_CODE_MYSQL) created_profile = created_profile_response.message new_artist_url = { 'artist_id': artist_id, 'social_profile_id': created_profile['social_profile_id'], 'url': data['platform_id'], 'site_id': get_site_id(data['platform']), 'evaluated_for_collection': True} result = artist_url.create_artist_url(new_artist_url) if not result: return response.create_error_response( status.BAD_REQUEST, error.ERROR_CODE_MYSQL) result.message.update(created_profile) return result def _create_artist_url(data, found_profile_response): """Update social profile. Args: data (dict): The data from which to update the social profile. found_profile_response (response.Response): A response.Response object with the a social profile. Returns: response.Response: Containing the created social profile dict. """ found_profile = found_profile_response.message new_artist_url = { 'artist_id': data['artist_id'], 'social_profile_id': found_profile['social_profile_id'], 'url': data['platform_id'], 'site_id': get_site_id(data['platform']), 'evaluated_for_collection': True} create_artist_url_response = artist_url.create_artist_url(new_artist_url) if not create_artist_url_response: return response.create_error_response( status.BAD_REQUEST, error.ERROR_CODE_MYSQL) create_artist_url_response.message.update(data) return create_artist_url_response def get_site_id(platform): """Retrieve site id based on platform. Args: platform (string): The platform name e.g. facebook Returns: int: containing the site_id. """ supported_platforms = ( [collectors.FACEBOOK, collectors.SPOTIFY, collectors.INSTAGRAM]) site_ids = { collectors.FACEBOOK: 2, collectors.INSTAGRAM: 46, collectors.SPOTIFY: 47} if platform not in supported_platforms: return None return site_ids[platform]