add script to extract top mails
This commit is contained in:
50
read_topmails.py
Executable file
50
read_topmails.py
Executable file
@@ -0,0 +1,50 @@
|
||||
#!/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
|
||||
|
||||
MAILDIR = "/home/yannik/mail/INBOX.Mailinglisten.FSMI/"
|
||||
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__":
|
||||
config = yaml.full_load(open(CONFIG_FILE))
|
||||
in_mbox = mailbox.Maildir(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("data/last_date").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()
|
||||
|
||||
Reference in New Issue
Block a user