Skip to content

Commit 8c5617c

Browse files
committed
code comments added for instagram graph api python code
1 parent 82cbed3 commit 8c5617c

7 files changed

Lines changed: 159 additions & 138 deletions

File tree

instagram_graph_api/python/business_discovery.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@ def getAccountInfo( params ) :
1111
1212
"""
1313

14-
endpointParams = dict()
15-
endpointParams['fields'] = 'business_discovery.username(' + params['ig_username'] + '){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count}'
16-
endpointParams['access_token'] = params['access_token']
17-
18-
url = params['endpoint_base'] + params['instagram_account_id']
19-
20-
return makeApiCall( url, endpointParams, params['debug'] )
21-
22-
params = getCreds()
23-
params['debug'] = 'no'
24-
response = getAccountInfo( params )
25-
26-
print "\n---- ACCOUNT INFO -----\n"
27-
print "username:"
28-
print response['json_data']['business_discovery']['username']
29-
print "\nwebsite:"
30-
print response['json_data']['business_discovery']['website']
31-
print "\nnumber of posts:"
32-
print response['json_data']['business_discovery']['media_count']
33-
print "\nfollowers:"
34-
print response['json_data']['business_discovery']['followers_count']
35-
print "\nfollowing:"
36-
print response['json_data']['business_discovery']['follows_count']
37-
print "\nprofile picture url:"
38-
print response['json_data']['business_discovery']['profile_picture_url']
39-
print "\nbiography:"
40-
print response['json_data']['business_discovery']['biography']
14+
endpointParams = dict() # parameter to send to the endpoint
15+
endpointParams['fields'] = 'business_discovery.username(' + params['ig_username'] + '){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count}' # string of fields to get back with the request for the account
16+
endpointParams['access_token'] = params['access_token'] # access token
17+
18+
url = params['endpoint_base'] + params['instagram_account_id'] # endpoint url
19+
20+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
21+
22+
params = getCreds() # get creds
23+
params['debug'] = 'no' # set debug
24+
response = getAccountInfo( params ) # hit the api for some data!
25+
26+
print "\n---- ACCOUNT INFO -----\n" # display latest post info
27+
print "username:" # label
28+
print response['json_data']['business_discovery']['username'] # display username
29+
print "\nwebsite:" # label
30+
print response['json_data']['business_discovery']['website'] # display users website
31+
print "\nnumber of posts:" # label
32+
print response['json_data']['business_discovery']['media_count'] # display number of posts user has made
33+
print "\nfollowers:" # label
34+
print response['json_data']['business_discovery']['followers_count'] # display number of followers the user has
35+
print "\nfollowing:" # label
36+
print response['json_data']['business_discovery']['follows_count'] # display number of people the user follows
37+
print "\nprofile picture url:" # label
38+
print response['json_data']['business_discovery']['profile_picture_url'] # display profile picutre url
39+
print "\nbiography:" # label
40+
print response['json_data']['business_discovery']['biography'] # display users about section

instagram_graph_api/python/debug_access_token.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ def debugAccessToken( params ) :
1212
1313
"""
1414

15-
endpointParams = dict()
16-
endpointParams['input_token'] = params['access_token']
17-
endpointParams['access_token'] = params['access_token']
15+
endpointParams = dict() # parameter to send to the endpoint
16+
endpointParams['input_token'] = params['access_token'] # input token is the access token
17+
endpointParams['access_token'] = params['access_token'] # access token to get debug info on
1818

19-
url = params['graph_domain'] + '/debug_token'
19+
url = params['graph_domain'] + '/debug_token' # endpoint url
2020

21-
return makeApiCall( url, endpointParams, params['debug'] )
21+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
2222

23-
params = getCreds()
24-
params['debug'] = 'yes'
25-
response = debugAccessToken( params )
23+
params = getCreds() # get creds
24+
params['debug'] = 'yes' # set debug
25+
response = debugAccessToken( params ) # hit the api for some data!
2626

27-
print "\nData Access Expires at: "
28-
print datetime.datetime.fromtimestamp( response['json_data']['data']['data_access_expires_at'] )
27+
print "\nData Access Expires at: " # label
28+
print datetime.datetime.fromtimestamp( response['json_data']['data']['data_access_expires_at'] ) # display out when the token expires
2929

