Browse Source

First import

Giulio 5 years ago
commit
6677a10475
3 changed files with 64 additions and 0 deletions
  1. 60 0
      app.py
  2. 1 0
      attachments/.gitkeep
  3. 3 0
      schema.sql

+ 60 - 0
app.py

@@ -0,0 +1,60 @@
+import imaplib
+import email
+import email.header
+import sqlite3
+import hashlib
+import time
+
+conn = sqlite3.connect("mailboxes.db")
+conn.row_factory = sqlite3.Row    
+sql = conn.cursor()
+
+def connect(server, username, password, folder):
+    c = imaplib.IMAP4_SSL(server)
+    c.login(username, password)
+    c.select(folder, readonly=True)
+    return c
+
+def mailbox(c, id):
+
+    r, data = c.search(None, "ALL")
+    for num in data[0].split():
+        r, data = c.fetch(num, '(RFC822)')
+        msg = email.message_from_string(data[0][1])
+
+        subject = str(email.header.decode_header(msg['Subject'])[0][0])
+        mailid = hashlib.sha1(email.header.decode_header(msg['Message-ID'])[0][0]).hexdigest()
+        sender = email.header.decode_header(msg['From'])[0][0]
+
+    	print(subject)
+
+
+    	sql.execute("SELECT * FROM mails WHERE mailid = ?", (mailid, ))
+    	if sql.fetchone() != None:
+    		continue
+
+    	sql.execute("INSERT INTO mails (mailid, `date`, `from`, `to`, subject, body, account_id) VALUES (?, ?, ?, ?, ?, ?, ?)", (mailid, 0, sender.decode('utf-8', 'ignore'), 'idk', subject.decode('utf-8', 'ignore'), data[0][1].decode('utf-8', 'ignore'), id))
+
+        conn.commit()
+
+        # Save attachments
+        if msg.get_content_maintype() == 'multipart':
+            for part in msg.walk():
+                if part.get_content_maintype() != 'multipart' and part.get('Content-Disposition') is not None and part.get_filename() is not None:
+                    filename = str(time.time()) + "_" + part.get_filename()
+                    open('attachments/' + filename, 'wb').write(part.get_payload(decode=True))
+                    sql.execute("UPDATE mails SET has_attachments = 1 WHERE mailid = ?", (mailid, ))
+                    sql.execute("INSERT INTO attachments (mailid, filename) VALUES (?, ?)", (mailid, filename, ))
+
+
+
+
+def main():
+    sql.execute("SELECT * FROM accounts")
+    accounts = sql.fetchall()
+    for account in accounts:
+        mailbox(connect(account["server"], account["username"], account["password"], account["folder"]), account["id"])
+
+main()
+
+conn.commit()

+ 1 - 0
attachments/.gitkeep

@@ -0,0 +1 @@
+

+ 3 - 0
schema.sql

@@ -0,0 +1,3 @@
+CREATE TABLE "accounts" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `server` TEXT, `username` TEXT, `password` TEXT, `folder` TEXT, `last_fetch` INTEGER );
+CREATE TABLE "attachments" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `mailid` INTEGER, `filename` TEXT );
+CREATE TABLE "mails" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `mailid` TEXT, `date` INTEGER, `from` TEXT, `to` TEXT, `subject` TEXT, `body` BLOB, `has_attachments` INTEGER, `account_id` INTEGER );