2024-04-10 15:17:04 +02:00
|
|
|
from markdownify import markdownify
|
|
|
|
import imaplib
|
|
|
|
import email
|
|
|
|
from email.header import decode_header
|
|
|
|
import os
|
2024-04-12 11:44:05 +02:00
|
|
|
import re
|
2024-04-10 15:17:04 +02:00
|
|
|
|
|
|
|
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])
|
|
|
|
|
2024-04-12 11:44:05 +02:00
|
|
|
for i in range(messages, max(0, messages-N), -1):
|
2024-04-10 15:17:04 +02:00
|
|
|
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)
|
|
|
|
|
2024-04-11 15:27:04 +02:00
|
|
|
print(str(i) + ' - ' + 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:
|
2024-04-10 21:44:15 +02:00
|
|
|
open(filepath, "wb").write(body.replace('\r\n', '\n').encode('utf8'))
|
2024-04-10 15:17:04 +02:00
|
|
|
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 21:44:15 +02:00
|
|
|
open(filepath, "wb").write(body.replace('\r\n', '\n').encode('utf8'))
|
2024-04-10 15:17:04 +02:00
|
|
|
|
|
|
|
if content_type == "text/html":
|
2024-05-14 11:47:36 +02:00
|
|
|
print('>> html')
|
2024-04-10 15:17:04 +02:00
|
|
|
md = markdownify(body)
|
|
|
|
if md[:4] == 'html':
|
|
|
|
md = md[4:]
|
2024-04-12 11:44:05 +02:00
|
|
|
# fastmail specific: replace internal links
|
|
|
|
md = re.sub(r'\[(.*)\]\(https://app.fastmail.com.*\)', '[[\\1]]', md)
|
2024-04-10 21:44:15 +02:00
|
|
|
open(filepath, "wb").write(md.replace('\r\n', '\n').encode('utf8'))
|
2024-04-10 15:17:04 +02:00
|
|
|
|
|
|
|
imap.close()
|
|
|
|
imap.logout()
|