30-
print "\nToken Expires at: "
31-
print datetime.datetime.fromtimestamp( response['json_data']['data']['expires_at'] )
30+
print "\nToken Expires at: " # label
31+
print datetime.datetime.fromtimestamp( response['json_data']['data']['expires_at'] ) # display out when the token expires

instagram_graph_api/python/defines.py

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,60 @@
22
import json
33

44
def getCreds() :
5-
creds = dict()
6-
creds['access_token'] = 'ACCESS-TOKEN'
7-
creds['client_id'] = 'FB-APP-CLIENT-ID'
8-
creds['client_secret'] = 'FB-APP-CLIENT-SECRET'
9-
creds['graph_domain'] = 'https://graph.facebook.com/'
10-
creds['graph_version'] = 'v6.0'
11-
creds['endpoint_base'] = creds['graph_domain'] + creds['graph_version'] + '/'
12-
creds['debug'] = 'no'
13-
creds['page_id'] = 'FB-PAGE-ID'
14-
creds['instagram_account_id'] = 'INSTAGRAM-BUSINESS-ACCOUNT-ID'
15-
creds['ig_username'] = 'IG-USERNAME'
5+
""" Get creds required for use in the applications
6+
7+
Returns:
8+
dictonary: credentials needed globally
9+
10+
"""
11+
12+
creds = dict() # dictionary to hold everything
13+
creds['access_token'] = 'ACCESS-TOKEN' # access token for use with all api calls
14+
creds['client_id'] = 'FB-APP-CLIENT-ID' # client id from facebook app IG Graph API Test
15+
creds['client_secret'] = 'FB-APP-CLIENT-SECRET' # client secret from facebook app
16+
creds['graph_domain'] = 'https://graph.facebook.com/' # base domain for api calls
17+
creds['graph_version'] = 'v6.0' # version of the api we are hitting
18+
creds['endpoint_base'] = creds['graph_domain'] + creds['graph_version'] + '/' # base endpoint with domain and version
19+
creds['debug'] = 'no' # debug mode for api call
20+
creds['page_id'] = 'FB-PAGE-ID' # users page id
21+
creds['instagram_account_id'] = 'INSTAGRAM-BUSINESS-ACCOUNT-ID' # users instagram account id
22+
creds['ig_username'] = 'IG-USERNAME' # ig username
1623

1724
return creds
1825

1926
def makeApiCall( url, endpointParams, debug = 'no' ) :
20-
data = requests.get( url, endpointParams )
27+
""" Request data from endpoint with params
28+
29+
Args:
30+
url: string of the url endpoint to make request from
31+
endpointParams: dictionary keyed by the names of the url parameters
32+
33+
34+
Returns:
35+
object: data from the endpoint
2136
22-
response = dict()
23-
response['url'] = url
24-
response['endpoint_params'] = endpointParams
25-
response['endpoint_params_pretty'] = json.dumps( endpointParams, indent = 4 )
26-
response['json_data'] = json.loads( data.content )
27-
response['json_data_pretty'] = json.dumps( response['json_data'], indent = 4 )
37+
"""
2838

29-
if ( 'yes' == debug ) :
30-
displayApiCallData( response )
39+
data = requests.get( url, endpointParams ) # make get request
3140

32-
return response
41+
response = dict() # hold response info
42+
response['url'] = url # url we are hitting
43+
response['endpoint_params'] = endpointParams #parameters for the endpoint
44+
response['endpoint_params_pretty'] = json.dumps( endpointParams, indent = 4 ) # pretty print for cli
45+
response['json_data'] = json.loads( data.content ) # response data from the api
46+
response['json_data_pretty'] = json.dumps( response['json_data'], indent = 4 ) # pretty print for cli
47+
48+
if ( 'yes' == debug ) : # display out response info
49+
displayApiCallData( response ) # display response
50+
51+
return response # get and return content
3352

