/
app.py
474 lines (337 loc) · 11 KB
/
app.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
from flask import Flask, Markup
from flask import render_template
from flask import request, redirect
import xmpp
'''
Start script
. venv/bin/activate
python -m flask run
bandi made this comment
KIA
MIA
WIA
'''
class Message:
def __init__(self, timeStamp, name, msg):
self.timeStamp = timeStamp
self.name = name
self.msg = msg
timestampBool = False
nameBool = False
msgBool = False
timeStampInfo = ""
nameInfo = ""
msgInfo = ""
container = []
# for line in FileContent:
# #
# if "[" in line: #This next part will be a time stamp...
# #record the next segment of data
with open("./data/data.txt") as fileobj:
for line in fileobj:
for char in line:
# print(char)
if char == "]":
timestampBool = False
elif char == "<":
nameBool = True
elif char == ">":
nameBool = False
msgBool = True
elif char == "[":
timestampBool = True
if msgBool == True:
container.append(Message(timeStampInfo, nameInfo, msgInfo))
#re initialize temp vars
timeStampInfo = ""
nameInfo = ""
msgInfo = ""
msgBool = False
else:
if timestampBool == True:
timeStampInfo += char
# print(timeStampInfo)
if nameBool == True:
nameInfo += char
# print(nameInfo)
if msgBool == True:
msgInfo += char
# print(msgInfo)
container.append(Message(timeStampInfo, nameInfo, msgInfo)) #after exiting the loop, the temp variables are still stored so we can append the final message...
def showSitreps(): #function that prints all sitreps of the given file
ret = []
for i in range(len(container)):
temp = container[i]
if temp.name == "115 FAB BTL NCO":
if "SITREP" in temp.msg:
ret.append(temp)
return ret
def recentSitrep(): #function that prints all sitreps of the given file
for i in range(len(container)):
temp = container[i]
if temp.name == "115 FAB BTL NCO":
if "SITREP" in temp.msg:
mostRecent = temp
return mostRecent
def allSaluteReport():
ret = []
for i in range(len(container)):
temp = container[i]
if temp.name == "115 FAB BTL NCO":
ret.append(temp)
return ret
def recentSalute():
for i in range(len(container)):
temp = container[i]
if temp.name == "115 FAB BTL NCO":
if "SALUTE REPORT" in temp.msg:
mostRecent = temp
return mostRecent
def authors():
seen = {}
for i in range(len(container)):
temp = container[i]
if temp.name in seen:
seen[temp.name] += 1
else:
seen[temp.name] = 0
return len(seen)
def ammoStat(): #displays most recent ammo status
for i in range(len(container)):
temp = container[i]
if temp.name == "115 FAB BTL NCO":
if "SITREP" in temp.msg:
if "Ammo Status: Green" in temp.msg:
ammo = "Green"
elif "Ammo Status: Red" in temp.msg:
ammo = "Red"
elif "Ammo Status: Amber" in temp.msg:
ammo = "Amber"
elif "Ammo Status: Black" in temp.msg:
ammo = "Black"
elif "Ammo Status: N.A" in temp.msg:
ammo = "Not Available"
return ammo
def fuelStat(): #displays most recent fuel status
for i in range(len(container)):
temp = container[i]
if temp.name == "115 FAB BTL NCO":
if "SITREP" in temp.msg:
if "Fuel Status: Green" in temp.msg:
fuel = "Green"
elif "Fuel Status: Amber" in temp.msg:
fuel = "Amber"
elif "Fuel Status: Black" in temp.msg:
fuel = "Black"
elif "Fuel Status: Red" in temp.msg:
fuel = "Red"
elif "Fuel Status: N.A" in temp.msg:
fuel = "Not Available"
return fuel
def staffStat(): #displays most recent personnel status
for i in range(len(container)):
temp = container[i]
if temp.name == "115 FAB BTL NCO":
if "SITREP" in temp.msg:
if "Personnel Status: Red" in temp.msg:
personnel = "Red"
elif "Personnel Status: Green" in temp.msg:
personnel = "Green"
elif "Personnel Status: Amber" in temp.msg:
personnel = "Amber"
elif "Personnel Status: Black" in temp.msg:
personnel = "Black"
elif "Personnel Status: N.A" in temp.msg:
personnel = "Not Available"
return personnel
def getImportantNumbers():
'''
KIA
MIA
WIA
'''
kiaMsg, miaMsg, wiaMsg, size = [], [], [], len(container)
for i in range(size):
msg = container[i].msg
if 'kia' in msg or 'KIA' in msg:
kiaMsg.append(msg.upper())
elif 'mia' in msg or 'MIA' in msg:
miaMsg.append(msg.upper())
elif 'wia' in msg or 'WIA' in msg:
wiaMsg.append(msg.upper())
else:
pass
m1, k1, w1 = parseMsgScraper(kiaMsg)
m2, k2, w2 = parseMsgScraper(wiaMsg)
# print(miaMsgFinal)
# print(wiaMsgFinal)
#kiaInt, miaInt, wiaInt = parseIntMsgScrapper(kiaMsgFinal), parseIntMsgScrapper(miaMsgFinal), parseIntMsgScrapper(wiaMsgFinal)
kiaIntArr1 = parseIntMsgScrapper(k1)
k1Int = sum(kiaIntArr1)
kiaIntArr2 = parseIntMsgScrapper(k2)
k2Int = sum(kiaIntArr2)
miaIntArr1 = parseIntMsgScrapper(m1)
m1Int = sum(miaIntArr1)
miaIntArr2 = parseIntMsgScrapper(m2)
m2Int = sum(miaIntArr1)
wiaIntArr1 = parseIntMsgScrapper(w1)
w1Int = sum(wiaIntArr1)
wiaIntArr2 = parseIntMsgScrapper(w2)
w2Int = sum(wiaIntArr1)
total_KIA = k1Int + k2Int
total_WIA = w1Int + w2Int
total_MIA = m1Int + m2Int
# print("total kia: " + str(total_KIA) + " total wia: " + str(total_WIA) + " total mia:" + str(total_MIA))
return total_KIA, total_WIA, total_MIA
def parseMsgScraper(messages):
# init
size= len(messages)
miaRet, kiaRet, wiaRet = [], [], []
for i in range(size):
msg = messages[i]
for j in range(len(msg)):
msgChar = msg[j]
if msgChar == 'M':
if 'MIA' in msg[j:j+4]:
miaRet.append(msg[j:j+10])
elif msgChar == 'K':
if 'KIA' in msg[j:j+4]:
kiaRet.append(msg[j:j+10])
elif msgChar == 'W':
if 'WIA' in msg[j:j+4]:
wiaRet.append(msg[j:j+10])
else:
pass
return miaRet, kiaRet, wiaRet
def parseIntMsgScrapper(messages):
total = []
for message in messages:
for msg in message.split():
if msg.isdigit():
total.append(int(msg))
return total
def userSearch(user): # outputs all messages from an inputted user
ret = []
for i in range(len(container)):
temp = container[i]
if temp.name == user:
ret.append(temp)
return ret # returns all the messages containing the user in the .name.
# for i in range(len(sitreps)):
# temp = sitreps[i]
# print("\n\nSITREP report at: ", temp.timeStamp)
# print("Content of SITREP: ", temp.msg)
def getUserInfo():
seen = {}
for i in range(len(container)):
temp = container[i]
if temp.name in seen:
seen[temp.name] = seen[temp.name] + 1
else:
seen[temp.name] = 1
return seen
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('./index.html')
'''
@app.route("/upload-txt", methods=["GET", "POST"])
def upload_image():
if request.method == "POST":
if request.files:
image = request.files["text"]
print(image)
return redirect(request.url)
return render_template("./index.html")
'''
@app.route("/processing", methods=["GET", "POST"])
def processing():
if request.method == "GET":
return render_template("./processing.html")
return render_template("./index.html")
@app.route("/final", methods=["GET", "POST"])
def final():
total = len(container)
total_KIA, total_WIA, total_MIA = getImportantNumbers()
first = container[0].timeStamp.split()
last = container[total - 1].timeStamp.split()
first = first[0]
last = last[0]
print(first)
print(last)
authorTotal = authors()
mostRecent = recentSitrep()
salute = recentSalute()
personnel = staffStat()
ammo = ammoStat()
fuel = fuelStat()
labels = ["KIA", "WIA", "MIA",""]
values = []
values.append(total_KIA)
values.append(total_WIA)
values.append(total_MIA+1)
return render_template("./final.html", firstDate = first, lastDate = last,KIA = total_KIA, MIA = total_MIA, WIA = total_WIA, totalMessages=total, totalAuthors=authorTotal, sitRep = mostRecent, saluteReport = salute, ammoStatus = ammo, fuelStatus = fuel, personnelStatus = personnel, labels=labels,values=values)
@app.route("/allSitreps", methods=["GET", "POST"])
def allSitreps():
sitreps = showSitreps()
return render_template("./allSitreps.html", finalSitreps=sitreps)
@app.route("/allReports", methods=["GET", "POST"])
def reports():
allReports = allSaluteReport()
for report in allReports:
if "ACK" in report.msg:
allReports.remove(report)
return render_template("./allReports.html", reports=allReports)
@app.route("/allMessages", methods=["GET", "POST"])
def allMessages():
return render_template("./allMessages.html", messages=container)
@app.route("/searchUser", methods=["GET", "POST"])
def searchUser():
if request.method == "POST":
user = request.form['user']
userList = userSearch(user)
if userList == []:
userList == ""
return render_template("./searchUser.html", userList=userList)
@app.route("/userInfo", methods=["GET", "POST"])
def userInfo():
seen = getUserInfo()
return render_template("./userInfo.html", people = seen)
# updates carousel with messages
@app.route("/carousel", methods = ["GET", "POST"])
def carousel():
total = len(container)
return render_template("./carousel.html", totalMessages = total, messages = container )
# Test page for chart
@app.route("/chart", methods = ["GET", "POST"])
def chart():
test = 0
legend = 'Monthly Data'
labels = ["January", "February", "March", "April", "May", "June", "July", "August"]
values = [10, 9, 8, 7, 6, 4, 7, 8]
return render_template("./chart.html", values=values, labels=labels, legend=legend)
# Tactical Balitical Missle Messages
@app.route("/tactical", methods = ["GET", "POST"])
def tacticalBM():
tacticalB = tbm()
return render_template("./tbm.html", tacticalBM=tacticalB, i = 0)
def tbm():
ret = []
for i in range(len(container)):
temp = container[i]
if 'TBM' in temp.msg:
ret.append(temp)
return ret
def exampleXmpp():
# in this example we create an xmpp client that can send and recieve messages connectiong to a gtalk server
username = 'username'
passwd = 'password'
to='name@example.com'
msg='hello :)'
client = xmpp.Client('gmail.com')
client.connect(server=('talk.google.com',5223))
client.auth(username, passwd, 'botty')
client.sendInitPresence()
message = xmpp.Message(to, msg)
message.setAttr('type', 'chat')
client.send(message)