-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·296 lines (221 loc) · 7.98 KB
/
main.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
import glob
import os
import sys
import time
import tornado.ioloop
import tornado.web
import WhatsAppWebScraper
from Webdriver import Webdriver
from WhatsAppDB import WhatsAppDB
"""
run the WhatsApp web scrapper.
"""
def scrape_whatsapp_and_analyze_db():
"""
runs the whatsapp web scrapping procedure.
:param db: the WhatsAppDB object
"""
# print("create driver")
driver = Webdriver() # create new driver
scraper = WhatsAppWebScraper.WhatsAppWebScraper(driver) # create new WhatsApp scraper
scraper.scrape(DB) # scrape
print("finished scraping,load headset instructions")
driver.close() # close driver
DB.convert_to_datetime()
DB.run_data_analysis_and_store_results()
def InitializeDBAndAvatars():
"""
Initialize a new DB instance,
and remove all avatars
"""
global DB # TODO
DB = WhatsAppDB()
files = glob.glob('static/tempAvatars/*')
for f in files:
if 'contact_avatar' in f:
os.remove(f)
"""""
web page handlers:
"""""
class IphoneHandler(tornado.web.RequestHandler):
def get(self):
print("Stage 6: Iphone Chosen")
DB.phone = "ios"
self.finish()
class AndroidHandler(tornado.web.RequestHandler):
def get(self):
print("Stage 6: Android Chosen")
DB.phone = "android"
self.finish()
class LandingHandler(tornado.web.RequestHandler):
def get(self):
print("Stage 1: Loading HomePage")
self.render("LandingPage.html")
class NameSubmitHandler(tornado.web.RequestHandler):
def get(self):
print("Stage 3: Name Submitted, Loading TermsPage")
# These variables hold the users input
first_name = self.get_argument("first")
last_name = self.get_argument("last")
print("User first name:", first_name)
print("User last name:", last_name)
# assign the first name to the user name attribute in DB
DB.user_name = first_name
DB.user_first_name = first_name
DB.user_last_name = last_name
# writes the users name to file
with open('users', 'a', encoding='utf8') as users_file:
users_file.write(first_name + " " + last_name + "\n")
self.finish()
class NickNameSubmitHandler(tornado.web.RequestHandler):
def get(self):
print("Stage 2: Nick Name Submitted, Loading Full Name")
# These variables hold the users input
nick_name = self.get_argument("nick")
print("User NickName:", nick_name)
self.finish()
DB.user_nicknames.append(nick_name)
class TermAgreeHandler(tornado.web.RequestHandler):
def get(self):
print("Stage 4: Agreed, Loading WhatssApp web scrapper")
# run whats up web scrapper
print("Stage 4: Agreed, Go to phone sort")
self.finish()
class ChoosePhoneHandler(tornado.web.RequestHandler):
def get(self):
print("Stage 5: Load Phone Sort")
phone = self.get_argument("phone")
DB.phone = phone
print('Phone is {}'.format(phone))
self.finish()
class LetsGoHandler(tornado.web.RequestHandler):
def get(self):
print("Stage 7: Lets GO!, Load whatssapp web!")
# Insert whats up web run here
scrape_whatsapp_and_analyze_db()
# if mainDriver is not None:
# mainDriver.getBrowser().get("localhost:8888/static/headset2.html")
# mainDriver.getBrowser().refresh()
import DataAnalysisTestDriver
DataAnalysisTestDriver.test_data_analysis(DB)
print("going out of handler letsGo")
self.finish()
"""
unity communication handlers
"""
class GiveLatestChatsHandler(tornado.web.RequestHandler):
def get(self):
print("GetLatestChatsHandler")
self.finish(DB.latest_chats)
class GiveClosestPersonsAndMsgs(tornado.web.RequestHandler):
def get(self):
print("GetClosestPersonsAndMsgs")
self.finish(DB.closest_persons_and_msg)
class HaveHebrew(tornado.web.RequestHandler):
def get(self):
print("HaveHebrew")
self.finish(DB.have_hebrew)
class GiveGoodNightMessages(tornado.web.RequestHandler):
def get(self):
print("GiveGoodNightMessages")
self.finish(DB.good_night_messages)
class GiveDreamsOrOldMessages(tornado.web.RequestHandler):
def get(self):
print("GiveDreamsOrOldMessages")
self.finish(DB.dreams_or_old_messages)
class GiveMostActiveGroupsAndUserGroups(tornado.web.RequestHandler):
def get(self):
print("GiveMostActiveGroupsAndUserGroups")
self.finish(DB.most_active_groups_and_user_groups)
class GiveChatArchive(tornado.web.RequestHandler):
def get(self):
print("GiveChatArchive")
self.finish(DB.chat_archive)
class ResetHandler(tornado.web.RequestHandler):
"""
Sends the user to the first page, resets the DB and removes all avatars
"""
def get(self):
print("ResetHandler")
InitializeDBAndAvatars()
try:
mainDriver.getBrowser().execute_async_script('''
setTimeout(function(){
window.location = '/static/reset.html';
}, 1000);
''')
except Exception as e:
print("shits")
# mainDriver.getBrowser().get("localhost:8888/static/reset")
# time.sleep(5)
# mainDriver.getBrowser().get("localhost:8888")
self.finish()
"""""
make_app, settings, main
"""""
# Allows access to static directory to any http request
settings = dict(static_path=os.path.join(os.path.dirname(__file__), "static"))
# Create tornado web application
def make_app():
# print("make_app")
return tornado.web.Application([(
# web page handlers
(r"/", LandingHandler)),
(r"/namesubmit", NameSubmitHandler),
(r"/agree", TermAgreeHandler),
(r"/nicknamesubmit", NickNameSubmitHandler),
(r"/choosephone", ChoosePhoneHandler),
(r"/letsgo", LetsGoHandler),
# unity handlers
(r"/get_latest_chats", GiveLatestChatsHandler),
(r"/get_closest_persons_and_msgs", GiveClosestPersonsAndMsgs),
(r"/have_hebrew", HaveHebrew),
(r"/get_good_night_messages", GiveGoodNightMessages),
(r"/get_dreams_or_old_messages", GiveDreamsOrOldMessages),
(r"/get_most_active_groups_and_user_groups", GiveMostActiveGroupsAndUserGroups),
(r"/get_chat_archive", GiveChatArchive),
(r"/reset", ResetHandler)
], **settings)
# Initialize and empty DB variable for future use
DB = None
# Initialize an empty driver for the user.
# We'll use this to send the user back to the main page when we reset the experience
driver_user = None
if __name__ == "__main__":
InitializeDBAndAvatars()
global mainDriver
port = 8888
app = make_app()
# enter webPage as the first argument to run the web page
if sys.argv[1] == 'WebPage':
print("web Page")
# A Chrome window to navigate to our site
mainDriver = Webdriver()
mainDriver.browser.get("localhost:" + str(port))
# save the data to a file for future work
elif sys.argv[1] == 'SaveData':
print("scrapping and saving data to pickle")
scrape_whatsapp_and_analyze_db()
DB.save_db_to_files(".\\stored data\\")
sys.exit()
# load the last stored data and analyze it
elif sys.argv[1] == 'LoadData':
print('loading the data')
DB.load_db_from_files(".\\stored data\\")
# Print data
import DataAnalysisTestDriver
DataAnalysisTestDriver.test_data_analysis(DB)
# Save data
DB.run_data_analysis_and_store_results()
# just runs the scrapping and analysis
else:
print("scrape_whatsapp")
scrape_whatsapp_and_analyze_db()
# Print data
import DataAnalysisTestDriver
DataAnalysisTestDriver.test_data_analysis(DB)
print("server keeps running for unity get requests")
print("listening")
app.listen(port)
# app.listen(port, address="192.168.173.1") # listen to ip
tornado.ioloop.IOLoop.current().start()