Skip to content

Commit dccf969

Browse files
committed
instagram graph api hashtag search with python
1 parent 8c5617c commit dccf969

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from defines import getCreds, makeApiCall
2+
import sys
3+
4+
def getHashtagInfo( params ) :
5+
""" Get info on a hashtag
6+
7+
API Endpoint:
8+
https://graph.facebook.com/{graph-api-version}/ig_hashtag_search?user_id={user-id}&q={hashtag-name}&fields={fields}
9+
10+
Returns:
11+
object: data from the endpoint
12+
13+
"""
14+
15+
endpointParams = dict() # parameter to send to the endpoint
16+
endpointParams['user_id'] = params['instagram_account_id'] # user id making request
17+
endpointParams['q'] = params['hashtag_name'] # hashtag name
18+
endpointParams['fields'] = 'id,name' # fields to get back
19+
endpointParams['access_token'] = params['access_token'] # access token
20+
21+
url = params['endpoint_base'] + 'ig_hashtag_search' # endpoint url
22+
23+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
24+
25+
def getHashtagMedia( params ) :
26+
""" Get posts for a hashtag
27+
28+
API Endpoints:
29+
https://graph.facebook.com/{graph-api-version}/{ig-hashtag-id}/top_media?user_id={user-id}&fields={fields}
30+
https://graph.facebook.com/{graph-api-version}/{ig-hashtag-id}/recent_media?user_id={user-id}&fields={fields}
31+
32+
Returns:
33+
object: data from the endpoint
34+
35+
"""
36+
37+
endpointParams = dict() # parameter to send to the endpoint
38+
endpointParams['user_id'] = params['instagram_account_id'] # user id making request
39+
endpointParams['fields'] = 'id,children,caption,comment_count,like_count,media_type,media_url,permalink' # fields to get back
40+
endpointParams['access_token'] = params['access_token'] # access token
41+
42+
url = params['endpoint_base'] + params['hashtag_id'] + '/' + params['type'] # endpoint url
43+
44+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
45+
46+
def getRecentlySearchedHashtags( params ) :
47+
""" Get hashtags a user has recently search for
48+
49+
API Endpoints:
50+
https://graph.facebook.com/{graph-api-version}/{ig-user-id}/recently_searched_hashtags?fields={fields}
51+
52+
Returns:
53+
object: data from the endpoint
54+
55+
"""
56+
57+
endpointParams = dict() # parameter to send to the endpoint
58+
endpointParams['fields'] = 'id,name' # fields to get back
59+
endpointParams['access_token'] = params['access_token'] # access token
60+
61+
url = params['endpoint_base'] + params['instagram_account_id'] + '/' + 'recently_searched_hashtags' # endpoint url
62+
63+
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
64+
65+
try : # try and get param from command line
66+
hashtag = sys.argv[1] # hashtag to get info on
67+
except : # default to coding hashtag
68+
hashtag = 'coding' # hashtag to get info on
69+
70+
params = getCreds() # params for api call
71+
params['hashtag_name'] = hashtag # add on the hashtag we want to search for
72+
hashtagInfoResponse = getHashtagInfo( params ) # hit the api for some data!
73+
params['hashtag_id'] = hashtagInfoResponse['json_data']['data'][0]['id']; # store hashtag id
74+
75+
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> HASHTAG INFO <<<<<<<<<<<<<<<<<<<<\n" # section heading
76+
print "\nHashtag: " + hashtag # display hashtag
77+
print "Hashtag ID: " + params['hashtag_id'] # display hashtag id
78+
79+
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> HASHTAG TOP MEDIA <<<<<<<<<<<<<<<<<<<<\n" # section heading
80+
params['type'] = 'top_media' # set call to get top media for hashtag
81+
hashtagTopMediaResponse = getHashtagMedia( params ) # hit the api for some data!
82+
83+
for post in hashtagTopMediaResponse['json_data']['data'] : # loop over posts
84+
print "\n\n---------- POST ----------\n" # post heading
85+
print "Link to post:" # label
86+
print post['permalink'] # link to post
87+
print "\nPost caption:" # label
88+
print post['caption'] # post caption
89+
print "\nMedia type:" # label
90+
print post['media_type'] # type of media
91+
92+
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> HASHTAG RECENT MEDIA <<<<<<<<<<<<<<<<<<<<\n" # section heading
93+
params['type'] = 'recent_media' # set call to get recent media for hashtag
94+
hashtagRecentMediaResponse = getHashtagMedia( params ) # hit the api for some data!
95+
96+
for post in hashtagRecentMediaResponse['json_data']['data'] : # loop over posts
97+
print "\n\n---------- POST ----------\n" # post heading
98+
print "Link to post:" # label
99+
print post['permalink'] # link to post
100+
print "\nPost caption:" # label
101+
print post['caption'] # post caption
102+
print "\nMedia type:" # label
103+
print post['media_type'] # type of media
104+
105+
print "\n\n\n\t\t\t >>>>>>>>>>>>>>>>>>>> USERS RECENTLY SEARCHED HASHTAGS <<<<<<<<<<<<<<<<<<<<\n" # section heading
106+
getRecentSearchResponse = getRecentlySearchedHashtags( params ) # hit the api for some data!
107+
108+
for hashtag in getRecentSearchResponse['json_data']['data'] : # looop over hashtags
109+
print "\n\n---------- SEARCHED HASHTAG ----------\n" # searched heading
110+
print "\nHashtag: " + hashtag['name'] # display hashtag
111+
print "Hashtag ID: " + hashtag['id'] # display hashtag id

0 commit comments

Comments
 (0)