menu-creche/main.py
2025-05-21 23:40:07 +02:00

58 lines
1.7 KiB
Python

from pathlib import Path
from dotenv import load_dotenv
from jinja2 import Environment, PackageLoader, select_autoescape, FileSystemLoader
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfparser import PDFParser
from config import (
MENU_CRECHE_PDF_URL,
MENU_TYPES,
SIGNAL_SENDER,
SIGNAL_RECIPIENTS,
SIGNAL_API_URL,
)
from menus import Menus, MenuMessageFormatter
from message_sender import SignalMessager
from pdf_downloader import download
from result_file import ResultFile
load_dotenv()
def read_pdf_metadata(pdf_file: Path):
fp = open(pdf_file, "rb")
parser = PDFParser(fp)
doc = PDFDocument(parser)
return doc.info
def main():
menu_pdf_file = download(MENU_CRECHE_PDF_URL, file_name="menu.pdf")
menus = Menus(menu_pdf_file)
pdf_metadata = read_pdf_metadata(menu_pdf_file)
creation_date = pdf_metadata[0]["CreationDate"].decode("utf-8").removeprefix("D:")
modification_date = pdf_metadata[0]["ModDate"].decode("utf-8").removeprefix("D:")
result_file = ResultFile(Path.home() / ".cache" / "menu_creche.txt")
templates_path = Path(__file__).parent / "templates"
env = Environment(loader=FileSystemLoader(templates_path), autoescape=select_autoescape())
message_formatter = MenuMessageFormatter(env)
for menu_type in MENU_TYPES:
if result_file.was_already_sent(creation_date, modification_date, menu_type):
return
message = message_formatter.create_message(menus, menu_type)
signal_messager = SignalMessager(SIGNAL_API_URL, SIGNAL_SENDER)
signal_messager.send_message(message, menu_pdf_file, SIGNAL_RECIPIENTS)
result_file.write(creation_date, modification_date, menu_type)
if __name__ == "__main__":
main()