notetools/imap2md/export-imap-notes.py

66 lines
2.1 KiB
Python
Raw Normal View History

2024-04-10 15:17:04 +02:00
from markdownify import markdownify
import imaplib
import email
from email.header import decode_header
import os
username = ""
password = ""
imapserver = ""
2024-04-10 17:26:01 +02:00
N = 500
2024-04-10 15:17:04 +02:00
if not os.path.isdir('notes'):
os.mkdir('notes')
imap = imaplib.IMAP4_SSL(imapserver)
imap.login(username, password)
2024-04-10 17:13:55 +02:00
status, messages = imap.select("Notes", True)
2024-04-10 15:17:04 +02:00
messages = int(messages[0])
for i in range(messages, messages-N, -1):
res, msg = imap.fetch(str(i), "(RFC822)")
for response in msg:
if isinstance(response, tuple):
msg = email.message_from_bytes(response[1])
subject, encoding = decode_header(msg["Subject"])[0]
if isinstance(subject, bytes):
subject = subject.decode(encoding)
From, encoding = decode_header(msg.get("From"))[0]
if isinstance(From, bytes):
From = From.decode(encoding)
print(subject)
2024-04-10 17:51:44 +02:00
filepath = subject + '.md'
2024-04-10 15:17:04 +02:00
filepath = os.path.join('notes', filepath)
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
content_disposition = str(part.get("Content-Disposition"))
try:
body = part.get_payload(decode=True).decode()
except:
pass
if content_type == "text/plain" and "attachment" not in content_disposition:
open(filepath, "w").write(body)
elif "attachment" in content_disposition:
print('/!\\ attachment')
else:
content_type = msg.get_content_type()
body = msg.get_payload(decode=True).decode()
if content_type == "text/plain":
2024-04-10 17:38:31 +02:00
open(filepath, "wb").write(body.encode('utf8'))
2024-04-10 15:17:04 +02:00
if content_type == "text/html":
md = markdownify(body)
if md[:4] == 'html':
md = md[4:]
open(filepath, "wb").write(md.encode('utf8'))
imap.close()
imap.logout()