From 3c02cc20e908e5aa600669c525426e8c22d74171 Mon Sep 17 00:00:00 2001 From: trueold89 Date: Wed, 7 Aug 2024 21:01:00 +0300 Subject: [PATCH] Init bot.py && some bugfix - init bot.py - add initialize function - add entrypoint - fix user auth wrong exception - fix dirs cache --- requirements.txt | 14 +++++++--- setup.py | 6 ++--- tubot/bot.py | 53 ++++++++++++++++++++++++++++++++++++++ tubot/static/controller.py | 4 ++- tubot/static/env.py | 17 ++++++++++++ 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 tubot/bot.py diff --git a/requirements.txt b/requirements.txt index 80e5bb5..d460167 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,19 @@ -aiofiles>=24.1.0 +aiofiles>=23.2.1 +aiogram>=3.10.0 aiohappyeyeballs>=2.3.4 -aiohttp>=3.10.0 +aiohttp>=3.9.5 aiosignal>=1.3.1 -attrs>=23.2.0 +annotated-types>=0.7.0 +attrs>=24.2.0 +certifi>=2024.7.4 frozenlist>=1.4.1 idna>=3.7 +magic-filter>=1.0.12 multidict>=6.0.5 +pydantic>=2.8.2 +pydantic_core>=2.20.1 python-magic>=0.4.27 redis>=5.0.8 +ruff>=0.5.6 +typing_extensions>=4.12.2 yarl>=1.9.4 diff --git a/setup.py b/setup.py index 508b17b..0333cfd 100644 --- a/setup.py +++ b/setup.py @@ -8,10 +8,10 @@ setup( author_email="root@orudo.ru", description="A simple Telegram bot that will allow you to upload torrent files / magnet links to a remote Torrent server (qBitTorrent, Transmission, etc.)", install_requires=[ - "aiohttp>=3.10.0", - "aiofiles>=24.1.0", - "aiofiles>=24.1.0", + "aiohttp>=3.9.5", + "aiofiles>=23.2.1", "redis>=5.0.8", + "aiogram>=3.10.0" ], packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter", "db"], ) diff --git a/tubot/bot.py b/tubot/bot.py new file mode 100644 index 0000000..3dfbd7e --- /dev/null +++ b/tubot/bot.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +##################### +# Aiogram bot logic # +##################### + +# Imports +from asyncio import run + +from tubot.db.abc import CacheDB +from tubot.dirgetter.abc import DirGetter +from tubot.static.init import init_modules +from tubot.static.controller import Controller +from tubot.static.env import BOT_TOKEN +from tubot.torrent.abc import TorrentAPI + +from aiogram import Bot, Dispatcher, Router + +# Init + +## --- Modules --- ## +cache: CacheDB +dirgetter: DirGetter +torrent_api: TorrentAPI +ctrl: Controller + +## --- Bot --- ## +bot: Bot +router: Router +dp: Dispatcher + + +async def initialize() -> None: + # --- Modules --- # + global cache, dirgetter, torrent_api, ctrl + torrent_api, dirgetter, cache = await init_modules() + ctrl = Controller(torrent_api, dirgetter, cache) + # --- Bot --- # + global router, dp, bot + router = Router() + dp = Dispatcher() + dp.include_router(router) + bot = Bot(BOT_TOKEN()()) + await dp.start_polling(bot) + + +def main() -> None: + run(initialize()) + + +if __name__ == "__main__": + main() diff --git a/tubot/static/controller.py b/tubot/static/controller.py index 7834fbe..c5dfdae 100644 --- a/tubot/static/controller.py +++ b/tubot/static/controller.py @@ -10,7 +10,7 @@ from tubot.dirgetter.abc import DirGetter from tubot.db.abc import CacheDB from tubot.db.types import User, UserStates from tubot.static.functions import validate -from tubot.static.env import AUTH_PASSWD +from tubot.static.env import AUTH_PASSWD, CACHE_EXPIRE from tubot.static.exceptions import AlreadyExists, AuthError @@ -44,6 +44,7 @@ class Controller(object): return dirs dirs = await self.getter.folders if len(dirs) > 0: + await self.cache.cache_dirs(dirs, int(CACHE_EXPIRE()())) return dirs raise KeyError("No dirs found") @@ -107,6 +108,7 @@ class Controller(object): if pwd == AUTH_PASSWD()(): user.auth = True await self.cache.write_user(user.tg_id, user) + return raise AuthError("Wrong password") async def set_user_state(self, user: User, state: UserStates) -> None: diff --git a/tubot/static/env.py b/tubot/static/env.py index d83540b..8b3646b 100644 --- a/tubot/static/env.py +++ b/tubot/static/env.py @@ -114,3 +114,20 @@ class AUTH_PASSWD(ENV): _name = "AUTH_PASSWD" DEFAULT = "changeme" + + +class BOT_TOKEN(ENV): + """ + TelegramAPI Bot Token + """ + + _name = "BOT_TOKEN" + DEFAULT = "" + + +class CACHE_EXPIRE(ENV): + """ + Cache expire time in seconds + """ + _name = "CACHE_EXPIRE" + DEFAULT = "120"