-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram_monitor.py
58 lines (40 loc) · 1.6 KB
/
instagram_monitor.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
import json
import os
import pprint
import unicodedata
import numpy
import requests
FIELD = 'business_discovery.username({name}){{name,username,followers_count,media{{comments_count,like_count}}}}'
API_VER = '13.0'
ACCESS_TOKEN = os.environ['TOKEN']
API_ENDPOINT = 'https://graph.facebook.com'
MY_IG_ID = '17841405931345347'
request_url = API_ENDPOINT + '/v' + API_VER + '/' + MY_IG_ID + '?fields=' + \
FIELD + '&access_token=' + ACCESS_TOKEN
def get_data(username=None):
if username is None:
return Exception
return json.loads(requests.get(request_url.format(name=username)).text)
def process_data(data):
username = data['business_discovery']['username']
display_name = data['business_discovery']['name']
display_name = unicodedata.normalize('NFKC', display_name)
follower_count = data['business_discovery']['followers_count']
comments_count_mean = numpy.mean([obj['comments_count'] for obj in data['business_discovery']['media']['data']])
like_counts = [obj['like_count'] for obj in data['business_discovery']['media']['data'] if 'like_count' in obj]
if not like_counts:
like_count_mean = 0.0
else:
like_count_mean = numpy.mean(like_counts)
return [username, display_name, follower_count, comments_count_mean, like_count_mean]
with open('instagram_user.list', 'r') as f:
user_list = f.read().split()
pprint.pprint(user_list)
all_data = []
for user in user_list:
if '#' in user:
continue
json_data = get_data(user)
proceed_data = process_data(data=json_data)
all_data.append(proceed_data)
pprint.pprint(all_data)