Insert emails into database

master
Sven Slootweg 12 years ago
parent 1aa988d9d6
commit c3fc1730c1

16
parse

@ -13,11 +13,12 @@ parser.add_argument('-r', '--render', dest='render', action='store_true',
args = parser.parse_args() args = parser.parse_args()
options = vars(args) options = vars(args)
database = sqlite3.connect('emails.db').cursor() database = sqlite3.connect('emails.db')
cursor = database.cursor()
try: try:
# Try to create the table # Try to create the table
database.execute("CREATE TABLE emails (`message_id`, `from`, `to`, `subject`, `date`, `body`, `html`, `hash`)") cursor.execute("CREATE TABLE emails (`message_id`, `from`, `to`, `subject`, `date`, `body`, `html`, `hash`)")
except sqlite3.OperationalError: except sqlite3.OperationalError:
# Table already exists # Table already exists
pass pass
@ -36,5 +37,14 @@ for email_file in file_list:
subject = message['subject'] subject = message['subject']
sha1_hash = hashlib.sha1("%s/%s/%s/%s" % (message['from'], message['to'], message['message-id'], subject)).hexdigest() sha1_hash = hashlib.sha1("%s/%s/%s/%s" % (message['from'], message['to'], message['message-id'], subject)).hexdigest()
timestamp = 0
textbody = ""
htmlbody = ""
new_row = (message['message-id'], message['from'], message['to'], subject, timestamp, textbody, htmlbody, sha1_hash)
cursor.execute("INSERT INTO emails VALUES (?, ?, ?, ?, ?, ?, ?, ?)", new_row)
print "Successfully parsed and inserted e-mail with SHA1 hash %s." % sha1_hash
database.commit()
print "Changes successfully committed to database, exiting..."

Loading…
Cancel
Save