3453
def displayApiCallData( response ) :
35-
print "\nURL: "
36-
print response['url']
37-
print "\nEndpoint Params: "
38-
print response['endpoint_params_pretty']
39-
print "\nResponse: "
40-
print response['json_data_pretty']
54+
""" Print out to cli response from api call """
55+
56+
print "\nURL: " # title
57+
print response['url'] # display url hit
58+
print "\nEndpoint Params: " # title
59+
print response['endpoint_params_pretty'] # display params passed to the endpoint
60+
print "\nResponse: " # title
61+
print response['json_data_pretty'] # make look pretty for cli

instagram_graph_api/python/get_instagram_account.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ def getInstagramAccount( params ) :
1111
1212
"""
1313

14-
endpointParams = dict()
15-
endpointParams['access_token'] = params['access_token']
16-
endpointParams['fields'] = 'instagram_business_account'
14+
endpointParams = dict() # parameter to send to the endpoint
15+
endpointParams['access_token'] = params['access_token'] # tell facebook we want to exchange token
16+
endpointParams['fields'] = 'instagram_business_account' # access token
1717

18-
url = params['endpoint_base'] + params['page_id']
18+
url = params['endpoint_base'] + params['page_id'] # endpoint url
1919

20-
return makeApiCall( url, endpointParams, params['debug'] )
20+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
2121

22-
params = getCreds()
23-
params['debug'] = 'no'
24-
response = getInstagramAccount( params )
22+
params = getCreds() # get creds
23+
params['debug'] = 'no' # set debug
24+
response = getInstagramAccount( params ) # get debug info
2525

2626
print "\n---- INSTAGRAM ACCOUNT INFO ----\n"
27-
print "Page Id:"
28-
print response['json_data']['id']
29-
print "\nInstagram Business Account Id:"
30-
print response['json_data']['instagram_business_account']['id']
27+
print "Page Id:" # label
28+
print response['json_data']['id'] # display the page id
29+
print "\nInstagram Business Account Id:" # label
30+
print response['json_data']['instagram_business_account']['id'] #display the instagram account id

instagram_graph_api/python/get_long_lived_access_token.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ def getLongLivedAccessToken( params ) :
1111
1212
"""
1313

14-
endpointParams = dict()
15-
endpointParams['grant_type'] = 'fb_exchange_token'
16-
endpointParams['client_id'] = params['client_id']
17-
endpointParams['client_secret'] = params['client_secret']
18-
endpointParams['fb_exchange_token'] = params['access_token']
14+
endpointParams = dict() # parameter to send to the endpoint
15+
endpointParams['grant_type'] = 'fb_exchange_token' # tell facebook we want to exchange token
16+
endpointParams['client_id'] = params['client_id'] # client id from facebook app
17+
endpointParams['client_secret'] = params['client_secret'] # client secret from facebook app
18+
endpointParams['fb_exchange_token'] = params['access_token'] # access token to get exchange for a long lived token
1919

20-
url = params['endpoint_base'] + 'oauth/access_token'
20+
url = params['endpoint_base'] + 'oauth/access_token' # endpoint url
2121

22-
return makeApiCall( url, endpointParams, params['debug'] )
22+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
2323

24-
params = getCreds()
25-
params['debug'] = 'yes'
26-
response = getLongLivedAccessToken( params )
24+
params = getCreds() # get creds
25+
params['debug'] = 'yes' # set debug
26+
response = getLongLivedAccessToken( params ) # hit the api for some data!
2727

28-
print "\n ---- ACCESS TOKEN INFO ----\n"
29-
print "Access Token:"
30-
print response['json_data']['access_token']
28+
print "\n ---- ACCESS TOKEN INFO ----\n" # section header
29+
print "Access Token:" # label
30+
print response['json_data']['access_token'] # display access token

