-
Notifications
You must be signed in to change notification settings - Fork 26
/
social_elastic.py
241 lines (197 loc) · 9.67 KB
/
social_elastic.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import sys
import json
import datetime
import calendar
import time
from elasticsearch import Elasticsearch, TransportError, ConnectionError, ConnectionTimeout
import get_fb_data
import get_insta_data
# Facebook Globals
OWNED_PAGES_TOKENS = {
"MyPage1": os.environ['PAGE1_FB_PERM_TOKEN'],
"MyPage2": os.environ['PAGE2_FB_PERM_TOKEN'],
"MyPage3": os.environ['PAGE3_FB_PERM_TOKEN']
}
# Instagram Globals
MY_INSTA_TOKEN = os.environ['MY_INSTA_TOKEN']
MY_INSTA_USER_ID = MY_INSTA_TOKEN.split('.')[0]
#ELASTIC_HOSTS = [os.environ['ELASTIC_HOST_DEV'], os.environ['ELASTIC_HOST_PROD'], os.environ['ELASTIC_HOST_PROD2']]
ELASTIC_HOSTS = [os.environ['ELASTIC_HOST_PROD2']]
#ELASTIC_HOSTS = [os.environ['ELASTIC_HOST_DEV']]
def create_bulk_req_elastic(json_data, index, doc_type, id_field):
action_data_string = ""
for i, json_post in enumerate(json_data):
index_action = {"index":{"_index":index, "_type":doc_type, "_id":json_post[id_field]}}
action_data_string += json.dumps(index_action, separators=(',', ':')) + '\n' + json.dumps(json_post, separators=(',', ':')) + '\n'
return action_data_string
def insert_bulk_elastic(action_data_string, hosts):
return_ack_list = []
for host in hosts:
es = Elasticsearch(host)
success = False
while success == False:
try:
return_ack_list.append(es.bulk(body=action_data_string))
success = True
except (ConnectionError, ConnectionTimeout, TransportError) as e:
print e
print "\nRetrying in 3 seconds"
time.sleep(3)
return return_ack_list
def update_alias(source_index, alias_index, hosts):
return_ack_list = []
for host in hosts:
es = Elasticsearch(host)
assert(es.indices.exists(index=source_index))
# Delete existing alias index if it exists
if es.indices.exists_alias(name=alias_index) == True:
es.indices.delete_alias(index='_all', name=alias_index)
return_ack_list.append(es.indices.put_alias(index=source_index, name=alias_index))
return return_ack_list
def insert_ig_followers(user_id, access_token, index, doc_type):
return_ack_list = []
num_followers = get_insta_data.get_followers(user_id, access_token)
followers_insert_timestamp = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + 'Z'
for host in ELASTIC_HOSTS:
es = Elasticsearch(host)
return_ack_list.append(
es.index(op_type='index', index='followers', doc_type='instagram',\
body={"Type": "instagram", "Followers": num_followers, "Timestamp": followers_insert_timestamp}))
return return_ack_list
def put_fb_template(template_name, template_pattern, raw_fields_pattern, hosts):
return_ack_list = []
template_body = {
"template" : template_pattern,
"mappings" : {
"_default_" : {
"_all" : {"enabled" : True, "omit_norms" : True},
"properties": {
raw_fields_pattern: {
"type": "string",
"fielddata" : { "format" : "paged_bytes" },
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
},
"stemmed": {
"type": "string",
"fielddata" : { "format" : "paged_bytes" },
"analyzer": "english"
}
}
}
}
}
}
}
for host in hosts:
es = Elasticsearch(host)
return_ack_list.append(es.indices.put_template(name=template_name, body=template_body, create=False))
return return_ack_list
def is_date_string(date_string):
try:
date_object = datetime.datetime.strptime(date_string, '%Y-%m-%d')
return True
except ValueError as e:
return False
def ig_main(local_from_date):
instagram_doc_type = 'instagram-media-endpoint'
local_now = datetime.datetime.now()
index_suffix = local_now.strftime('%Y%m%d-%H%M')
instagram_index = 'instagram-' + index_suffix
instagram_index_alias = 'instagram'
api_scraped_rows = get_insta_data.scrape_insta_items(MY_INSTA_USER_ID, local_from_date, MY_INSTA_TOKEN)
posts_with_views = get_insta_data.append_views(api_scraped_rows)
posts_with_views_impressions = get_insta_data.append_social_analytics(posts_with_views)
# Create request
action_data_string = create_bulk_req_elastic(posts_with_views_impressions, instagram_index, instagram_doc_type, 'Post ID')
print "\nInserting {} documents into Elasticsearch at {}".format(str(len(posts_with_views_impressions)), ELASTIC_HOSTS)
# Insert documents via Bulk API
insert_acks = insert_bulk_elastic(action_data_string, ELASTIC_HOSTS)
if all(response.get('errors') == False for response in insert_acks):
print "Success"
else:
print "Errors occured with new index {}".format(instagram_index_alias)
for host_el in insert_acks:
for el in host_el['items']:
if el.get('index').get('error') is not None:
print "_id: " + el.get('index').get('_id')
print el.get('index').get('error')
#sys.exit()
# Redirect alias so Kibana picks up latest snapshot
print "\nUpdating Instagram alias"
update_alias_acks = update_alias(instagram_index, instagram_index_alias, ELASTIC_HOSTS)
if all(response.get('acknowledged') == True for response in update_alias_acks):
print "Success. {} points to {}".format(instagram_index_alias, instagram_index)
else:
print "\nFailed to update Instagram alias"
# Also push in Instagram followers
# Instagram Followers
print "\nGetting Instagram followers"
followers_index = 'follwers'
followers_doctype_ig = 'instagram'
insert_followers_acks = insert_ig_followers(MY_INSTA_USER_ID, MY_INSTA_TOKEN, followers_index, followers_doctype_ig)
if all(response.get('acknowledged') == True for response in update_alias_acks):
print "Success. Inserted followers into Elasticsearch at {}".format(ELASTIC_HOSTS)
else:
print "\nFailed to insert followers into Elasticsearch at {}".format(ELASTIC_HOSTS)
def fb_main(local_from_date):
facebook_video_doctype = 'facebook-video-endpoint'
facebook_post_doctype = 'facebook-post-endpoint'
utc_now = datetime.datetime.utcnow()
utc_posix_until_date = calendar.timegm(utc_now.timetuple())
index_suffix = datetime.datetime.now().strftime('%Y%m%d-%H%M')
facebook_index = 'facebook-' + index_suffix
facebook_index_alias = 'facebook'
print "Processing Videos"
fb_video_data = get_fb_data.scrape_fb_pages_items(OWNED_PAGES_TOKENS.keys(), local_from_date, utc_posix_until_date, get_fb_data.get_fb_page_video_data, get_fb_data.process_fb_page_video_all_metrics)
print "\nProcessing Posts"
fb_post_data = get_fb_data.scrape_fb_pages_items(OWNED_PAGES_TOKENS.keys(), local_from_date, utc_posix_until_date, get_fb_data.get_fb_page_post_data, get_fb_data.process_fb_page_post)
print "\nInserting {} post documents and {} video documents into Elasticsearch at {}".format(str(len(fb_post_data)), str(len(fb_video_data)), ELASTIC_HOSTS)
for host in ELASTIC_HOSTS:
action_data_string_video = create_bulk_req_elastic(fb_video_data, facebook_index, facebook_video_doctype, 'Video ID')
action_data_string_post = create_bulk_req_elastic(fb_post_data, facebook_index, facebook_post_doctype, 'Post ID')
# Insert video documents via Bulk API
insert_acks_video = insert_bulk_elastic(action_data_string_video, ELASTIC_HOSTS)
# Insert post documents via Bulk API
insert_acks_post = insert_bulk_elastic(action_data_string_post, ELASTIC_HOSTS)
if all(response.get('errors') == False for response in insert_acks_video + insert_acks_post):
print "Success"
else:
print "Errors occured for new index {}".format(facebook_index_alias)
for host_el in insert_acks_video + insert_acks_post:
for el in host_el['items']:
if el.get('index').get('error') is not None:
print "_id: " + el.get('index').get('_id')
print el.get('index').get('error')
#sys.exit()
print "\nUpdating Facebook alias"
update_alias_acks = update_alias(facebook_index, facebook_index_alias, ELASTIC_HOSTS)
if all(response.get('acknowledged') == True for response in update_alias_acks):
print "Success. {} points to {}".format(facebook_index_alias, facebook_index)
else:
print "\nFailed to update Facebook alias"
if __name__ == '__main__':
if not is_date_string(sys.argv[2]) or len(sys.argv) != 3:
print "python {} <fb/ig> <from-date: yyyy-mm-dd>".format(sys.argv[0])
sys.exit()
else:
local_from_date = datetime.datetime.strptime(sys.argv[2], '%Y-%m-%d')
# Verify ES clusters are reachable
for host in ELASTIC_HOSTS:
es = Elasticsearch(host)
try:
if es.ping() == False:
print "{} is not reachable".format(host)
sys.exit()
except ConnectionError:
print "{} is not reachable".format(host)
sys.exit()
# Only need to put a template in once, but little harm in overwriting
put_fb_template('facebook_template', 'facebook-*', 'Headline', ELASTIC_HOSTS)
if sys.argv[1] == 'fb':
fb_main(local_from_date)
elif sys.argv[1] == 'ig':
ig_main(local_from_date)