mirror of
https://github.com/EDeev/school_menu.git
synced 2026-10-07 20:49:36 +03:00
Значения ячеек Excel-меню с сайта школы вычислялись через eval — любой текст в файле выполнился бы как код. Теперь arithmetic() разбирает только числа и + - * /.
69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
import ast
|
|
import operator
|
|
import requests as req
|
|
import datetime as dt
|
|
import openpyxl, os, re
|
|
|
|
_OPS = {ast.Add: operator.add, ast.Sub: operator.sub, ast.Mult: operator.mul, ast.Div: operator.truediv}
|
|
|
|
|
|
def arithmetic(text):
|
|
"""Значение ячейки вида «150» или «150+30»: только числа и + - * /, без eval."""
|
|
def calc(node):
|
|
if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
|
|
return node.value
|
|
if isinstance(node, ast.BinOp) and type(node.op) in _OPS:
|
|
return _OPS[type(node.op)](calc(node.left), calc(node.right))
|
|
if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
|
|
return -calc(node.operand)
|
|
raise ValueError(f"недопустимое значение в ячейке: {text!r}")
|
|
return calc(ast.parse(text, mode="eval").body)
|
|
|
|
|
|
def eating(day):
|
|
if day == 0:
|
|
wkd = dt.datetime.today().weekday()
|
|
else:
|
|
wkd = (dt.date.today() + dt.timedelta(days=1)).weekday()
|
|
|
|
if wkd != 6:
|
|
flag = False
|
|
try:
|
|
if day == 0:
|
|
eat = f"https://www.school01.ru/food/{str(dt.date.today())}-sm.xlsx"
|
|
else:
|
|
eat = f"https://www.school01.ru/food/{str(dt.date.today() + dt.timedelta(days=1))}-sm.xlsx"
|
|
|
|
file, xls = open("school_food.xlsx", "wb"), req.get(eat)
|
|
file.write(xls.content)
|
|
file.close()
|
|
|
|
excel_file = openpyxl.load_workbook('school_food.xlsx')
|
|
employees_sheet = excel_file[excel_file.sheetnames[0]]
|
|
|
|
food = {employees_sheet["A4"].value: [], employees_sheet["A12"].value: []}
|
|
food_info = [[employees_sheet["A4"].value, 4, 8], [employees_sheet["A12"].value, 12, 9]]
|
|
|
|
flag = True
|
|
|
|
if employees_sheet["A22"].value is not None:
|
|
food[employees_sheet["A22"].value] = []
|
|
food_info.append([employees_sheet["A22"].value, 21, 2])
|
|
|
|
for j in food_info:
|
|
for i in range(j[2]):
|
|
if employees_sheet[f"D{j[1] + i}"].value is None:
|
|
continue
|
|
|
|
gramm = str(int(arithmetic(str(employees_sheet[f"E{j[1] + i}"].value).strip())))
|
|
kkal = str(arithmetic("".join(str(employees_sheet[f"J{j[1] + i}"].value).split()).replace(",", ".", 1)))
|
|
|
|
food[j[0]].append([employees_sheet[f"D{j[1] + i}"].value, [gramm, kkal]])
|
|
|
|
os.remove("school_food.xlsx")
|
|
|
|
return [True, food]
|
|
except Exception as e:
|
|
return [False, flag, wkd, repr(e)]
|
|
else:
|
|
return [False, False, wkd]
|