Add basic parser for mbox mails
This commit is contained in:
parent
05155e183f
commit
32650ae05a
1 changed files with 51 additions and 0 deletions
51
kontr_emails.py
Normal file
51
kontr_emails.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
from mailbox import mbox, mboxMessage
|
||||
import re
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class Parser:
|
||||
INFO_REGEX = re.compile(r"(\d{6}) \| (x\S*)\s*")
|
||||
SUBMISSION_REGEX = re.compile(r"adresář:\s+\S*\/(\S*)\s*")
|
||||
POINTS_REGEX = re.compile(r"\*\scelkový počet bodů\s+((\d|\.)*)\s*")
|
||||
|
||||
@staticmethod
|
||||
def parse_student_info(msg: mboxMessage) -> Tuple[str, str]:
|
||||
body = msg.get_payload()
|
||||
match = Parser.INFO_REGEX.search(body)
|
||||
if not match:
|
||||
raise ValueError("invalid mail has been given")
|
||||
|
||||
return match.group(1), match.group(2)
|
||||
|
||||
@staticmethod
|
||||
def parse_submission(msg: mboxMessage) -> str:
|
||||
body = msg.get_payload()
|
||||
match = Parser.SUBMISSION_REGEX.search(body)
|
||||
if not match:
|
||||
raise ValueError("invalid mail has been given")
|
||||
|
||||
return match.group(1)
|
||||
|
||||
@staticmethod
|
||||
def parse_points(msg: mboxMessage) -> float:
|
||||
body = msg.get_payload()
|
||||
match = Parser.POINTS_REGEX.search(body)
|
||||
if not match:
|
||||
raise ValueError("invalid mail has been given")
|
||||
|
||||
return float(match.group(1))
|
||||
|
||||
def __init__(self, path: str) -> None:
|
||||
self.path = path
|
||||
self.box = mbox(path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mails = mbox("~/xyz.mbox")
|
||||
_, mail = mails.popitem()
|
||||
print(Parser.parse_student_info(mail))
|
||||
print(Parser.parse_submission(mail))
|
||||
print(Parser.parse_points(mail))
|
Reference in a new issue