Compare commits
No commits in common. "c2ab00680fded2f5e0e2c08443f90782502def0f" and "dd27264fd559508e159b901fbf5a629ef6316b37" have entirely different histories.
c2ab00680f
...
dd27264fd5
|
@ -7,7 +7,7 @@
|
|||
# Imports
|
||||
from abc import ABC, abstractmethod
|
||||
from tubot.static.abc import IValidatable
|
||||
from tubot.db.types import CacheDBTypes, User
|
||||
from tubot.db.types import CacheDBTypes, UserStates
|
||||
|
||||
|
||||
class CacheDB(IValidatable, ABC):
|
||||
|
@ -24,22 +24,50 @@ class CacheDB(IValidatable, ABC):
|
|||
# Users
|
||||
|
||||
@abstractmethod
|
||||
async def write_user(self, tg_id: int, user: User) -> None:
|
||||
async def add_user(self, tg_id: int, name: str) -> None:
|
||||
"""
|
||||
Writes user to cache db
|
||||
Add user to cache db
|
||||
|
||||
:param tg_id: User telegram id
|
||||
:param user: User object
|
||||
:param name: User telegram name
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def read_user(self, tg_id: int) -> User:
|
||||
async def user_state(self, tg_id: int) -> UserStates:
|
||||
"""
|
||||
Writes user to cache db
|
||||
Get user state
|
||||
|
||||
:param tg_id: User telegram id
|
||||
:return: Status of user
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def change_user_state(self, tg_id: int, status: UserStates) -> None:
|
||||
"""
|
||||
Change user state
|
||||
|
||||
:param tg_id: User telegram id
|
||||
:param status: New user status
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def auth_user(self, tg_id: int) -> None:
|
||||
"""
|
||||
Auth user
|
||||
|
||||
:param tg_id: User telegram id
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def is_user_auth(self, tg_id: int) -> bool:
|
||||
"""
|
||||
Check if user is already auth
|
||||
|
||||
:param tg_id: User telegram id
|
||||
:return: User object
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
|
|
@ -60,13 +60,38 @@ class PythonCache(CacheDB):
|
|||
|
||||
# Users
|
||||
|
||||
async def write_user(self, tg_id: int, user: User) -> None:
|
||||
self.users[tg_id] = user.to_dict
|
||||
async def add_user(self, tg_id: int, name: str) -> None:
|
||||
if tg_id in tuple(self.users.keys()):
|
||||
raise ValueError("User already exists")
|
||||
user = User(tg_id, name)
|
||||
self.users[tg_id] = user.dict
|
||||
await self._save_pkl()
|
||||
|
||||
async def read_user(self, tg_id: int) -> User:
|
||||
user_data = self.users[tg_id]
|
||||
return User.from_dict(user_data)
|
||||
async def user_state(self, tg_id: int) -> UserStates:
|
||||
if tg_id not in tuple(self.users.keys()):
|
||||
raise ValueError("User doesn't exists")
|
||||
user = self.users[tg_id]
|
||||
return user["state"]
|
||||
|
||||
async def change_user_state(self, tg_id: int, status: UserStates) -> None:
|
||||
if tg_id not in tuple(self.users.keys()):
|
||||
raise ValueError("User doesn't exists")
|
||||
user = self.users[tg_id]
|
||||
user["state"] = status
|
||||
await self._save_pkl()
|
||||
|
||||
async def auth_user(self, tg_id: int) -> None:
|
||||
if tg_id not in tuple(self.users.keys()):
|
||||
raise ValueError("User doesn't exists")
|
||||
user = self.users[tg_id]
|
||||
user["auth"] = True
|
||||
await self._save_pkl()
|
||||
|
||||
async def is_user_auth(self, tg_id: int) -> bool:
|
||||
if tg_id not in tuple(self.users.keys()):
|
||||
raise ValueError("User doesn't exists")
|
||||
user = self.users[tg_id]
|
||||
return user["auth"]
|
||||
|
||||
# Dirs
|
||||
|
||||
|
@ -100,21 +125,49 @@ class RedisCache(CacheDB):
|
|||
|
||||
# Users
|
||||
|
||||
async def write_user(self, tg_id: int, user: User) -> None:
|
||||
async def add_user(self, tg_id: int, name: str) -> None:
|
||||
async with aioredis.from_url(
|
||||
f"redis://{self.host}", encoding="utf-8", decode_responses=True
|
||||
) as redis:
|
||||
json = json_dumps(user.to_dict)
|
||||
user = User(tg_id, name)
|
||||
json = json_dumps(user.dict)
|
||||
await redis.set(str(tg_id), json)
|
||||
|
||||
async def read_user(self, tg_id: int) -> User:
|
||||
async def user_state(self, tg_id: int) -> UserStates:
|
||||
async with aioredis.from_url(
|
||||
f"redis://{self.host}", encoding="utf-8", decode_responses=True
|
||||
) as redis:
|
||||
json = await redis.get(str(tg_id))
|
||||
user_data = json_loads(json)
|
||||
user_data["state"] = UserStates(user_data["state"])
|
||||
return User.from_dict(user_data)
|
||||
user = json_loads(json)
|
||||
return user["state"]
|
||||
|
||||
async def change_user_state(self, tg_id: int, status: UserStates) -> None:
|
||||
async with aioredis.from_url(
|
||||
f"redis://{self.host}", encoding="utf-8", decode_responses=True
|
||||
) as redis:
|
||||
json = await redis.get(str(tg_id))
|
||||
user = json_loads(json)
|
||||
user["state"] = status
|
||||
json = json_dumps(user)
|
||||
await redis.set(str(tg_id), json)
|
||||
|
||||
async def auth_user(self, tg_id: int) -> None:
|
||||
async with aioredis.from_url(
|
||||
f"redis://{self.host}", encoding="utf-8", decode_responses=True
|
||||
) as redis:
|
||||
json = await redis.get(str(tg_id))
|
||||
user = json_loads(json)
|
||||
user["auth"] = True
|
||||
json = json_dumps(user)
|
||||
await redis.set(str(tg_id), json)
|
||||
|
||||
async def is_user_auth(self, tg_id: int) -> bool:
|
||||
async with aioredis.from_url(
|
||||
f"redis://{self.host}", encoding="utf-8", decode_responses=True
|
||||
) as redis:
|
||||
json = await redis.get(str(tg_id))
|
||||
user = json_loads(json)
|
||||
return bool(user["auth"])
|
||||
|
||||
# Dirs
|
||||
|
||||
|
|
|
@ -22,9 +22,7 @@ class UserStates(Enum):
|
|||
Types of User status
|
||||
"""
|
||||
|
||||
IDLE = "IDLE"
|
||||
DIRS = "DIRS"
|
||||
WAIT_FOR_TORRENT = "WAIT_FOR_TORRENT"
|
||||
...
|
||||
|
||||
|
||||
class User(object):
|
||||
|
@ -34,15 +32,11 @@ class User(object):
|
|||
|
||||
tg_id: int
|
||||
name: str
|
||||
state: UserStates = UserStates.IDLE
|
||||
state: UserStates | None = None
|
||||
auth: bool = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
tg_id: int,
|
||||
name: str,
|
||||
state: UserStates = UserStates.IDLE,
|
||||
auth: bool = False,
|
||||
self, tg_id: int, name: str, state: UserStates | None = None, auth: bool = False
|
||||
) -> None:
|
||||
self.tg_id = tg_id
|
||||
self.name = name
|
||||
|
@ -50,18 +44,10 @@ class User(object):
|
|||
self.auth = auth
|
||||
|
||||
@property
|
||||
def to_dict(self):
|
||||
def dict(self):
|
||||
return {
|
||||
"tg_id": self.tg_id,
|
||||
"name": self.name,
|
||||
"state": self.state.value,
|
||||
"state": self.state,
|
||||
"auth": self.auth,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, usr: dict) -> "User":
|
||||
tg = usr["tg_id"]
|
||||
name = usr["name"]
|
||||
state = UserStates(usr["state"])
|
||||
auth = usr["auth"]
|
||||
return cls(tg, name, state, auth)
|
||||
|
|
|
@ -13,5 +13,5 @@ class GetterTypes(Enum):
|
|||
Types of getters
|
||||
"""
|
||||
|
||||
OS = "os"
|
||||
Jellyfin = "jellyfin"
|
||||
OS = "Python os module"
|
||||
Jellyfin = "Jelyfin API"
|
||||
|
|
|
@ -24,84 +24,3 @@ class REDIS_HOST(ENV):
|
|||
|
||||
_name = "REDIS_HOST"
|
||||
DEFAULT = "localhost:6379"
|
||||
|
||||
|
||||
class CACHE_TYPE(ENV):
|
||||
"""
|
||||
CacheDB Type
|
||||
"""
|
||||
|
||||
_name = "CACHE_TYPE"
|
||||
DEFAULT = "python"
|
||||
|
||||
|
||||
class DIR_GETTER(ENV):
|
||||
"""
|
||||
DirGetter Type
|
||||
"""
|
||||
|
||||
_name = "DIR_GETTER"
|
||||
DEFAULT = "os"
|
||||
|
||||
|
||||
class DG_OS_FOLDER(ENV):
|
||||
"""
|
||||
Path to parent directory for OS_DirGetter
|
||||
"""
|
||||
|
||||
_name = "DG_OS_FOLDER"
|
||||
DEFAULT = "/mnt/Media"
|
||||
|
||||
|
||||
class DG_JELLYFIN_HOST(ENV):
|
||||
"""
|
||||
Jellyfin Server API host
|
||||
"""
|
||||
|
||||
_name = "DG_JELLYFIN_HOST"
|
||||
DEFAULT = "http://localhost:8096"
|
||||
|
||||
|
||||
class DG_JELLYFIN_TOKEN(ENV):
|
||||
"""
|
||||
Jellyfin API key
|
||||
"""
|
||||
|
||||
_name = "DG_JELLYFIN_TOKEN"
|
||||
DEFAULT = ""
|
||||
|
||||
|
||||
class TORRENT_SERVER(ENV):
|
||||
"""
|
||||
Torrent Server Type
|
||||
"""
|
||||
|
||||
_name = "TORRENT_SERVER"
|
||||
DEFAULT = "qbit"
|
||||
|
||||
|
||||
class TS_USER(ENV):
|
||||
"""
|
||||
Torrent Server auth username
|
||||
"""
|
||||
|
||||
_name = "TS_USER"
|
||||
DEFAULT = ""
|
||||
|
||||
|
||||
class TS_PASSWORD(ENV):
|
||||
"""
|
||||
Torrent Server auth password
|
||||
"""
|
||||
|
||||
_name = "TS_PASSWORD"
|
||||
DEFAULT = ""
|
||||
|
||||
|
||||
class TS_HOST(ENV):
|
||||
"""
|
||||
Torrent Server host
|
||||
"""
|
||||
|
||||
_name = "TS_HOST"
|
||||
DEFAULT = "http://localhost"
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
######################
|
||||
# Init static module #
|
||||
######################
|
||||
|
||||
# Imports
|
||||
from typing import Iterable
|
||||
from tubot.static import env
|
||||
from tubot.static.functions import validate
|
||||
from tubot.torrent.apis import qBitTorrent
|
||||
from tubot.torrent.types import ServerTypes
|
||||
from tubot.torrent.abc import TorrentAPI
|
||||
from tubot.dirgetter.types import GetterTypes
|
||||
from tubot.dirgetter.abc import DirGetter
|
||||
from tubot.dirgetter.getter import OSGetter, Jellyfin
|
||||
from tubot.db.types import CacheDBTypes
|
||||
from tubot.db.abc import CacheDB
|
||||
from tubot.db.cache import PythonCache, RedisCache
|
||||
from asyncio import create_task, gather
|
||||
|
||||
|
||||
class InitBuilder(object):
|
||||
"""
|
||||
Init all bot modules
|
||||
"""
|
||||
|
||||
TORRENT_SERVER: ServerTypes | TorrentAPI
|
||||
DG: GetterTypes | DirGetter
|
||||
CACHE: CacheDBTypes | CacheDB
|
||||
|
||||
def set_torrent_server(self, server_type: ServerTypes) -> "InitBuilder":
|
||||
self.TORRENT_SERVER = server_type
|
||||
return self
|
||||
|
||||
def set_directory_getter(self, dg_type: GetterTypes) -> "InitBuilder":
|
||||
self.DG = dg_type
|
||||
return self
|
||||
|
||||
def set_cache_type(self, cache_type: CacheDBTypes) -> "InitBuilder":
|
||||
self.CACHE = cache_type
|
||||
return self
|
||||
|
||||
async def init_ts(self) -> None:
|
||||
host = env.TS_HOST()()
|
||||
user = env.TS_USER()()
|
||||
pwd = env.TS_PASSWORD()()
|
||||
match self.TORRENT_SERVER:
|
||||
case ServerTypes.qBitTorrent:
|
||||
self.TORRENT_SERVER = qBitTorrent(host, user, pwd)
|
||||
case _:
|
||||
raise TypeError
|
||||
await validate(self.TORRENT_SERVER)
|
||||
|
||||
async def init_dg(self) -> None:
|
||||
match self.DG:
|
||||
case GetterTypes.OS:
|
||||
base_dir = env.DG_OS_FOLDER()()
|
||||
self.DG = OSGetter(base_dir)
|
||||
case GetterTypes.Jellyfin:
|
||||
host = env.DG_JELLYFIN_HOST()()
|
||||
key = env.DG_JELLYFIN_TOKEN()()
|
||||
self.DG = Jellyfin(host, key)
|
||||
case _:
|
||||
raise TypeError
|
||||
await validate(self.DG)
|
||||
|
||||
async def init_cache(self) -> None:
|
||||
match self.CACHE:
|
||||
case CacheDBTypes.PythonPKL:
|
||||
self.CACHE = PythonCache()
|
||||
case CacheDBTypes.Redis:
|
||||
host = env.REDIS_HOST()()
|
||||
self.CACHE = RedisCache(host)
|
||||
case _:
|
||||
raise TypeError
|
||||
await validate(self.CACHE)
|
||||
|
||||
async def init_all_modules(self) -> None:
|
||||
tasks = (create_task(self.init_ts()), create_task(self.init_dg()), create_task(self.init_cache()))
|
||||
await gather(*tasks)
|
||||
|
||||
@property
|
||||
def tuple(self) -> Iterable:
|
||||
return (self.TORRENT_SERVER, self.DG, self.CACHE)
|
||||
|
||||
|
||||
async def init_modules() -> Iterable:
|
||||
ts = ServerTypes(env.TORRENT_SERVER()())
|
||||
dg = GetterTypes(env.DIR_GETTER()())
|
||||
cache = CacheDBTypes(env.CACHE_TYPE()())
|
||||
builder = InitBuilder().set_torrent_server(ts).set_directory_getter(dg).set_cache_type(cache)
|
||||
await builder.init_all_modules()
|
||||
return builder.tuple
|
|
@ -24,7 +24,7 @@ class ServerTypes(Enum):
|
|||
Types of Torrent servers API's
|
||||
"""
|
||||
|
||||
qBitTorrent = "qbit"
|
||||
qBitTorrent = "qBitTorrent Remote API"
|
||||
|
||||
|
||||
class TorrentFromServer(object):
|
||||
|
|
Reference in New Issue