def __init__(self): self.State = StateHandler() self.Data = self.State.get_data() self.Config = self.State.get_config() self.Creds = getCreds() self.Halt = False
def getCommentatorAndComment(commentID): params = getCreds() params['debug'] = 'no' response = getCommentInfo(params, commentID) commentator = response['json_data']['username'] comment = response['json_data']['text'] return commentator, comment
""" Get long lived access token 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} Returns: object: data from the endpoint """ endpointParams = dict() # parameter to send to the endpoint endpointParams[ 'grant_type'] = 'fb_exchange_token' # tell facebook we want to exchange token endpointParams['client_id'] = params[ 'client_id'] # client id from facebook app endpointParams['client_secret'] = params[ 'client_secret'] # client secret from facebook app endpointParams['fb_exchange_token'] = params[ 'access_token'] # access token to get exchange for a long lived token url = params['endpoint_base'] + 'oauth/access_token' # endpoint url return makeApiCall(url, endpointParams, params['debug']) # make the api call params = getCreds() # get creds params['debug'] = 'yes' # set debug response = getLongLivedAccessToken(params) # hit the api for some data! print("\n ---- ACCESS TOKEN INFO ----\n") # section header print("Access Token:") # label print(response['json_data']['access_token']) # display access token
Returns: object: data from the endpoint """ endpointParams = dict() endpointParams['fields'] = 'business_discovery.username(' + params[ 'ig_username'] + '){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count}' endpointParams['access_token'] = params['access_token'] url = params['endpoint_base'] + params['instagram_account_id'] return makeApiCall(url, endpointParams, params['debug']) params = getCreds() params['debug'] = 'no' response = getAccountInfo(params) print "\n---- ACCOUNT INFO -----\n" print "username:"******"\nwebsite:" print response['json_data']['business_discovery']['website'] print "\nnumber of posts:" print response['json_data']['business_discovery']['media_count'] print "\nfollowers:" print response['json_data']['business_discovery']['followers_count'] print "\nfollowing:" print response['json_data']['business_discovery']['follows_count'] print "\nprofile picture url:"
from defines import getCreds, makeApiCall import datetime def debugAccessToken( params ) : endpointParams = dict() # endpoint'e gönderilicek parametreler endpointParams['input_token'] = params['access_token'] # access tokenini gir endpointParams['access_token'] = params['access_token'] # hata ayıklama bilgilerini almak için access token url = params['graph_domain'] + '/debug_token' # endpoint url return makeApiCall( url, endpointParams, params['debug'] ) #api çağrısı döndür params = getCreds() # creds'leri al params['debug'] = 'yes' # debug'u ayarla response = debugAccessToken( params ) # apiye bilgileri almak için istek print ("\nData Access Expires at: ") # label print (datetime.datetime.fromtimestamp( response['json_data']['data']['data_access_expires_at'] )) # token'in süresi dolduğunda göster print ("\nToken Expires at: ") # label print (datetime.datetime.fromtimestamp( response['json_data']['data']['expires_at'] )) # token'in süresi dolduğunda göster
endpointParams = dict() # parameter to send to the endpoint endpointParams['user_id'] = params['instagram_account_id'] # user id making request endpointParams['fields'] = 'id,children,caption,comment_count,like_count,media_type,media_url,permalink' # fields to get back endpointParams['access_token'] = params['access_token'] # access token url = params['endpoint_base'] + params['hashtag_id'] + '/' + params['type'] # endpoint url return makeApiCall( url, endpointParams, params['debug'] ) # make the api call try : # try and get param from command line hashtag = sys.argv[1] # hashtag to get info on except : # default to coding hashtag hashtag = 'rip_Jacques_Chirac' # hashtag to get info on params = getCreds() # params for api call params['hashtag_name'] = hashtag # add on the hashtag we want to search for hashtagInfoResponse = getHashtagInfo( params ) # hit the api for some data! params['hashtag_id'] = hashtagInfoResponse['json_data']['data'][0]['id']; # store hashtag id print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> HASHTAG INFO <<<<<<<<<<<<<<<<<<<<\n" # section heading print "\nHashtag: " + hashtag # display hashtag print "Hashtag ID: " + params['hashtag_id'] # display hashtag id print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> HASHTAG TOP MEDIA <<<<<<<<<<<<<<<<<<<<\n" # section heading params['type'] = 'top_media' # set call to get top media for hashtag hashtagTopMediaResponse = getHashtagMedia( params ) # hit the api for some data! print "\n\n\n\t\t\t >>>>>>>>>>>>>>>> Enstablish mongodb connexion <<<<<<<<<<<<<<<<\n" client = MongoClient('localhost', 27017) db = client['db_instagram']
def return_last_media(uploaded_username): def getUserMedia(params, ig_username, pagingUrl=''): """ Get users media API Endpoint: https://graph.facebook.com/{graph-api-version}/{ig-user-id}/media?fields={fields}&access_token={access-token} GET graph.facebook.com/[YOUR-IG-BUSINESS-ACCOUNT-ID]?fields=business_discovery.username(USERNAME){media{caption,media_url,media_type,like_count,comments_count,id}} Returns: object: data from the endpoint """ endpointParams = dict() # parameter to send to the endpoint endpointParams[ 'fields'] = 'business_discovery.username(' + ig_username + '){media{timestamp,caption,media_url,media_type,like_count,comments_count,id}}' # string of fields to get back with the request for the account # endpointParams['fields'] = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username,like_count,comments_count' # fields to get back endpointParams['access_token'] = params['access_token'] # access token if ('' == pagingUrl): # get first page url = params['endpoint_base'] + params['instagram_account_id'] else: # get specific page url = params['endpoint_base'] + params['instagram_account_id'] endpointParams['after'] = pagingUrl # url = params['endpoint_base'] + params['instagram_account_id'] + pagingUrl # endpoint url return makeApiCall(url, endpointParams, params['debug']) # make the api call params = getCreds() # get creds params['debug'] = 'no' # set debug params['ig_username'] = uploaded_username media_list = list() #To store last 50 posts list_one = [] #To store last post details for ig_username in params['ig_username']: like_count = 0 comments_count = 0 media_dict = {} #To store last 50 posts post_count = 0 dict_one = {} #To store last post details media_dict['Username'] = ig_username dict_one['Username'] = ig_username response = getUserMedia(params, ig_username) # get users media from the api print("\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> {} <<<<<<<<<<<<<<<<<<<<\n". format(ig_username)) # print("\n\n----------Last POST ----------\n") # post heading try: # print("\nID:") # label # print(response['json_data']['business_discovery']['media']['data'][0]['id']) # link to post dict_one['Last Post Media ID'] = response['json_data'][ 'business_discovery']['media']['data'][0]['id'] except: dict_one['Last Post Media ID'] = None try: # print("\nLink to post:") # label # print(response['json_data']['business_discovery']['media']['data'][0]['media_url']) # link to post dict_one['Link to Last Post'] = response['json_data'][ 'business_discovery']['media']['data'][0]['media_url'] except: dict_one['Link to Last Post'] = None try: # print("\nPost caption:") # label # print(response['json_data']['business_discovery']['media']['data'][0]['caption'])# post caption dict_one['Last Post Caption'] = response['json_data'][ 'business_discovery']['media']['data'][0]['caption'] except: dict_one['Last Post Caption'] = None try: # print("\nMedia type:") # label # print(response['json_data']['business_discovery']['media']['data'][0]['media_type']) # type of media dict_one['Last Post Media Type'] = response['json_data'][ 'business_discovery']['media']['data'][0]['media_type'] except: dict_one['Last Post Media Type'] = None try: # print("\nLike Count:") # label # print(response['json_data']['business_discovery']['media']['data'][0]['like_count']) # type of media dict_one['Last Post Like Count'] = response['json_data'][ 'business_discovery']['media']['data'][0]['like_count'] except: dict_one['Last Post Like Count'] = None try: # print("\nComments Count:") # label # print(response['json_data']['business_discovery']['media']['data'][0]['comments_count']) # type of media dict_one['Last Post Comments Count'] = response['json_data'][ 'business_discovery']['media']['data'][0]['comments_count'] except: dict_one['Last Post Comments Count'] = None try: # print("\nPosted at:") # label # print(response['json_data']['business_discovery']['media']['data'][0]['timestamp']) # when it was posted dict_one['Last Post Posted At'] = response['json_data'][ 'business_discovery']['media']['data'][0]['timestamp'] except: dict_one['Last Post Posted At'] = None list_one.append(dict_one) # print("\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> PAGE 1 <<<<<<<<<<<<<<<<<<<<\n") # display page 1 of the posts try: for post in response['json_data']['business_discovery']['media'][ 'data']: # print("\n\n---------- POST ----------\n") # post heading # print("\nID:") # label # print(post['id']) # link to post # print("\nLink to post:") # label # print(post['media_url']) # link to post # print("\nPost caption:") # label # print(post['caption'])# post caption # print("\nMedia type:") # label # print(post['media_type']) # type of media # print("\nLike Count:") # label # print(post['like_count']) # type of media like_count += int(post['like_count']) # print("\nComments Count:") # label # print(post['comments_count']) # type of media comments_count += int(post['comments_count']) # print("\nPosted at:") # label # print(post['timestamp']) # when it was posted post_count += 1 except: like_count += 0 comments_count += 0 post_count += 0 params['debug'] = 'no' # set debug response = getUserMedia( params, ig_username, response['json_data']['business_discovery']['media']['paging'] ['cursors']['after']) # get next page of posts from the api # print("\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> PAGE 2 <<<<<<<<<<<<<<<<<<<<\n") # display page 2 of the posts try: for post in response['json_data']['business_discovery']['media'][ 'data']: # print("\n\n---------- POST ----------\n") # post heading # print("Link to post:") # label # print(post['media_url']) # link to post # print("\nPost caption:") # label # print(post['caption'])# post caption # print("\nMedia type:") # label # print(post['media_type']) # type of media # print("\nLike Count:") # label # print(post['like_count']) # type of media like_count += int(post['like_count']) # print("\nComments Count:") # label # print(post['comments_count']) # type of media comments_count += int(post['comments_count']) # print("\nPosted at:") # label # print(post['timestamp']) # when it was posted post_count += 1 except: like_count += 0 comments_count += 0 post_count += 0 media_dict['Last 50 Posts Like Count'] = like_count media_dict['Last 50 Posts Comments Count'] = comments_count media_dict['Total Posts Count'] = post_count media_list.append(media_dict) media_df = pd.DataFrame(media_list) one_df = pd.DataFrame(list_one) return media_df, one_df
def return_business_discovery(uploaded_username): def getAccountInfo(params, ig_username): """ Get info on a users account API Endpoint: https://graph.facebook.com/{graph-api-version}/{ig-user-id}?fields=business_discovery.username({ig-username}){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count}&access_token={access-token} Returns: object: data from the endpoint """ endpointParams = dict() # parameter to send to the endpoint endpointParams[ 'fields'] = 'business_discovery.username(' + 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 endpointParams['access_token'] = params['access_token'] # access token url = params['endpoint_base'] + params[ 'instagram_account_id'] # endpoint url return makeApiCall(url, endpointParams, params['debug']) # make the api call params = getCreds() # get creds params['debug'] = 'no' # set debug # username_df = pd.read_csv(uploaded_username) params['ig_username'] = uploaded_username user_info_list = [] for ig_username in params['ig_username']: user_info_dict = {} user_info_dict['Username'] = ig_username response = getAccountInfo(params, ig_username) # hit the api for some data! # print("\n---- ACCOUNT INFO -----\n") # display latest post info # print("username:"******"\nname:") # label # print(response['json_data']['business_discovery']['name']) user_info_dict['Name'] = response['json_data'][ 'business_discovery']['name'] except: user_info_dict['Name'] = None try: # print("\nID:") # label # print(response['json_data']['business_discovery']['id']) user_info_dict['User ID'] = response['json_data'][ 'business_discovery']['id'] except: user_info_dict['User ID'] = None try: # print("\nIG_ID:") # label # print(response['json_data']['business_discovery']['ig_id']) user_info_dict['User IG ID'] = response['json_data'][ 'business_discovery']['ig_id'] except: user_info_dict['User IG ID'] = None try: # print("\nwebsite:") # label # print(response['json_data']['business_discovery']['website']) # display users website user_info_dict['website'] = response['json_data'][ 'business_discovery']['website'] except: user_info_dict['website'] = None try: # print("\nnumber of posts:") # label # print(response['json_data']['business_discovery']['media_count']) # display number of posts user has made user_info_dict['Total Posts'] = response['json_data'][ 'business_discovery']['media_count'] except: user_info_dict['Total Posts'] = None try: # print("\nfollowers:") # label # print(response['json_data']['business_discovery']['followers_count']) # display number of followers the user has user_info_dict['Followers'] = response['json_data'][ 'business_discovery']['followers_count'] except: user_info_dict['Followers'] = None try: # print("\nfollowing:") # label # print(response['json_data']['business_discovery']['follows_count'] )# display number of people the user follows user_info_dict['Following'] = response['json_data'][ 'business_discovery']['follows_count'] except: user_info_dict['Following'] = None # try: # print("\nprofile picture url:") # label # print(response['json_data']['business_discovery']['profile_picture_url']) # display profile picutre url # user_info_dict['Profile Picture Url'] = response['json_data']['business_discovery']['profile_picture_url'] # except: # user_info_dict['Profile Picture Url'] = None try: # print("\nbiography:" )# label # print(response['json_data']['business_discovery']['biography']) # display users about section user_info_dict['Biography'] = response['json_data'][ 'business_discovery']['biography'] except: user_info_dict['Biography'] = None user_info_list.append(user_info_dict) user_info_df = pd.DataFrame(user_info_list) # one_df = get_user_media_one.return_media_one() media_df, one_df = get_user_media_new.return_last_media(uploaded_username) data_df = pd.merge(user_info_df, one_df, on="Username") data_df = pd.merge(data_df, media_df, on='Username') data_df['Avg Post Impressions'] = ( data_df['Last 50 Posts Like Count'] + data_df['Last 50 Posts Comments Count']) / data_df['Total Posts Count'] data_df['Organic Ratio'] = data_df['Avg Post Impressions'] / data_df[ 'Followers'] data_df['Follow Ratio'] = data_df['Following'] / data_df['Followers'] return data_df
def getInstagramMediaCommentResponse(): params = getCreds() params['debug'] = 'no' response = getInstagramMediaComment(params) return response
from defines import getCreds, makeApiCall def getUserPages( params ) : endpointParams = dict() endpointParams['access_token'] = params['access_token'] url = params['endpoint_base'] + 'me/accounts' return makeApiCall( url, endpointParams, params['debug'] ) # api çağrısı yap params = getCreds() # credsleri'al params['debug'] = 'no' # debug ayarla response = getUserPages( params ) # debug infoyu al print ("\n---- FACEBOOK PAGE INFO ----\n") # print ("Page Name:") print (response['json_data']['data'][0]['name']) print ("\nPage Category:") print (response['json_data']['data'][0]['category']) print ("\nPage Id:") print (response['json_data']['data'][0]['id'])
""" endpointParams = dict() #parameters to send to the endpoint endpointParams['access_token'] = params['access_token'] #access token endpointParams['limit'] = "400" #limit of comments to return url = params['endpoint_base'] + postID + '/comments' #endpoint url return fetchData(url, endpointParams, params['debug']) #API call def getAllComments(): """All in one function to get all the getComments of the last ig post Returns: object: data from the endpoint """ params = getCreds() #dictionary of credentials fbPages = getUserPages(params) #fb pages fbID = getFBPageID(fbPages) #fb page id igPage = getInstagramPage(params, fbID) #ig page igPageID = getIGPageID(igPage) #ig page id myPageID = igPageID igPosts = getInstagramPosts(params, igPageID) #ig media content postID = getLastPostID(igPosts) #ig last post return getComments(params, postID) def getUserProfilePic(user): """Get user's instagram profile picture API Endpoint: https://graph.facebook.com/{instagram_page_id}?fields=business_discovery.username(bluebottle){fields} Args:
#Fonksiyon bildirildi def getAccountInfo(params): endpointParams = dict() # parameter to send to the endpoint endpointParams[ 'fields'] = 'business_discovery.username(' + ig_username + '){id,name,follows_count,followers_count,media_count,website,profile_picture_url,biography}' # string of fields to get back with the request for the account endpointParams['access_token'] = params[ 'access_token'] # access token url = params['endpoint_base'] + params[ 'instagram_account_id'] # endpoint url return makeApiCall(url, endpointParams, params['debug']) # make the api call params = getCreds() # creds'ler alındı params['debug'] = 'no' # debug ayarlandı response = getAccountInfo(params) # apiye bilgileri almak için istek try: #Json'dan bilgileri al user_id = response['json_data']['business_discovery']['id'] name = response['json_data']['business_discovery']['name'] follows_count = response['json_data']['business_discovery'][ 'follows_count'] follower_count = response['json_data']['business_discovery'][ 'followers_count'] media_count = response['json_data']['business_discovery'][ 'media_count'] website = response['json_data']['business_discovery']['website'] profile_pic = response['json_data']['business_discovery'][