"""Collector for facebook metrics.""" import requests from social_analytics import config from social_analytics.constants import collectors def collect_facebook_metrics(profile_id, facebook_access_token): """ Collect facebook metrics for a given profile id. Args: profile_id (str): the id by which to query the facebook API. facebook_access_token (str): the facebook auth token. Returns: dict: facebook API's json response dict. """ url = construct_facebook_url(profile_id, facebook_access_token) result = requests.get(url) return result.json() def construct_facebook_url(profile_id, facebook_access_token): """ Generate a facebook api url based on a profile id. Args: profile_id (str): the id for which to generate a facebook API url. facebook_access_token (str): the facebook auth token. Returns: Response: a facebook API url for the given id. """ url = '{base_url}v2.7/{profile_id}?fields={query_fields}&' \ 'access_token={access_token}'.format( base_url=collectors.FACEBOOK_BASE_URL, profile_id=profile_id, query_fields=collectors.FACEBOOK_QUERY_FIELDS, access_token=facebook_access_token) return url def refresh_token(): """Refresh facebook access token. Returns: (str): The facebook access token. """ # Get the code needed to refresh the long lived token. code_params = { 'access_token': config.FACEBOOK_ACCESS_TOKEN, 'client_secret': config.FACEBOOK_CLIENT_SECRET, 'client_id': config.FACEBOOK_CLIENT_ID, 'redirect_uri': config.FACEBOOK_REDIRECT_URI } code_response = requests.get( collectors.FACEBOOK_AUTHORIZATION_CODE_URL, params=code_params) code = code_response.json()['code'] # Use the code to get access token. token_params = { 'code': code, 'client_id': config.FACEBOOK_CLIENT_ID, 'machine_id': config.FACEBOOK_MACHINE_ID, 'redirect_uri': config.FACEBOOK_REDIRECT_URI } token_response = requests.get( collectors.FACEBOOK_AUTHORIZATION_TOKEN_URL, params=token_params) token = token_response.json()['access_token'] return token def recommend_facebook_profile(platform_name, facebook_access_token): """Recommend facebook profile. Args: platform_name (str): The profile name of the artist e.g. Shakira facebook_access_token (str): The Facebook access token. Returns: Dict or None: A dictionary with the suggested profile or None when there is not any profile for the given profile name. """ if not facebook_access_token: facebook_access_token = refresh_token() url = ( '{base_url}v2.7/search?q={platform_name}&type=page&' 'fields={query_fields}&access_token={access_token}'.format( base_url=collectors.FACEBOOK_BASE_URL, platform_name=platform_name, query_fields=collectors.FACEBOOK_QUERY_FIELDS, access_token=facebook_access_token)) result = requests.get(url) result_json = result.json() if 'data' in result_json and result_json['data']: profiles = result_json['data'] profile_with_most_likes = ( max( profiles, key=lambda item: item[ 'fan_count'] if item['is_verified'] is True else item[ 'fan_count'])) return { 'platform': 'facebook', 'platform_id': profile_with_most_likes['id'], 'platform_name': profile_with_most_likes['name'], 'is_verified': profile_with_most_likes['is_verified'], 'metric_value': profile_with_most_likes['fan_count'] } else: return None def verify_facebook_profile(platform_id): """Verify facebook profile. Args: platform_id (str): The profile id of the artist e.g. 123444453 Returns: Dict or None: A dictionary with the verified profile or None when there is not any profile for the given profile name. """ token = refresh_token() url = construct_facebook_url(platform_id, token) result = requests.get(url) result_json = result.json() if 'error' in result_json: return None return { 'platform': 'facebook', 'platform_id': result_json['id'], 'platform_name': result_json['name'], 'is_verified': result_json['is_verified'], 'metric_value': result_json['fan_count'] }