Skip to content

Commit b8b7473

Browse files
committed
instagram graph api insights with python
1 parent dccf969 commit b8b7473

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from defines import getCreds, makeApiCall
2+
3+
def getUserMedia( params ) :
4+
""" Get users media
5+
6+
API Endpoint:
7+
https://graph.facebook.com/{graph-api-version}/{ig-user-id}/media?fields={fields}
8+
9+
Returns:
10+
object: data from the endpoint
11+
12+
"""
13+
14+
endpointParams = dict() # parameter to send to the endpoint
15+
endpointParams['fields'] = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username' # fields to get back
16+
endpointParams['access_token'] = params['access_token'] # access token
17+
18+
url = params['endpoint_base'] + params['instagram_account_id'] + '/media' # endpoint url
19+
20+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
21+
22+
def getMediaInsights( params ) :
23+
""" Get insights for a specific media id
24+
25+
API Endpoint:
26+
https://graph.facebook.com/{graph-api-version}/{ig-media-id}/insights?metric={metric}
27+
28+
Returns:
29+
object: data from the endpoint
30+
31+
"""
32+
endpointParams = dict() # parameter to send to the endpoint
33+
endpointParams['metric'] = params['metric'] # fields to get back
34+
endpointParams['access_token'] = params['access_token'] # access token
35+
36+
url = params['endpoint_base'] + params['latest_media_id'] + '/insights' # endpoint url
37+
38+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
39+
40+
def getUserInsights( params ) :
41+
""" Get insights for a users account
42+
43+
API Endpoint:
44+
https://graph.facebook.com/{graph-api-version}/{ig-user-id}/insights?metric={metric}&period={period}
45+
46+
Returns:
47+
object: data from the endpoint
48+
49+
"""
50+
51+
endpointParams = dict() # parameter to send to the endpoint
52+
endpointParams['metric'] = 'follower_count,impressions,profile_views,reach' # fields to get back
53+
endpointParams['period'] = 'day' # period
54+
endpointParams['access_token'] = params['access_token'] # access token
55+
56+
url = params['endpoint_base'] + params['instagram_account_id'] + '/insights' # endpoint url
57+
58+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
59+
60+
params = getCreds() # get creds
61+
response = getUserMedia( params ) # get users media from the api
62+
63+
print "\n---- LATEST POST -----\n" # section header
64+
print "\tLink to post:" # link to post
65+
print "\t" + response['json_data']['data'][0]['permalink'] # link to post
66+
print "\n\tPost caption:" # post caption
67+
print "\t" + response['json_data']['data'][0]['caption'] # post caption
68+
print "\n\tMedia Type:" # type of media
69+
print "\t" + response['json_data']['data'][0]['media_type'] # type of media
70+
print "\n\tPosted at:" # when it was posted
71+
print "\t" + response['json_data']['data'][0]['timestamp'] # when it was posted
72+
73+
params['latest_media_id'] = response['json_data']['data'][0]['id'] # store latest post id
74+
75+
if 'VIDEO' == response['json_data']['data'][0]['media_type'] : # media is a video
76+
params['metric'] = 'engagement,impressions,reach,saved,video_views'
77+
else : # media is an image
78+
params['metric'] = 'engagement,impressions,reach,saved'
79+
80+
response = getMediaInsights( params ) # get insights for a specific media id
81+
82+
print "\n---- LATEST POST INSIGHTS -----\n" # section header
83+
84+
for insight in response['json_data']['data'] : # loop over post insights
85+
print "\t" + insight['title'] + " (" + insight['period'] + "): " + str( insight['values'][0]['value'] ) # display info
86+
87+
response = getUserInsights( params ) # get insights for a user
88+
89+
print "\n---- DAILY USER ACCOUNT INSIGHTS -----\n" # section header
90+
91+
for insight in response['json_data']['data'] : # loop over user account insights
92+
print "\t" + insight['title'] + " (" + insight['period'] + "): " + str( insight['values'][0]['value'] ) # display info
93+
94+
for value in insight['values'] : # loop over each value
95+
print "\t\t" + value['end_time'] + ": " + str( value['value'] ) # print out counts for the date

0 commit comments

Comments
 (0)