1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramEDT/base.py

70 lines
2.3 KiB
Python
Raw Normal View History

import datetime
import requests
from EDTcalendar import Calendar
2019-12-21 11:47:56 +01:00
from feedparser import parse
2019-12-25 17:57:59 +01:00
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Boolean, Date
2019-11-22 10:40:43 +01:00
KFET_URL = "http://kfet.bdeinfo.org/orders"
2019-12-25 17:57:59 +01:00
Base = declarative_base()
def get_now():
return datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None)
2019-12-25 17:57:59 +01:00
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, unique=True)
language = Column(String, default="")
resources = Column(Integer)
nt = Column(Boolean, default=False)
nt_time = Column(Integer, default=20)
nt_cooldown = Column(Integer, default=20)
nt_last = Column(Date, default=get_now)
kfet = Column(Integer, default=0)
await_cmd = Column(String, default="")
tomuss_rss = Column(String)
tomuss_last = Column(String)
def calendar(self, time: str = "", pass_week: bool = False):
return Calendar(time, self.resources, pass_week=pass_week)
def get_notif(self):
if self.resources and self.nt:
now = get_now()
c = self.calendar(pass_week=False)
for e in c.timeline:
if 0 < (e.begin - now).total_seconds() // 60 <= self.nt_time and \
0 < (now - self.nt_last).total_seconds() // 60 >= self.nt_cooldown:
self.nt_last = get_now()
return e
return None
def get_kfet(self):
res = None
if self.kfet:
cmds = requests.get(KFET_URL).json()
if cmds and str(self.kfet) in cmds:
2019-11-22 10:40:43 +01:00
res = 1 if cmds[str(self.kfet)] == "ok" else 2 if cmds[str(self.kfet)] == "ko" else 3
elif get_now().hour >= 14:
res = 3
self.kfet = None if res else self.kfet
return res
2019-12-21 11:47:56 +01:00
def get_tomuss(self):
entry = list()
if self.tomuss_rss:
entry = [e for e in parse(self.tomuss_rss).entries]
if not self.tomuss_last:
return entry
2019-12-25 17:57:59 +01:00
tomuss_last = 0
for i,e in enumerate(entry):
if str(e) == self.tomuss_last:
tomuss_last = i+1
break
return entry[tomuss_last:]
def __repr__(self):
return f"<User: {self.id}>"