Init bot.py && some bugfix

- init bot.py
- add initialize function
- add entrypoint
- fix user auth wrong exception
- fix dirs cache
This commit is contained in:
trueold89 2024-08-07 21:01:00 +03:00
parent f88f007910
commit 3c02cc20e9
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
5 changed files with 87 additions and 7 deletions

View File

@ -1,11 +1,19 @@
aiofiles>=24.1.0 aiofiles>=23.2.1
aiogram>=3.10.0
aiohappyeyeballs>=2.3.4 aiohappyeyeballs>=2.3.4
aiohttp>=3.10.0 aiohttp>=3.9.5
aiosignal>=1.3.1 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 frozenlist>=1.4.1
idna>=3.7 idna>=3.7
magic-filter>=1.0.12
multidict>=6.0.5 multidict>=6.0.5
pydantic>=2.8.2
pydantic_core>=2.20.1
python-magic>=0.4.27 python-magic>=0.4.27
redis>=5.0.8 redis>=5.0.8
ruff>=0.5.6
typing_extensions>=4.12.2
yarl>=1.9.4 yarl>=1.9.4

View File

@ -8,10 +8,10 @@ setup(
author_email="root@orudo.ru", 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.)", 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=[ install_requires=[
"aiohttp>=3.10.0", "aiohttp>=3.9.5",
"aiofiles>=24.1.0", "aiofiles>=23.2.1",
"aiofiles>=24.1.0",
"redis>=5.0.8", "redis>=5.0.8",
"aiogram>=3.10.0"
], ],
packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter", "db"], packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter", "db"],
) )

53
tubot/bot.py Normal file
View File

@ -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()

View File

@ -10,7 +10,7 @@ from tubot.dirgetter.abc import DirGetter
from tubot.db.abc import CacheDB from tubot.db.abc import CacheDB
from tubot.db.types import User, UserStates from tubot.db.types import User, UserStates
from tubot.static.functions import validate 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 from tubot.static.exceptions import AlreadyExists, AuthError
@ -44,6 +44,7 @@ class Controller(object):
return dirs return dirs
dirs = await self.getter.folders dirs = await self.getter.folders
if len(dirs) > 0: if len(dirs) > 0:
await self.cache.cache_dirs(dirs, int(CACHE_EXPIRE()()))
return dirs return dirs
raise KeyError("No dirs found") raise KeyError("No dirs found")
@ -107,6 +108,7 @@ class Controller(object):
if pwd == AUTH_PASSWD()(): if pwd == AUTH_PASSWD()():
user.auth = True user.auth = True
await self.cache.write_user(user.tg_id, user) await self.cache.write_user(user.tg_id, user)
return
raise AuthError("Wrong password") raise AuthError("Wrong password")
async def set_user_state(self, user: User, state: UserStates) -> None: async def set_user_state(self, user: User, state: UserStates) -> None:

View File

@ -114,3 +114,20 @@ class AUTH_PASSWD(ENV):
_name = "AUTH_PASSWD" _name = "AUTH_PASSWD"
DEFAULT = "changeme" 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"