52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
from pathlib import Path
|
|
|
|
from dotenv import load_dotenv
|
|
from pdfminer.pdfdocument import PDFDocument
|
|
from pdfminer.pdfparser import PDFParser
|
|
|
|
from config import (
|
|
MENU_CRECHE_PDF_URL,
|
|
MENU_TYPE,
|
|
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")
|
|
if result_file.was_already_sent(creation_date, modification_date, MENU_TYPE):
|
|
return
|
|
|
|
message_formatter = MenuMessageFormatter()
|
|
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()
|