-
Notifications
You must be signed in to change notification settings - Fork 10
/
hashtag_discovery_recent_media.py
61 lines (46 loc) · 3 KB
/
hashtag_discovery_recent_media.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import subprocess
import argparse, os, csv
import time
from defines import getCreds, makeApiCall
import sys
if __name__ == '__main__':
filename = 'ig_hashtag_recent_media.csv'
open(filename, 'w').close()
input = open('ig_hashtag_input.csv', 'r')
content = input.readlines()
for line in content:
if line:
ig_hashtag = line.strip()
#Function Declared
def getHashtagInfo( params ) :
endpointParams = dict() # parameter to send to the endpoint
endpointParams['user_id'] = params['instagram_account_id'] # user id making request
endpointParams['q'] = ig_hashtag # hashtag name
endpointParams['fields'] = 'id,name' # fields to get back
endpointParams['access_token'] = params['access_token'] # access token
url = params['endpoint_base'] + 'ig_hashtag_search' # endpoint url
return makeApiCall( url, endpointParams, params['debug'] )
def getHashtagMedia( params ) :
endpointParams = dict() # parameter to send to the endpoint
endpointParams['user_id'] = params['instagram_account_id'] # user id making request
endpointParams['fields'] = 'id,permalink,comments_count,like_count,media_type,media_url,timestamp,caption' # fields to get back
endpointParams['access_token'] = params['access_token'] # access token
hashtagInfoResponse = getHashtagInfo( params ) # hit the api for some data!
params['hashtag_id'] = hashtagInfoResponse['json_data']['data'][0]['id']
params['type'] = 'recent_media' # set call to get top media for hashtag
url = params['endpoint_base'] + params['hashtag_id'] + '/' + params['type'] # endpoint url
return makeApiCall( url, endpointParams, params['debug'] ) # make the api call
params = getCreds() # get creds
params['debug'] = 'no' # set debug
response = getHashtagMedia( params ) # get users media from the api
try:
#Data From JSON
hashtag_post = response['json_data']['data']
#Data to CSV
with open(filename, 'a', encoding='utf-8') as csvfile:
employee_writer = csv.writer(csvfile, delimiter='\t', lineterminator='\r', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for pid in hashtag_post:
employee_writer.writerow([ig_hashtag,', ', pid.values()])
print(ig_hashtag + ' --- Done')
except:
print(ig_hashtag + ' --- Failed')