instagram_graph_api/python/get_user_media.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,46 @@ def getUserMedia( params, pagingUrl = '' ) :
1111
1212
"""
1313

14-
endpointParams = dict()
15-
endpointParams['fields'] = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username'
16-
endpointParams['access_token'] = params['access_token']
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
1717

18-
if ( '' == pagingUrl ) :
19-
url = params['endpoint_base'] + params['instagram_account_id'] + '/media'
20-
else :
21-
url = pagingUrl
18+
if ( '' == pagingUrl ) : # get first page
19+
url = params['endpoint_base'] + params['instagram_account_id'] + '/media' # endpoint url
20+
else : # get specific page
21+
url = pagingUrl # endpoint url
2222

23-
return makeApiCall( url, endpointParams, params['debug'] )
23+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
2424

25-
params = getCreds()
26-
params['debug'] = 'no'
27-
response = getUserMedia( params )
25+
params = getCreds() # get creds
26+
params['debug'] = 'no' # set debug
27+
response = getUserMedia( params ) # get users media from the api
2828

29-
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> PAGE 1 <<<<<<<<<<<<<<<<<<<<\n"
29+
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> PAGE 1 <<<<<<<<<<<<<<<<<<<<\n" # display page 1 of the posts
3030

3131
for post in response['json_data']['data'] :
32-
print "\n\n---------- POST ----------\n"
33-
print "Link to post:"
34-
print post['permalink']
35-
print "\nPost caption:"
36-
print post['caption']
37-
print "\nMedia type:"
38-
print post['media_type']
39-
print "\nPosted at:"
40-
print post['timestamp']
32+
print "\n\n---------- POST ----------\n" # post heading
33+
print "Link to post:" # label
34+
print post['permalink'] # link to post
35+
print "\nPost caption:" # label
36+
print post['caption'] # post caption
37+
print "\nMedia type:" # label
38+
print post['media_type'] # type of media
39+
print "\nPosted at:" # label
40+
print post['timestamp'] # when it was posted
4141

42-
params['debug'] = 'no'
43-
response = getUserMedia( params, response['json_data']['paging']['next'] )
42+
params['debug'] = 'no' # set debug
43+
response = getUserMedia( params, response['json_data']['paging']['next'] ) # get next page of posts from the api
4444

45-
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> PAGE 2 <<<<<<<<<<<<<<<<<<<<\n"
45+
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> PAGE 2 <<<<<<<<<<<<<<<<<<<<\n" # display page 2 of the posts
4646

4747
for post in response['json_data']['data'] :
48-
print "\n\n---------- POST ----------\n"
49-
print "Link to post:"
50-
print post['permalink']
51-
print "\nPost caption:"
52-
print post['caption']
53-
print "\nMedia type:"
54-
print post['media_type']
55-
print "\nPosted at:"
56-
print post['timestamp']
48+
print "\n\n---------- POST ----------\n" # post heading
49+
print "Link to post:" # label
50+
print post['permalink'] # link to post
51+
print "\nPost caption:" # label
52+
print post['caption'] # post caption
53+
print "\nMedia type:" # label
54+
print post['media_type'] # type of media
55+
print "\nPosted at:" # label
56+
print post['timestamp'] # when it was posted

instagram_graph_api/python/get_user_pages.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ def getUserPages( params ) :
1111
1212
"""
1313

14-
endpointParams = dict()
15-
endpointParams['access_token'] = params['access_token']
14+
endpointParams = dict() # parameter to send to the endpoint
15+
endpointParams['access_token'] = params['access_token'] # access token
1616

17-
url = params['endpoint_base'] + 'me/accounts'
17+
url = params['endpoint_base'] + 'me/accounts' # endpoint url
1818

19-
return makeApiCall( url, endpointParams, params['debug'] )
19+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
2020

21-
params = getCreds()
22-
params['debug'] = 'no'
23-
response = getUserPages( params )
21+
params = getCreds() # get creds
22+
params['debug'] = 'no' # set debug
23+
response = getUserPages( params ) # get debug info
2424

25-
print "\n---- FACEBOOK PAGE INFO ----\n"
26-
print "Page Name:"
27-
print response['json_data']['data'][0]['name']
28-
print "\nPage Category:"
29-
print response['json_data']['data'][0]['category']
30-
print "\nPage Id:"
31-
print response['json_data']['data'][0]['id']
25+
print "\n---- FACEBOOK PAGE INFO ----\n" # section heading
26+
print "Page Name:" # label
27+
print response['json_data']['data'][0]['name'] # display name
28+
print "\nPage Category:" # label
29+
print response['json_data']['data'][0]['category'] # display category
30+
print "\nPage Id:" # label
31+
print response['json_data']['data'][0]['id'] # display id

0 commit comments

Comments
 (0)