This commit is contained in:
2020-11-12 14:21:19 +01:00
parent 0aa7c05313
commit 3e42ca0bcd
7 changed files with 130 additions and 27 deletions

View File

@@ -13,10 +13,14 @@ from pprint import pprint
from dateutil import parser
MAILDIR = "/home/yannik/mail/INBOX/"
OUTFILE = "ubemails.txt"
OUTFILE = "data/ubemails.txt"
def decode_header(header):
return "".join([ x[0].decode(x[1] or "ascii") if isinstance(x[0], bytes) else x[0] for x in email.header.decode_header(header) ])
decoded_header = email.header.decode_header(header)[0]
encoding = decoded_header[1] or "ascii"
if encoding == "unknown-8bit":
encoding = "ascii"
return decoded_header[0].decode(encoding, errors="replace") if isinstance(decoded_header[0], bytes) else decoded_header[0]
if __name__ == "__main__":
mbox = mailbox.Maildir(MAILDIR)
@@ -35,7 +39,12 @@ if __name__ == "__main__":
latest = message
latest_date = date
if not latest:
print("ERROR: No Mail found", file=sys.stderr)
sys.exit(1)
if latest_date.date() != datetime.date.today():
print("WARNING: Mail is not from today", file=os.stderr)
print("WARNING: Mail is not from today", file=sys.stderr)
payload = latest.get_payload(decode=True).decode("utf8").strip()
print(payload.rpartition("\n--")[0].strip() if "\n--" in payload else payload)
result = (payload.rpartition("\n--")[0].strip() if "\n--" in payload else payload)
with open(OUTFILE, "w") as f:
f.write(result+"\n")