From 3d5a2eb1c7d29531cc8d5b04e9a7413ca33ebae4 Mon Sep 17 00:00:00 2001 From: tema Date: Sun, 19 Feb 2023 00:51:52 +0200 Subject: [PATCH] Added timetable --- .gitignore | 2 ++ configs/timetable/groups.json | 11 +++++++++++ handlers/callback/__init__.py | 3 ++- handlers/callback/main.py | 2 +- handlers/callback/timetable.py | 21 +++++++++++++++++++++ handlers/private/__init__.py | 3 ++- handlers/private/timetable.py | 10 ++++++++++ keyboards/inline/timetable.py | 34 ++++++++++++++++++++++++++++++++++ 8 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 configs/timetable/groups.json create mode 100644 handlers/callback/timetable.py create mode 100644 handlers/private/timetable.py create mode 100644 keyboards/inline/timetable.py diff --git a/.gitignore b/.gitignore index c33a55f..75d34c5 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ __pycache__/ *.ini !example_config.ini *.json +!configs/timetable/groups.json +*.jpg *.db *.sqlite3 *.txt diff --git a/configs/timetable/groups.json b/configs/timetable/groups.json new file mode 100644 index 0000000..63e410d --- /dev/null +++ b/configs/timetable/groups.json @@ -0,0 +1,11 @@ +{ + "1, 121, 12c": "1.jpg", + "131, 13c, 141, 14c": "2.jpg", + "3, 411, 42c, 431": "3.jpg", + "43c": "4.jpg", + "4, 521, 52c, 531": "5.jpg", + "53c, 541, 54c": "6.jpg", + "2, 221, 22c, 231": "7.jpg", + "23c, 241, 24c": "8.jpg", + "411, 421, 431": ["9.jpg","10.jpg"] +} diff --git a/handlers/callback/__init__.py b/handlers/callback/__init__.py index deec4a8..dc6f0de 100644 --- a/handlers/callback/__init__.py +++ b/handlers/callback/__init__.py @@ -1 +1,2 @@ -from . import main \ No newline at end of file +from . import main +from . import timetable \ No newline at end of file diff --git a/handlers/callback/main.py b/handlers/callback/main.py index bcd66a3..b440fdc 100644 --- a/handlers/callback/main.py +++ b/handlers/callback/main.py @@ -7,7 +7,7 @@ from keyboards.inline.keyboard import cancel_button, menu from parser import get_about_replacements -@dp.callback_query_handler(lambda c: c.data != "back") +@dp.callback_query_handler(lambda c: c.data != "back" and not len(c.data.split("|")) == 2) async def callback_query(query: types.CallbackQuery): from_user = query.from_user data = get_about_replacements() diff --git a/handlers/callback/timetable.py b/handlers/callback/timetable.py new file mode 100644 index 0000000..3fc7f2a --- /dev/null +++ b/handlers/callback/timetable.py @@ -0,0 +1,21 @@ +import io +import json + +from aiogram import types + +from load import dp, bot +from keyboards.inline.timetable import timetable + + +@dp.callback_query_handler(lambda c: c.data.split("|")[0] == "timetable") +async def callback_table(query: types.CallbackQuery): + message = query.message + group = query.data.split("|")[1] + file = timetable(group) + + for f in file: + await bot.send_photo( + message.chat.id, + io.BytesIO(open(f, 'rb').read()) + ) + await query.answer() \ No newline at end of file diff --git a/handlers/private/__init__.py b/handlers/private/__init__.py index 9ea4ffa..33e13d3 100644 --- a/handlers/private/__init__.py +++ b/handlers/private/__init__.py @@ -1,2 +1,3 @@ from . import main -from . import admin \ No newline at end of file +from . import admin +from . import timetable \ No newline at end of file diff --git a/handlers/private/timetable.py b/handlers/private/timetable.py new file mode 100644 index 0000000..1343d07 --- /dev/null +++ b/handlers/private/timetable.py @@ -0,0 +1,10 @@ +from aiogram import types + +from load import dp, bot +from keyboards.inline.timetable import timetable + + +@dp.message_handler(commands='timetable') +async def get_table(message: types.Message): + markup = timetable() + await bot.send_message(message.chat.id, "q", reply_markup=markup) diff --git a/keyboards/inline/timetable.py b/keyboards/inline/timetable.py new file mode 100644 index 0000000..a515332 --- /dev/null +++ b/keyboards/inline/timetable.py @@ -0,0 +1,34 @@ +import json + +from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton + + +BASE_PATH = 'configs/timetable' +FILE = f'{BASE_PATH}/groups.json' + + +def timetable(id: int = None): + keyboard = InlineKeyboardMarkup() + + with open(FILE, "r") as f: + a = json.load(f) + + if id is not None: + if len(id.split(",")) > 1: + links = [] + for i in id.split(","): + if i == '': continue + links.append(f"{BASE_PATH}/{i}") + return links + return [f"{BASE_PATH}/{id}"] + + for key, value in a.items(): + if type(value) == type([]): + tmp = '' + for i in value: + tmp += i + tmp += "," + keyboard.add(InlineKeyboardButton(key, callback_data=f"timetable|{str(tmp)}")) + continue + keyboard.add(InlineKeyboardButton(key, callback_data=f"timetable|{str(value)}")) + return keyboard \ No newline at end of file