def AccessYourAccount(): #GETTING USER POSTS AND COMMENTS params = getCredentials() params['debug'] = 'no' response = getUserMedia(params) response_json = dict() i = 0 response_json['id'] = dict() response_json['permalink'] = dict() response_json['caption'] = dict() response_json['media_type'] = dict() response_json['timestamp'] = dict() response_json['comments'] = dict() response_json['like_count'] = dict() #response_json['comments']=dict() for post in response['json_data']['data']: print("\n\n---------- POST ----------\n") print("\nPost_id:") print(post['id']) response_json['id'][i] = post['id'] print("\nLink to post:") print(post['permalink']) response_json['permalink'][i] = post['permalink'] print("\nPost caption:") print(post['caption']) response_json['caption'][i] = post['caption'] print("\nMedia type:") print(post['media_type']) response_json['media_type'][i] = post['media_type'] print("\nPosted at:") print(post['timestamp']) response_json['timestamp'][i] = post['timestamp'] if ('comments' in post): j = 0 response_json['comments'][i] = dict() for com in post['comments']['data']: response_json['comments'][i][j] = com j += 1 else: response_json['comments'][i] = "null" print(post['like_count']) response_json['like_count'][i] = post['like_count'] i = i + 1 os.chdir('/home/devaki/Desktop/insta') req = json.dumps(response_json) df = pd.read_json(req) df.to_csv('Insta_posts.csv', index=False)
def getInstagramAccount(params): """ Get instagram account API Endpoint: https://graph.facebook.com/{graph-api-version}/{page-id}?access_token={your-access-token}&fields=instagram_business_account Returns: object: data from the endpoint """ endpointParams = dict() # parameter to send to the endpoint endpointParams['access_token'] = params[ 'access_token'] # tell facebook we want to exchange token endpointParams['fields'] = 'instagram_business_account' # access token url = params['endpoint_base'] + params['page_id'] # endpoint url return callAPI(url, endpointParams, params['debug']) # make the api call params = getCredentials() # get creds params['debug'] = 'no' # set debug response = getInstagramAccount(params) # get debug info print("\n---- INSTAGRAM ACCOUNT INFO ----\n") print("Page Id:") # label print(response['json_data']['id']) # display the page id print("\nInstagram Business Account Id:") # label print(response['json_data']['instagram_business_account'] ['id']) #display the instagram account id
from func import getCredentials, callAPI def getLongLivedAccessToken( params ) : #API Endpoint: # https://graph.facebook.com/{graph-api-version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={your-access-token} 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'] url = params['endpoint_base'] + 'oauth/access_token' return callAPI( url, endpointParams, params['debug'] ) params = getCredentials() params['debug'] = 'yes' response = getLongLivedAccessToken( params ) print ("\n ---- ACCESS TOKEN INFO ----\n") print ("Access Token:") print (response['json_data']['access_token'])
def AccessInstagramAPI(): params = getCredentials() params['debug'] = 'yes' response = AccessToken(params)
def getPopularMedia(): str_hashtag = input('Enter hashtag:#') params = getCredentials() params['hashtag_name'] = str_hashtag hashtagInfoResponse = getHashtagInfo(params) params['hashtag_id'] = hashtagInfoResponse['json_data']['data'][0]['id'] params['hashtag_id'] = hashtagInfoResponse['json_data']['data'][0]['id'] filename = str_hashtag + '_top' + '.csv' print("\n\n\n\t-------------------HASHTAG INFO -----------------------\n") print("\nHashtag: " + str_hashtag) print("Hashtag ID: " + params['hashtag_id']) print( "\n\n\n\t\t\t ------------------- HASHTAG TOP MEDIA --------------------\n" ) params['type'] = 'top_media' hashtagPopularMediaResponse = getHashtagMedia(params) print(hashtagPopularMediaResponse) hashtag_response = dict() hashtag_response['id'] = dict() hashtag_response['permalink'] = dict() hashtag_response['caption'] = dict() hashtag_response['media_type'] = dict() hashtag_response['like_count'] = dict() hashtag_response['comments_count'] = dict() #hashtag_response['comments']=dict() i = 0 for post in hashtagPopularMediaResponse['json_data']['data']: print("\n\n---------- POST ----------\n") print("Postid:") print(post['id']) hashtag_response['id'][i] = post['id'] print("Link to post:") print(post['permalink']) hashtag_response['permalink'][i] = post['permalink'] print("\nPost caption:") print(post['caption']) hashtag_response['caption'][i] = post['caption'] print("\nMedia type:") print(post['media_type']) hashtag_response['media_type'][i] = post['media_type'] print("\nlike_count:") print(post['like_count']) hashtag_response['like_count'][i] = post['like_count'] print("\nComments:") print(post['comments_count']) hashtag_response['comments_count'][i] = post['comments_count'] i += 1 req = json.dumps(hashtag_response) df = pd.read_json(req) df.to_csv(filename, index=False)