Added last_fetch to avoid refetching older messages

This commit is contained in:
Giulio 2018-05-21 16:42:21 +02:00
parent 0b589bca09
commit 7614da6b92
2 changed files with 12 additions and 5 deletions

15
app.py
View File

@ -4,6 +4,7 @@ import email.header
import sqlite3
import hashlib
import time
import datetime
conn = sqlite3.connect("mailboxes.db")
conn.row_factory = sqlite3.Row
@ -15,9 +16,15 @@ def connect(server, username, password, folder):
c.select(folder, readonly=True)
return c
def mailbox(c, id):
def mailbox(c, id, last_fetch):
today = datetime.datetime.today().strftime("%d-%b-%Y")
r, data = c.search(None, "ALL")
if last_fetch:
search_string = "(SINCE " + last_fetch + ")"
else:
search_string = "ALL"
r, data = c.search(None, search_string)
for num in data[0].split():
r, data = c.fetch(num, '(RFC822)')
msg = email.message_from_string(data[0][1])
@ -28,7 +35,6 @@ def mailbox(c, id):
print(subject)
sql.execute("SELECT * FROM mails WHERE mailid = ?", (mailid, ))
if sql.fetchone() != None:
continue
@ -46,6 +52,7 @@ def mailbox(c, id):
sql.execute("UPDATE mails SET has_attachments = 1 WHERE mailid = ?", (mailid, ))
sql.execute("INSERT INTO attachments (mailid, filename) VALUES (?, ?)", (mailid, filename, ))
sql.execute("UPDATE accounts SET last_fetch = ? WHERE id = ?", (today, id,))
@ -53,7 +60,7 @@ 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"])
mailbox(connect(account["server"], account["username"], account["password"], account["folder"]), account["id"], account["last_fetch"])
main()

View File

@ -1,3 +1,3 @@
CREATE TABLE "accounts" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `server` TEXT, `username` TEXT, `password` TEXT, `folder` TEXT, `last_fetch` INTEGER );
CREATE TABLE "accounts" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `server` TEXT, `username` TEXT, `password` TEXT, `folder` TEXT, `last_fetch` TEXT );
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 );