-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
executable file
·381 lines (288 loc) · 10.9 KB
/
api.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import json
from birdy.twitter import UserClient, BirdyException
from time import sleep
import string
import matplotlib.pyplot as plt
import numpy as np
import urllib
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import mpld3
import urllib.request
import logic
import requests
watchTowerIP = "https://watchtower.openmaker.eu"
watchTowerPort = ""
watchTowerVersion = "v1.3"
spirometerIP = "http://178.62.229.16"
spirometerPort = "5000"
spirometerVersion = ""
def get_user_topics(crm_ids):
all_topics = []
for crm_id in crm_ids:
user_topics = logic.get_user_topics(crm_id)
all_topics.append({"crm_id": crm_id, "topics": user_topics})
return all_topics
def get_psychometric_scores(crm_ids):
all_scores = []
for crm_id in crm_ids:
personality_scores = logic.get_psychometric_scores(crm_id)
all_scores.append({"crm_id": crm_id, "scores": personality_scores})
return all_scores
def get_feedbacks(crm_ids, content_ids, question_ids):
all_feedbacks = []
for crm_id in crm_ids:
feedbacks = logic.get_feedbacks(crm_id, content_ids, question_ids)
all_feedbacks.append({"crm_id": crm_id, "feedbacks": feedbacks})
return all_feedbacks
def convert_fig_to_html(fig):
canvas = FigureCanvas(fig)
png_output = StringIO.StringIO()
canvas.print_png(png_output)
data = png_output.getvalue().encode('base64')
return '<img src="data:image/png;base64,{}">'.format(urllib.quote(data.rstrip('\n')))
def get_articles(userID, topic_names):
topic_info = urllib.request.urlopen(watchTowerIP + watchTowerPort + "/api/" + watchTowerVersion + "/get_topics?keywords="+topic_names).read()
topic_info = json.loads(topic_info.decode('utf-8'))
topic_id = topic_info['topics'][0]['topic_id']
topic_name = topic_info['topics'][0]['topic_name']
articles = urllib.request.urlopen(watchTowerIP + watchTowerPort + "/api/" + watchTowerVersion + "/get_news?topic_ids="+str(topic_id)+"&date=month").read()
articles = json.loads(articles.decode('utf-8'))
articleInfo = articles['news'][np.random.randint(len(articles['news']))]
return articleInfo
def get_question_articles(userID, articleInfo):
# Here we can customize how we choose the qestion (from logic.get_question)
question = list(logic.get_questions())[0]
del question['_id']
return question
def demonstrate_OMBot_interaction(userID, topic_names):
articleInfo = get_articles(userID, topic_names)
art_summary = articleInfo['summary']
art_url = articleInfo['url']
question = get_question_articles(userID, articleInfo)
print(question)
sid = question['StatementID']
t = question['text']
q = question['question']
a1 = question['response_choices'][0]
a2 = question['response_choices'][1]
a3 = question['response_choices'][2]
return art_summary, art_url, sid, t, q, a1, a2, a3
def ombotChoice(choice, sid):
logic.add_answer_to_db(5, sid, choice)
def spirometer_categories():
try:
categories = urllib.request.urlopen(spirometerIP + ":" + spirometerPort).read()
categories = json.loads(categories.decode('utf-8'))
except:
categories = {"error": "Not found"}
return categories
def spirometer_scoreboard():
try:
scoreboard = urllib.request.urlopen(spirometerIP + ":" + spirometerPort + "/scoreboard").read()
scoreboard = json.loads(scoreboard.decode('utf-8'))
except:
scoreboard = {"error": "Not found"}
return scoreboard
def spirometer_gui_api(screen_name):
gui_api_result = requests.get(spirometerIP + ":" + spirometerPort + "/gui/api/"+screen_name)
html_doc = ""
for line in gui_api_result:
html_doc += line.strip().decode('utf-8')
return html_doc
def spirometer_scoreboardCategory(category):
try:
category_info = urllib.request.urlopen(spirometerIP + ":" + spirometerPort + "/scoreboard/"+str(category)).read()
category_info = json.loads(category_info.decode('utf-8'))
except:
category_info = {"error": "Not found"}
print(category_info)
return category_info
def spirometer_influencer(influencer):
try:
influencer_info = urllib.request.urlopen(spirometerIP + ":" + spirometerPort + "/influencer/"+str(influencer)).read()
influencer_info = json.loads(influencer_info.decode('utf-8'))
except:
influencer_info = {"error": "Not found"}
return influencer_info
def omn_crawler_get_profiles(twitter_ids,crmIDs):
crmIDs_profile = []
ids = []
isTwitterID = True
if len(twitter_ids) != 0:
ids = twitter_ids
else:
ids = crmIDs
isTwitterID = False
for _id in ids:
twitterID, crmID, twitter_profile = logic.get_twitter_profiles(_id,isTwitterID)
crmIDs_profile.append({"twitter_id":twitterID, "crm_id":crmID, "twitter_profile":twitter_profile})
return crmIDs_profile
def omn_crawler_get_followers(twitter_ids,crmIDs,page,from_date,to_date,only_crm_users):
followers = []
ids = []
isTwitterID = True
if len(twitter_ids) != 0:
ids = twitter_ids
else:
ids = crmIDs
isTwitterID = False
for _id in ids:
twitterID, crmID, followerIDs, capsuleIDs = logic.get_twitter_followers(_id,from_date,to_date,page,isTwitterID,only_crm_users)
temp = []
for i in range(len(followerIDs)):
temp.append({"follower_id":str(followerIDs[i]),"crm_id":str(capsuleIDs[i])})
followers.append({"twitter_id":twitterID, "crm_id":crmID, "follower_ids":temp})
return followers
def omn_crawler_get_friends(twitter_ids,crmIDs,page,from_date,to_date,only_crm_users):
friends = []
ids = []
isTwitterID = True
if len(twitter_ids) != 0:
ids = twitter_ids
else:
ids = crmIDs
isTwitterID = False
for _id in ids:
twitterID, crmID, friendIDs, capsuleIDs = logic.get_twitter_friends(_id,from_date,to_date,page,isTwitterID,only_crm_users)
temp = []
for i in range(len(friendIDs)):
temp.append({"friend_id":str(friendIDs[i]),"crm_id":str(capsuleIDs[i])})
friends.append({"twitter_id":twitterID, "crm_id":crmID, "friend_ids":temp})
return friends
def omn_crawler_get_tweets(twitter_ids,crmIDs,page):
all_tweets = []
ids = []
isTwitterID = True
if len(twitter_ids) != 0:
ids = twitter_ids
else:
ids = crmIDs
isTwitterID = False
for _id in ids:
twitterID, crmID, tweets = logic.get_twitter_tweets(_id,page,isTwitterID)
all_tweets.append({"twitter_id":twitterID, "crm_id":crmID, "tweets":tweets})
return all_tweets
def omn_crawler_get_ids(twitter_ids,crmIDs):
all_ids = []
for tid in twitter_ids:
cid = logic.twitter_id_to_crm_id(tid)
all_ids.append({"twitter_id":tid, "crm_id":cid})
for cid in crmIDs:
tid = logic.crm_id_to_twitter_id(cid)
all_ids.append({"twitter_id":tid, "crm_id":cid})
return all_ids
def omn_crawler_get_news(twitter_ids,crmIDs,page):
all_news = []
ids = []
isTwitterID = True
if len(twitter_ids) != 0:
ids = twitter_ids
else:
ids = crmIDs
isTwitterID = False
for _id in ids:
twitterID, crmID, news = logic.get_twitter_news(_id,page,isTwitterID)
all_news.append({"twitter_id":twitterID, "crm_id":crmID, "news":news})
return all_news
def omn_crawler_get_crm_users(column_names,page):
header = {'Authorization': "AUTHORIZATION_CREDENTIALS"}
parties = []
r = requests.get('https://api.capsulecrm.com/api/v2/parties?embed=tags,fields,organisation&'+"page="+str(page), headers=header)
if len(r.json()['parties']) > 0:
for user in r.json()['parties']:
temp = {}
for c in column_names:
try:
temp[c] = user[c]
except:
pass
if len(temp) > 0:
parties.append(temp)
return {"parties": parties}
def recommendation_news(crmIDs, page):
if crmIDs == []:
_, news, feedback_type, prev_page, next_page = logic.get_recommended_news('1', page)
return {"news":news, "feedback_type": feedback_type, 'prev_page': prev_page, 'next_page': next_page}
else:
recommended_news = []
for _id in crmIDs:
crmID, news, feedback_type, prev_page, next_page = logic.get_recommended_news(_id, page)
recommended_news.append({"crm_id":crmID, "news":news, "feedback_type": feedback_type, 'prev_page': prev_page, 'next_page': next_page})
return {"users":recommended_news}
def recommendation_users(crmIDs):
recommended_users = []
for _id in crmIDs:
crmID, rec_users = logic.get_recommended_users(_id)
recommended_users.append({"crm_id":crmID, "recommended_users":rec_users})
return recommended_users
def recommendation_questions(crmIDs, count, context):
recommended_questions = []
for _id in crmIDs:
recom_questions = logic.get_recommended_questions(_id,count,context)
recommended_questions.append({"crm_id":_id, "questions": recom_questions})
return recommended_questions
def recommendation_events(crmIDs, page):
if crmIDs == []:
_, events, feedback_type, prev_page, next_page = logic.get_recommended_events('1', page)
return {"events":events, "feedback_type": feedback_type, 'prev_page': prev_page, 'next_page': next_page}
else:
recommended_events = []
for _id in crmIDs:
crmID, events, feedback_type, prev_page, next_page = logic.get_recommended_events(_id, page)
recommended_events.append({"crm_id":crmID, "events":events, "feedback_type": feedback_type, 'prev_page': prev_page, 'next_page': next_page})
return {"users":recommended_events}
def get_om_user_profiles(twitter_ids, crmIDs, page, sorting, descending, filters):
if len(twitter_ids) == 0 and len(crmIDs) == 0:
return logic.get_om_user_profiles(page,sorting,descending,filters)
else:
om_user_profiles = []
ids = []
isTwitterID = True
if len(twitter_ids) != 0:
ids = twitter_ids
else:
ids = crmIDs
isTwitterID = False
for _id in ids:
om_user_profiles.append(logic.get_om_user_profiles2(_id,isTwitterID))
return om_user_profiles
def omp_analytics_get_follower_change(twitter_ids, crmIDs):
follower_changes = []
ids = []
isTwitterID = True
if len(twitter_ids) != 0:
ids = twitter_ids
else:
ids = crmIDs
isTwitterID = False
for _id in ids:
twitterID, crmID, followerCountChange, followerChangeDate = logic.get_follower_change(_id,isTwitterID)
follower_changes.append({"twitter_id":twitterID, "crm_id":crmID, "follower_counts":followerCountChange, "follower_dates":followerChangeDate})
return follower_changes
def omn_crawler_get_mentions(twitter_ids, crmIDs):
all_mentions = []
ids = []
isTwitterID = True
if len(twitter_ids) != 0:
ids = twitter_ids
else:
ids = crmIDs
isTwitterID = False
for _id in ids:
twitterID, crmID, mentioned_to, mentioned_by = logic.get_twitter_mentions(_id,isTwitterID)
all_mentions.append({"twitter_id":twitterID, "crm_id":crmID, "mentioned_to":mentioned_to, "mentioned_by": mentioned_by})
return all_mentions
tnc_db_names = ["TaylanCemgilMLdb", "UskudarliTwitterdb"]
# tnc: twitter_network_crawler
def tnc_get_profiles(dbID):
twitter_profiles = logic.get_tnc_twitter_profiles(dbID)
return {'users':twitter_profiles}
def tnc_get_followers(dbID, from_date,to_date):
twitter_followers = logic.get_tnc_twitter_followers(dbID,from_date,to_date)
return {'users':twitter_followers}
def tnc_get_friends(dbID, from_date,to_date):
twitter_friends = logic.get_tnc_twitter_friends(dbID,from_date,to_date)
return {'users':twitter_friends}
def tnc_get_tweets(dbID):
twitter_tweets = logic.get_tnc_twitter_tweets(dbID)
return {'users':twitter_tweets}