-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (69 loc) · 3.05 KB
/
app.py
File metadata and controls
83 lines (69 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from flask import Flask, jsonify, render_template
import os
import json
import tomllib
import logging
from dotenv import load_dotenv
from helpers.db import get_emails, update_settings, get_email
from helpers.fetch import Polymensa
from helpers.sendmail import BurgerSend
from helpers.datehandler import is_weekend, is_quiet_date, today
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(levelname)s in %(module)s: %(message)s')
load_dotenv()
app = Flask(__name__)
@app.route("/api")
def send_email():
try:
# environment variables
email = os.getenv("EMAIL")
password = os.getenv("PASSWORD")
# config.toml variables
with open("config.toml", "rb") as f:
config = tomllib.load(f)
env = config["env"]
api = config["api"]
settings = config["settings"]
# don't send emails on weekends / the date is in the quiet days list
if is_quiet_date(today(), settings.get("quiet")):
return jsonify({"status": "today the burgerBot is quiet"}), 200
if is_weekend():
return jsonify({"status": "It's the weekend, no burgers today!"}), 200
# get data from polymensa, recipients from database
mensa = Polymensa(**api)
meals = mensa.get_dishes()
has_burger, _ = mensa.has_burger()
recipients = get_emails()
sent = 0
# send email to every recipient, adhering to the settings and enviroment
mail = BurgerSend(email, password, mensa, settings.get("url"))
for token, re in recipients.items():
# skip if dev environment
if env == "dev" and not re.get("development"):
logging.info(f"skipping prod recipient { re.get('email') }")
continue
# skip if settings is "never" or "burger" and there is no burger
sending = re.get("sending")
if sending == "never" or (sending == "burger" and not has_burger):
logging.info(f"skipping recipient { re.get("email") } because of settings: (sending:{ sending }), (burger: { has_burger })")
continue
# send email
mail.send(token, re)
logging.info(f"sent email to { re.get('email') }")
sent += 1
return jsonify({"status": f"Email sent successfully to {sent} recipients!", "meals": meals}), 200
except Exception as e:
logging.error(f"Error sending email: {e}")
return jsonify({"error": str(e)}), 500
@app.route("/")
def hello():
with open("config.toml", "rb") as f:
config = tomllib.load(f)
return render_template("index.html", config=config)
@app.route("/api/change/<token>/<sending>", methods=["GET", "POST"])
def change_settings(token, sending):
data = get_email(token)
if data and sending in ["always", "never", "burger"]:
update_settings(token, sending)
return render_template("settings.html", found=True, email=data["email"], setting=sending, token=token), 200
else:
return render_template("settings.html", found=False, token=token), 400