-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner.py
More file actions
400 lines (348 loc) · 11.7 KB
/
Copy pathrunner.py
File metadata and controls
400 lines (348 loc) · 11.7 KB
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
# Bradley Levine and Joshua Vasko
# Python for Developers: Milestone 3
# Purpose: To display top 20 keywords from maildir data into an
# html file with deadlinks for each keyword.
# Date Written: 9/24/2017
# Last Updated: 10/16/2017
# Python Version Used: 3.6.2
# Imports
from bow import BagOfWords as bw # Class to scrub text
import os # Traverse directory
import re # Pattern detection within files
import time # Used for progress bar
from tqdm import * # User needs to download seperately: pip install tqdm
from jinja2 import Template, Environment, FileSystemLoader # HTML Generation
import webbrowser as web # Open index automatically
import sqlite3 as sql # Database implementation
#import plotly # Report generation
#plotly.tools.set_credentials_file(username='jvasko', api_key='ljocCxHdgVsrmAqbXUOw')
import plotly.offline as offline # Report generation
import plotly.graph_objs as go # Report generation
# Main Function
def main():
##### Begin Menu Generation and User Choice #####
root = None
choices = next(os.walk("."))[1]
if "__pycache__" in choices:
choices.remove("__pycache__")
print("Please enter the number for which choice you would like to run.")
# Handle User Input
while True:
try:
print_menu(choices)
userin = input()
if not userin.isdigit(): # Makes sure input is an integer
raise ValueError
if not 1 <= int(userin) <= len(choices):
raise ValueError
except ValueError:
print('Please enter an integer within the menu.')
else:
userin = int(userin)
break
root = choices[userin - 1]
##### End Menu Generation and User Choice #####
##### Begin Import Email Files #####
emails = []
users = set()
print("Finding files")
for root, dirs, files in os.walk(root, topdown=False):
for file in files:
emails.append(os.path.join(root, file))
print("Found emails")
##### End Import Email Files #####
##### Begin Create Database Connection ######
try:
conn = sql.connect("keywords.db")
cursor = conn.cursor()
except sql.Error as e:
print(e)
else:
print("Connected to Database Successfully")
create_db(cursor)
##### End Create Database Connection End #####
##### Begin Retrieve Frequencies and Email Scrubbing #####
print("This may take a few moments...")
fields = ["From", "To", "Date"]
r = bw(noise="noise.txt", compound="compound.txt", map="substitution.txt")
for file in tqdm(range(len(emails))):# Progress Bar
try:
m = re.search("\\.[\\w]+$", emails[file])
if m == None:
current = open(emails[file], "r")
text = current.readlines()
user = get_email_owner(text)
### insert into user to email ###
cursor.execute("""INSERT INTO user_to_email (user_name, file_path) VALUES ('""" + user + """','""" + emails[file] + """');""")
### end ###
text, header = extract_header(text)
fill(header, fields)
norm_text, keywords = r.run(text)
### insert into emails ###
cursor.execute("""INSERT INTO emails (file_path, data, clean_data, from_, to_, date_) VALUES ('""" + emails[file] + """', '"""+ text.replace("'", "") +"""', '""" + norm_text + """', '""" + header["from"].replace("'", "") + """', '""" + header["to"].replace("'", "") + """', '""" + header["date"].replace("'", "") + """');""")
for k in keywords:
cursor.execute("""INSERT INTO keyword_to_email (file_path, keyword, frequency) VALUES ('""" + emails[file] + """', '""" + k + """', """ + str(keywords[k]) +""")""")
### end ###
if file % 150 == 0:
conn.commit()
except sql.Error as e:
print(e)
conn.rollback()
### Get the Top 20 keywords ###
q = cursor.execute("""SELECT keyword, SUM(frequency)
FROM keyword_to_email
GROUP BY keyword
ORDER BY SUM(frequency) DESC, keyword ASC
LIMIT 50;""")
top20 = dict()
for row in q:
top20.update({row[0]:row[1]})
for col in row:
print(str(col) + " ", end="")
print("\n")
### end ###
### Get list of found users ###
q = cursor.execute("""SELECT user_name, COUNT(*)
FROM user_to_email
GROUP BY user_name
ORDER BY user_name;""")
users = dict()
for row in q:
users.update({row[0]:row[1]})
print(row[0])
### end ###
### Generate HTML ###
index_html(top20, users)
for k in top20:
e = cursor.execute("""SELECT e.file_path, e.from_, e.to_, e.date_, e.data
FROM emails AS e
WHERE e.file_path IN (SELECT file_path
FROM keyword_to_email
WHERE '"""+ k +"""' = keyword)
ORDER BY e.file_path
;""")
em = {}
emd = {}
j = 1
for row in e:
i = 1
em.update({row[0]:dict()})
for f in fields:
em[row[0]].update({f:row[i]})
i+=1
emd.update({row[0]:{"data":row[4], "p":j}})
j+=1
keyword_html(top20, users, em, emd, k, fields)
for u in users:
e = cursor.execute("""SELECT e.file_path, e.from_, e.to_, e.date_, e.data
FROM emails AS e
WHERE e.file_path IN (SELECT file_path
FROM user_to_email
WHERE '"""+ u +"""' = user_name)
ORDER BY e.file_path
;""")
em = {}
emd = {}
j = 1
for row in e:
i = 1
em.update({row[0]:dict()})
for f in fields:
em[row[0]].update({f:row[i]})
i+=1
emd.update({row[0]:{"data":row[4], "p":j}})
j+=1
authors_html(top20, users, em, emd, u, fields)
### end ###
# Get Month and Date Frequencies
date = dict()
month = dict()
q = cursor.execute("""SELECT date_, COUNT(*)
FROM emails
GROUP BY date_
ORDER BY COUNT(*);""")
for row in q:
try:
rl = row[0].split(" ")
if rl[1].isdigit():
rk = rl[1] + rl[2][:3] + rl[3]
mk = rl[2][:3]
else:
rk = rl[2] + rl[1][:3] + rl[3]
mk = rl[1][:3]
rk = rk.replace(",", "")
if rk in date:
date[rk] += 1
else:
date.update({rk: 1})
if mk in month:
month[mk] += 1
else:
month.update({mk: 1})
except Exception as e:
pass
##### End Retrieve Frequencies and Email Scrubbing #####
generate_reports(users, top20, date, month)
conn.close()
web.open("index.html")
# create_db
# Purpose: Creates database to hold information extracted from emails. "keywords.db"
# Date Written: 10/12/17
# Date Updated: 10/15/17
def create_db(c):
sql_create_tables = """
DROP TABLE IF EXISTS keyword_to_email;
DROP TABLE IF EXISTS user_to_email;
DROP TABLE IF EXISTS emails;
CREATE TABLE
IF NOT EXISTS emails(
file_path text NOT NULL,
data text,
clean_data text,
from_ text,
to_ text,
date_ text,
PRIMARY KEY(file_path)
);
CREATE TABLE
IF NOT EXISTS keyword_to_email(
file_path text NOT NULL,
keyword text NOT NULL,
frequency integer,
FOREIGN KEY(file_path) REFERENCES emails (file_path),
PRIMARY KEY(file_path, keyword)
);
CREATE TABLE
IF NOT EXISTS user_to_email(
user_name NOT NULL,
file_path NOT NULL,
FOREIGN KEY(file_path) REFERENCES emails (file_path),
PRIMARY KEY(user_name, file_path)
);
"""
try:
c.executescript(sql_create_tables)
except sql.Error as e:
print(e)
else:
print("Created Database")
# index_html
# Purpose: Creations of index html page with keywords.
# Date Written: 9/24/2017
# Date Updated: 10/08/2017
def index_html(data, auths):
j2_env = Environment(loader=FileSystemLoader(os.path.dirname(os.path.abspath(__file__))), trim_blocks=True)
t = j2_env.get_template("index_template.html")
html = open("index.html", "w+")
html.write(t.render(keywords=data, authors=auths))
html.close()
# keyword_html
# Purpose: Creates individual html pages for each keyword.
# Date Written: 10/08/17
# Date Updated: 10/12/17
def keyword_html(data, auths, emails , emd, keyword, fields):
j2_env = Environment(loader=FileSystemLoader(os.path.dirname(os.path.abspath(__file__))), trim_blocks=True)
t = j2_env.get_template("keyword_template.html")
html = open(keyword + ".html", "w+")
html.write(t.render(keywords=data, authors=auths, emails=emails, ed=emd, keyword=keyword, fields=fields))
html.close()
# authors_html
# Purpose: Creates individual html pages for each author.
# Date Written: 10/08/17
# Date Updated: 10/12/2017
def authors_html(data, auths, emails, emd, author, fields):
j2_env = Environment(loader=FileSystemLoader(os.path.dirname(os.path.abspath(__file__))), trim_blocks=True)
t = j2_env.get_template("author_template.html")
html = open(author + ".html", "w+")
html.write(t.render(keywords=data, authors=auths, emails=emails, ed=emd, author=author, fields=fields))
html.close()
# extract_header
# Purpose: Removes header from each file so they do not get counted.
# Date Written: 9/24/2017
# Date Updated: 9/25/2017
def extract_header(data):
tracker = dict()
r = ""
for line in data:
if not (line.startswith("Message-ID:") or line.startswith("Date:") or line.startswith("X-From:") or line.startswith("X-To:") or line.startswith("X-cc:") or \
line.startswith("X-bcc:") or line.startswith("X-Folder:") or line.startswith("X-Origin:") or line.startswith("X-FileName:") or \
line.startswith("Subject:") or line.startswith("Date:") or line.startswith("Mime-Version:") or line.startswith("Content-Type:") or \
line.startswith("Content-Transfer-Encoding:") or line.startswith("From:") or line.startswith("To:")):
r += line
else:
t = line.split(":", 1)
if len(t) == 2 and t[0].strip() not in tracker:
tracker.update({t[0].strip().lower():t[1].strip().lower()})
return r, tracker
# get_email_owner
# Purpose: Retrieves whom the email document belongs to.
# Date Written: 10/08/17
# Date Updated: 10/08/17
def get_email_owner(data):
for line in data:
if line.startswith("X-Origin:"):
return line.replace("X-Origin: ", "").strip().lower()
return "None"
# print_menu
# Purpose: Generates menu for user to select root.
# Date Written: 10/12/17
# Date Updated: 10/13/17
def print_menu(l):
j = 1
for i in l:
print("\t" + str(j) + ". " + i)
j+=1
# fill
# Purpose: Handles empty header field to avoide KeyValue Errors.
# Date Written: 10/12/17
# Date Updated: 10/13/17
def fill(header, fields):
for field in fields:
if field.lower() not in header:
header.update({field.lower():"None"})
# generate_reports
# Purpose: Generates reports for user consumption.
# Date Written: 10/8/17
# Date Written: 10/15/17
def generate_reports(authors, keywords, date, month):
# Author
x1 = []
y1 = []
for author in authors:
x1.append(author)
y1.append(authors[author])
AutBar = [go.Bar(x = x1, y = y1)]
AutPie = [go.Pie(labels = x1, values = y1)]
offline.plot(AutBar, filename = "AuthorFreqBar.html")
offline.plot(AutPie, filename = "AuthorPie.html")
# Keyword
x2 = []
y2 = []
for key, value in keywords.items():
x2.append(key)
y2.append(int(value))
KeyBar = [go.Bar(x = x2, y = y2)]
KeyPie = [go.Pie(labels = x2, values = y2)]
offline.plot(KeyBar, filename = "KeyFreqBar.html")
offline.plot(KeyPie, filename = "KeyPie.html")
# Days
x3 = []
y3 = []
for key, value in date.items():
x3.append(key)
y3.append(int(value))
DateBar = [go.Bar(x = x3, y = y3)]
offline.plot(DateBar, filename = "DateFreqBar.html")
# Months
x4 = []
y4 = []
for key, value in month.items():
x4.append(key)
y4.append(int(value))
MonthBar = [go.Bar(x = x4, y = y4)]
MonthPie = [go.Pie(labels = x4, values = y4)]
offline.plot(MonthBar, filename = "MonthFreqBar.html")
offline.plot(MonthPie, filename = "MonthPie.html")
# Main Call
if __name__ == "__main__":
main()