This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import Libraries | |
import requests | |
import json | |
import datetime | |
import pandas as pd | |
# Define Parameters Dictionary | |
params = dict() | |
params['access_token'] = 'XXXXXXXXXXXXXXXXXXX' # not an actual access token | |
params['client_id'] = '111111111111' # not an actual client id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create DataFrame - Audience by City | |
df_audience_city = pd.Series(audience_city, name='Count') | |
df_audience_city.index.name = 'City' | |
df_audience_city = df_audience_city.sort_values(ascending=False).reset_index() | |
df_audience_city | |
# Create DataFrame - Audience by Country | |
df_audience_country = pd.Series(audience_country, name='Count') | |
df_audience_country.index.name = 'Country' | |
df_audience_country = df_audience_country.sort_values(ascending=False).reset_index() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define URL | |
url = params['endpoint_base'] + params['instagram_account_id'] + '/insights' | |
# Define Endpoint Parameters | |
endpointParams = dict() | |
endpointParams['metric'] = 'audience_city,audience_country,audience_gender_age' | |
endpointParams['period'] = 'lifetime' | |
endpointParams['access_token'] = params['access_token'] | |
# Requests Data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Initialize Empty Container | |
engagement_list = [] | |
impressions_list = [] | |
reach_list = [] | |
saved_list = [] | |
# Loop Over Insights to Fill Container | |
for insight in media_insight: | |
engagement_list.append(insight[0]['values'][0]['value']) | |
impressions_list.append(insight[1]['values'][0]['value']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
media_insight = [] | |
# Loop Over 'Media ID' | |
for i in basic_insight['data']: | |
params['latest_media_id'] = i['id'] | |
# Define URL | |
url = params['endpoint_base'] + params['latest_media_id'] + '/insights' | |
# Define Endpoint Parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define URL | |
url = params['endpoint_base'] + params['instagram_account_id'] + '/media' | |
# Define Endpoint Parameters | |
endpointParams = dict() | |
endpointParams['fields'] = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username,like_count,comments_count' | |
endpointParams['access_token'] = params['access_token'] | |
# Requests Data | |
data = requests.get(url, endpointParams ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define URL | |
url = params['endpoint_base'] + 'oauth/access_token' | |
# Define Endpoint Parameters | |
endpointParams = dict() | |
endpointParams['grant_type'] = 'fb_exchange_token' | |
endpointParams['client_id'] = params['client_id'] | |
endpointParams['client_secret'] = params['client_secret'] | |
endpointParams['fb_exchange_token'] = params['access_token'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_complete = pd.concat([df, df_media_insight], axis=1) | |
df_complete.head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df = pd.DataFrame(basic_insight['data']) | |
df.columns = ['id', 'Caption', 'Media_Type', 'Media_URL', 'Permalink', 'Timestamp', 'Username', 'Likes', 'Comments'] | |
df.head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Token Expires: ", datetime.datetime.fromtimestamp(access_token_data['data']['expires_at'])) |
NewerOlder