used jinja templates
This commit is contained in:
parent
aa4f085286
commit
84381325f1
5 changed files with 31 additions and 36 deletions
|
@ -5,7 +5,7 @@ from dotenv import load_dotenv
|
|||
load_dotenv()
|
||||
|
||||
MENU_CRECHE_PDF_URL = os.environ.get("MENU_CRECHE_PDF_URL")
|
||||
MENU_TYPE = os.environ.get("MENU_KIND")
|
||||
MENU_TYPES = os.environ.get("MENU_TYPES").split(",")
|
||||
SIGNAL_SENDER = os.environ.get("SIGNAL_SENDER")
|
||||
SIGNAL_RECIPIENTS = os.environ.get("SIGNAL_RECIPIENTS", default=SIGNAL_SENDER).split(
|
||||
","
|
||||
|
|
22
main.py
22
main.py
|
@ -1,12 +1,13 @@
|
|||
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_TYPE,
|
||||
MENU_TYPES,
|
||||
SIGNAL_SENDER,
|
||||
SIGNAL_RECIPIENTS,
|
||||
SIGNAL_API_URL,
|
||||
|
@ -36,16 +37,21 @@ def main():
|
|||
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)
|
||||
templates_path = Path(__file__).parent / "templates"
|
||||
env = Environment(loader=FileSystemLoader(templates_path), autoescape=select_autoescape())
|
||||
message_formatter = MenuMessageFormatter(env)
|
||||
|
||||
signal_messager = SignalMessager(SIGNAL_API_URL, SIGNAL_SENDER)
|
||||
signal_messager.send_message(message, menu_pdf_file, SIGNAL_RECIPIENTS)
|
||||
for menu_type in MENU_TYPES:
|
||||
if result_file.was_already_sent(creation_date, modification_date, menu_type):
|
||||
return
|
||||
|
||||
result_file.write(creation_date, modification_date, MENU_TYPE)
|
||||
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__":
|
||||
|
|
34
menus.py
34
menus.py
|
@ -1,5 +1,6 @@
|
|||
from pathlib import Path
|
||||
|
||||
from jinja2 import Environment
|
||||
from pdfminer.high_level import extract_text
|
||||
from pdfminer.pdfdocument import PDFDocument
|
||||
from pdfminer.pdfparser import PDFParser
|
||||
|
@ -110,8 +111,10 @@ class Line:
|
|||
|
||||
|
||||
class MenuMessageFormatter:
|
||||
@staticmethod
|
||||
def create_message(menus: Menus, menu_type: str) -> str:
|
||||
def __init__(self, env: Environment):
|
||||
self.env = env
|
||||
|
||||
def create_message(self, menus: Menus, menu_type: str) -> str:
|
||||
days = menus.days
|
||||
if menu_type.lower() == "introduction":
|
||||
menu = menus.introduction
|
||||
|
@ -124,28 +127,5 @@ class MenuMessageFormatter:
|
|||
else:
|
||||
raise Exception(f"Unknown menu type: {menu_type}")
|
||||
|
||||
return f"""
|
||||
**Menu de la crèche** (message automatique)
|
||||
|
||||
Menu {menu_type}:
|
||||
|
||||
{days[0].upper()}:
|
||||
*Midi*: {menu[days[0]]["midi"]}
|
||||
*Goûter*: {menu[days[0]]["gouter"]}
|
||||
|
||||
{days[1].upper()}:
|
||||
*Midi*: {menu[days[1]]["midi"]}
|
||||
*Goûter*: {menu[days[1]]["gouter"]}
|
||||
|
||||
{days[2].upper()}:
|
||||
*Midi*: {menu[days[2]]["midi"]}
|
||||
*Goûter*: {menu[days[2]]["gouter"]}
|
||||
|
||||
{days[3].upper()}:
|
||||
*Midi*: {menu[days[3]]["midi"]}
|
||||
*Goûter*: {menu[days[3]]["gouter"]}
|
||||
|
||||
{days[4].upper()}:
|
||||
*Midi*: {menu[days[4]]["midi"]}
|
||||
*Goûter*: {menu[days[4]]["gouter"]}
|
||||
"""
|
||||
template = self.env.get_template("menus.txt")
|
||||
return template.render(menu=menu, menu_type=menu_type, days=days)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
requests~=2.32.3
|
||||
pdfminer.six==20250506
|
||||
python-dotenv~=1.1.0
|
||||
jinja2~=3.1.6
|
||||
|
|
8
templates/menus.txt
Normal file
8
templates/menus.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
**Menu de la crèche**
|
||||
|
||||
Menu {{ menu_type.upper() }}:
|
||||
{% for day in days %}
|
||||
**{{ day.upper() }}**
|
||||
**Midi :** {{ menu[day]["midi"] }}
|
||||
**Goûter :** {{ menu[day]["gouter"] }}
|
||||
{% endfor %}
|
Loading…
Add table
Add a link
Reference in a new issue