From 7614da6b9256c3fd66e9bb8022f48b0b6590aac0 Mon Sep 17 00:00:00 2001 From: Giulio Date: Mon, 21 May 2018 16:42:21 +0200 Subject: [PATCH] Added last_fetch to avoid refetching older messages --- app.py | 15 +++++++++++---- schema.sql | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 01ebb3e..69f7b8c 100644 --- a/app.py +++ b/app.py @@ -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() diff --git a/schema.sql b/schema.sql index ac30c09..0f9df89 100644 --- a/schema.sql +++ b/schema.sql @@ -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 );