Merged with chore/refactor-core

This commit is contained in:
Maxim Slipenko 2024-07-07 21:25:10 +03:00
parent 9d89aee578
commit 837613e072
6 changed files with 42 additions and 44 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ __pycache__
OCAB.db
src/paths.json
src/core/config.yaml
src/core/log/**/*

View File

@ -1,13 +0,0 @@
TELEGRAM:
TOKEN:
YANDEXGPT:
TOKEN:
CATALOGID:
PROMPT:
ROLES:
ADMIN: 0
MODERATOR: 1
USER: 2
BOT: 3

View File

View File

@ -1,24 +1,29 @@
import logging
import os
import time
async def check_log_file():
# Проверка наличия файла для логов в формате log-dd-mm-yyyy.log
# Если файл существует, то pass
# Если файл не существует, то создаём его. файл лежит в директории src.core.log
current_data = time.strftime("%d-%m-%Y")
log_file = os.path.join(os.path.dirname(__file__), "log/", f"log-{current_data}.log")
if not os.path.exists(log_file):
with open(log_file, 'w') as file:
file.write("Log file created\n")
file.close()
else:
pass
def setup_logger():
"""
Настройка логирования
"""
current_date = time.strftime("%d-%m-%Y")
log_dir = os.path.join(os.path.dirname(__file__), "log")
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, f"log-{current_date}.log")
logging.basicConfig(
filename=log_file,
level=logging.INFO,
format="%(asctime)s %(message)s",
datefmt="%H:%M:%S"
)
async def log(message):
await check_log_file()
current_data = time.strftime("%d-%m-%Y")
log_file = os.path.join(os.path.dirname(__file__), "log/", f"log-{current_data}.log")
# print(log_file)
with open(log_file, 'a') as file:
file.write(f"{time.strftime('%H:%M:%S')} {message}\n")
file.close()
"""
Функция для логирования сообщений
Она асинхронная, хотя таковой на самом деле не является.
"""
logging.info(message)

View File

@ -1,13 +1,18 @@
from routers import include_routers
from src.core.logger import log, setup_logger
from src.modules.standard.config.config import get_telegram_token
from src.modules.standard.database.db_api import connect_database, create_tables
from asyncio import run
import asyncio
from aiogram import Bot, Dispatcher
async def main(bot: Bot):
async def main():
bot = None
database = None
setup_logger()
try:
bot = Bot(token=get_telegram_token())
database, path = connect_database()
database.connect()
create_tables(database)
@ -15,12 +20,14 @@ async def main(bot: Bot):
dp = Dispatcher()
await include_routers(dp)
await dp.start_polling(bot)
except Exception as e:
log(e)
finally:
await bot.session.close()
database.close()
if bot is not None:
await bot.session.close()
if database is not None:
database.close()
if __name__ == "__main__":
bot = Bot(token=get_telegram_token())
run(main(bot))
asyncio.run(main())

View File

@ -1,10 +1,8 @@
from aiogram import Dispatcher, Router, F, Bot
from aiogram.types import Message
from aiogram import Dispatcher
from src.modules.standard.info.routers import router as info_router
from src.modules.standard.admin.routers import router as admin_router
from src.modules.standard.message_processing.message_api import router as process_message
from src.modules.standard.welcome.routers import router as welcome_router
async def include_routers(dp: Dispatcher):