2019-09-18 17:06:34 +02:00
|
|
|
import datetime
|
2019-09-18 20:03:24 +02:00
|
|
|
import requests
|
|
|
|
from EDTcalendar import Calendar
|
2019-12-21 11:47:56 +01:00
|
|
|
from feedparser import parse
|
2019-09-18 20:03:24 +02:00
|
|
|
|
2019-11-22 10:40:43 +01:00
|
|
|
KFET_URL = "http://kfet.bdeinfo.org/orders"
|
2019-09-18 20:03:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_now():
|
|
|
|
return datetime.datetime.now(datetime.timezone.utc).astimezone(tz=None)
|
2019-09-18 17:06:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
class User:
|
|
|
|
def __init__(self, user_id: int, language: str):
|
|
|
|
self.id = user_id
|
|
|
|
self.language = language
|
|
|
|
self.resources = None
|
|
|
|
self.nt = False
|
|
|
|
self.nt_time = 20
|
|
|
|
self.nt_cooldown = 20
|
2019-09-18 20:03:24 +02:00
|
|
|
self.nt_last = get_now()
|
|
|
|
self.kfet = None
|
2019-11-22 13:25:35 +01:00
|
|
|
self.await_cmd = str()
|
2019-12-21 11:47:56 +01:00
|
|
|
self.tomuss_rss = str()
|
|
|
|
self.tomuss_last = str()
|
2019-09-18 17:06:34 +02:00
|
|
|
|
|
|
|
def calendar(self, time: str = "", pass_week: bool = False):
|
|
|
|
return Calendar(time, self.resources, pass_week=pass_week)
|
2019-09-18 20:03:24 +02:00
|
|
|
|
|
|
|
def get_notif(self):
|
|
|
|
if self.resources and self.nt:
|
|
|
|
now = get_now()
|
|
|
|
c = self.calendar(pass_week=False)
|
|
|
|
for e in c.timeline:
|
2019-09-20 13:45:10 +02:00
|
|
|
if 0 < (e.begin - now).total_seconds() // 60 <= self.nt_time and \
|
|
|
|
0 < (now - self.nt_last).total_seconds() // 60 >= self.nt_cooldown:
|
2019-09-18 20:03:24 +02:00
|
|
|
self.nt_last = get_now()
|
|
|
|
return e
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_kfet(self):
|
|
|
|
res = None
|
|
|
|
if self.kfet:
|
|
|
|
cmds = requests.get(KFET_URL).json()
|
2019-09-20 08:16:58 +02:00
|
|
|
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
|
2019-09-18 20:03:24 +02:00
|
|
|
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
|
|
|
|
return entry[self.tomuss_last:]
|