111 lines
3.8 KiB
Python
111 lines
3.8 KiB
Python
import datetime
|
|
import hashlib
|
|
import json
|
|
import os
|
|
|
|
from telebot.types import CallbackQuery, InlineQuery, Message, ReplyKeyboardRemove
|
|
|
|
from models import logger
|
|
|
|
from .schedule import Schedule, getSchedulePathByGroup
|
|
|
|
|
|
class User:
|
|
def __init__(self, userID: int) -> None:
|
|
self.id: int = userID
|
|
try:
|
|
self.data: dict[str, str] = json.load(
|
|
open(f"./users/{self.id}.json", "r", encoding="utf-8")
|
|
)
|
|
except:
|
|
logger.error("Bot", f'INVALID "{userID}" SAVED DATA! RESET!')
|
|
self.data = {}
|
|
|
|
self.state: str | bool | None = self.data.get("state", None)
|
|
|
|
self.topic: str | None = self.data.get("topic", None)
|
|
self.course: str | None = self.data.get("course", None)
|
|
self.group: str | None = self.data.get("group", None)
|
|
self.notifications: list[str] = self.data.get("notifications", []) # type: ignore
|
|
self.valid: bool = self.data.get("valid", True)
|
|
self.saved_groups: list[str] = self.data.get("saved_groups", [])
|
|
self.saved_groups.append(self.group)
|
|
self.saved_groups = [t if "-" in t else "{null}" for t in self.saved_groups]
|
|
self.saved_groups = list(set(self.saved_groups))
|
|
try: self.saved_groups.remove("{null}")
|
|
except: ...
|
|
|
|
self.user: dict = self.data.get("user", {})
|
|
self.usage: dict = self.data.get(
|
|
"usage", {"count": -1, "date": None, "time": None}
|
|
)
|
|
self.flags: list[str] = self.data.get('flags', [])
|
|
|
|
if "notifyEvening" in self.data.keys():
|
|
self.notifyEvening: str = self.data["notifyEvening"]
|
|
self.notifyBeforeLesson: str = self.data["notifyBeforeLesson"]
|
|
if self.data["notifyEvening"] and "evening" not in self.notifications:
|
|
self.notifications.append("evening")
|
|
if self.data["notifyBeforeLesson"] and "lesson" not in self.notifications:
|
|
self.notifications.append("lesson")
|
|
|
|
self.save()
|
|
|
|
def schedule(self) -> Schedule | None:
|
|
date = datetime.datetime.now()
|
|
path = getSchedulePathByGroup(self.group)
|
|
if not path:
|
|
self.valid = False
|
|
self.notifications = []
|
|
self.setState("broken")
|
|
self.save()
|
|
return None
|
|
|
|
return Schedule(
|
|
{
|
|
"date": {
|
|
"weekNumber": date.isocalendar().week,
|
|
"weekDay": date.weekday(),
|
|
},
|
|
"schedule": json.load(open(path, "r", encoding="utf-8"))[self.group],
|
|
}
|
|
)
|
|
|
|
def setState(self, newState: str) -> None:
|
|
self.state = newState
|
|
self.save()
|
|
|
|
def save(self) -> None:
|
|
return json.dump(
|
|
{
|
|
"state": self.state,
|
|
"topic": self.topic,
|
|
"course": self.course,
|
|
"group": self.group,
|
|
"notifications": self.notifications,
|
|
"valid": self.valid,
|
|
"saved_groups": self.saved_groups,
|
|
"user": self.user,
|
|
"usage": self.usage,
|
|
"flags": self.flags,
|
|
},
|
|
open(f"./users/{self.id}.json", "w", encoding="utf-8"),
|
|
ensure_ascii=False,
|
|
indent=4,
|
|
)
|
|
|
|
|
|
def validate(ctx: Message | InlineQuery | CallbackQuery) -> User:
|
|
try:
|
|
return User(ctx.from_user.id) # type: ignore
|
|
except Exception as e:
|
|
logger.error("Bot", f'INVALID "{ctx.from_user.id}" VALIDATION! {e}')
|
|
return False # type: ignore
|
|
|
|
|
|
def getAllUsers() -> list[User]: # type: ignore
|
|
for _, _, filenames in os.walk("./users"):
|
|
for userFile in filenames:
|
|
if ".json" not in userFile:
|
|
continue
|
|
yield User(int(userFile.replace(".json", ""))) # type: ignore
|