diff options
| author | 2012-12-07 04:51:45 +0100 | |
|---|---|---|
| committer | 2012-12-07 04:54:52 +0100 | |
| commit | b4869647e6f16fd85efc3b178043f01ff2496136 (patch) | |
| tree | ceef270c3e9332eeb4fd41ef8b49b11fbf93671b | |
| parent | Append .gmail to message id. (diff) | |
| download | gmail-notmuch-b4869647e6f16fd85efc3b178043f01ff2496136.tar.xz gmail-notmuch-b4869647e6f16fd85efc3b178043f01ff2496136.zip | |
Only download messages if we have to.
| -rwxr-xr-x | gmail-notmuch.py | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/gmail-notmuch.py b/gmail-notmuch.py index a228e6c..ae05a5e 100755 --- a/gmail-notmuch.py +++ b/gmail-notmuch.py @@ -53,7 +53,7 @@ def main(): imap = login(options) print("Discovering local messages...") - old_messages = [os.path.basename(filename[0:filename.rfind(".gmail")]) for filename in os.listdir(destination_dir + "/cur") if filename.contains(".gmail")] + old_messages = [os.path.basename(filename[0:filename.rfind(".gmail")]) for filename in os.listdir(destination_dir + "/cur") if ".gmail" in filename] new_messages = discover_new_messages(imap, old_messages) if len(new_messages) == 0: @@ -76,7 +76,7 @@ def login(options): return imap def discover_new_messages(imap, old_messages): - print("Reading message list...") + print("Receiving message list...") typ, data = imap.fetch("1:*", "X-GM-MSGID") new_messages = [] if typ != "OK": @@ -97,15 +97,17 @@ def download_new_messages(imap, messages, destination): progressbar.start() for gmail_id, imap_seq in messages: - typ, data = imap.fetch(str(imap_seq), "RFC822") - if typ != "OK": - sys.exit("Failed to download message gmail-%d/imap-%d" % (gmail_id, imap_seq)) - temp = destination + "/tmp/" + str(gmail_id) + ".gmail" dest = destination + "/new/" + str(gmail_id) + ".gmail" - f = open(temp, "w") - f.write(data[0][1]) - f.close() + if not os.path.exists(dest): + typ, data = imap.fetch(str(imap_seq), "RFC822") + if typ != "OK": + sys.exit("Failed to download message gmail-%d/imap-%d" % (gmail_id, imap_seq)) + f = open(temp, "w") + f.write(data[0][1]) + f.close() + os.link(temp, dest) # Because DJB says so... + os.unlink(temp) typ, data = imap.fetch(str(imap_seq), "(FLAGS X-GM-LABELS)") if typ != "OK": @@ -114,9 +116,6 @@ def download_new_messages(imap, messages, destination): labels = label_parser.search(data[0]).groups() labels = filter_labels(shlex.split(labels[0], False, True) + labels[1].split(" ")) - os.link(temp, dest) # Because DJB says so... - os.unlink(temp) - database.begin_atomic() message = database.add_message(dest, True)[0] message.freeze() |
