56 lines
1.6 KiB
Python
Executable File
56 lines
1.6 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import mailbox
|
|
import jinja2
|
|
import email.header
|
|
import email.utils
|
|
import yaml
|
|
import datetime
|
|
import sys, os
|
|
import pypandoc
|
|
import argparse
|
|
import quopri
|
|
import pytz
|
|
from pprint import pprint
|
|
from dateutil import parser
|
|
|
|
import generate
|
|
|
|
CONFIG_FILE = "generator.conf"
|
|
|
|
def 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__":
|
|
aparser = argparse.ArgumentParser()
|
|
aparser.add_argument("--config", "-c", default=CONFIG_FILE)
|
|
args = aparser.parse_args()
|
|
|
|
config = generate.get_config(args.config)
|
|
in_mbox = mailbox.Maildir(config["top_inbox_maildir"])
|
|
out_mbox = mailbox.mbox(config["top_mbox_file"])
|
|
|
|
out_mbox.clear()
|
|
|
|
buffer = []
|
|
|
|
timezone = pytz.timezone("Europe/Berlin")
|
|
last_fsr_date = datetime.date.fromisoformat(open(config["last_date_file"]).read().strip())
|
|
last_fsr_datetime = datetime.datetime.combine(last_fsr_date, datetime.time(17, 30), timezone)
|
|
|
|
for message in in_mbox:
|
|
if message["List-Id"]:
|
|
if "top.fsmi.uni-karlsruhe.de" in decode_header(message["List-Id"]).strip():
|
|
date = parser.parse(message["Date"])
|
|
if date > last_fsr_datetime:
|
|
buffer.append(message)
|
|
|
|
for message in sorted(buffer, key=lambda x: parser.parse(x["Date"])):
|
|
out_mbox.add(message)
|
|
|
|
out_mbox.close()
|
|
|