This repository was archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.py
More file actions
187 lines (163 loc) · 7.28 KB
/
schedule.py
File metadata and controls
187 lines (163 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
██ ██████ ███████ ███████ ██ ██
██ ██ ██ ██ ██ ██
██ █████ ███████ █████ ████
██ ██ ██ ██ ██
██ ██████ ███████ ███████ ██
Licensed under the GNU aGPLv3
https://www.gnu.org/licenses/agpl-3.0.html
"""
# requires: pytz
import re
from datetime import datetime, timedelta
from email.mime import message
import pytz
from telethon.tl.types import Message
from .. import loader, utils
@loader.tds
class ScheduleMod(loader.Module):
"""Lesson time schedule"""
strings = {
"name": "lessonSchedule",
'noneArg': f'<b>Не указаны аргументы.\nВот список поддерживаемых поясов\n{pytz.common_timezones}</b>',
'setShift': '<b>Смещение установлено.</b>',
'notCurrect': '<b>Указано неверное смещение</b>',
"noneSetuped": 'Расписание указано неверно!',
"sucSetuped": '<b>Расписание установлено, вы можете посмотреть его с помощью <code>.schedule</code></b>',
}
async def client_ready(self, client, db) -> None:
self._client = client
self._db = db
self.ts = self._db.get("Schedule", "shift", {})
self.sh = self._db.get("Schedule", "setup", {})
#self.sh = {}
async def days_schedule(self):
"""Storage to schedule
Returns:
dict: schedule of currect day
"""
day = datetime.isoweekday(datetime.now(pytz.timezone(self.ts.get('shift'))))
if day == 1 or day == 6 or day == 7:
sche = {'1': '08:35–09:15',
'2': '09:25–10:05',
'3': '10:20–11:00',
'4': '11:20–12:00',
'5': '12:20–13:00',
'6': '13:10–13:50',
'7': '14:00–14:40',
'8': '14:50–15:30'
}
return sche
else:
sche = {'1': '08:00–08:40',
'2': '08:50–09:30',
'3': '09:45–10:25',
'4': '10:45–11:25',
'5': '11:45–12:25',
'6': '12:35–13:15',
'7': '13:25–14:05',
'8': '14:15–14:55'
}
return sche
async def sort_time(self, delta_times):
"""converts dict into list and sort it.
Args:
timeRem (dict): number of lesson : delta of times
Returns:
list: sorted list
"""
deltas_list = []
temp_list = list(delta_times.items())
for val in temp_list:
deltas_list.append((val[0], val[1].seconds))
deltas_list.sort(key=lambda i: i[1])
return deltas_list
async def str_timing(self, sche_dict):
"""Find delta of currect time and times from dict
Args:
ScheDict (dict): dict with schedule
utczone (pytz.timezone()): timezone
Returns:
dict: number of lesson : delta of times
"""
time_delta = {}
for i in sche_dict.items():
now = datetime.now(pytz.timezone(self.ts.get('shift'))).time()
from_sche = datetime.strptime(i[1], "%H:%M")
delta_1 = timedelta(hours=now.hour,
minutes=now.minute,
seconds=now.second)
delta_2 = timedelta(hours=from_sche.hour,
minutes=from_sche.minute,
seconds=from_sche.second)
time_delta[i[0]] = (delta_2-delta_1)
return time_delta
async def shiftcmd(self, message: Message) -> None:
"""Смещение UTC в форме Часть света/Город (например Asia/Yekaterinburg)."""
args = utils.get_args_raw(message)
reply = await message.get_reply_message()
if args == "" and not reply:
await utils.answer(message, self.strings("noneArg"))
return
if args == "":
args = reply.text
try:
TimeZone = pytz.timezone(args)
except pytz.UnknownTimeZoneError:
await utils.answer(message, self.strings("notCurrect"))
else:
self.ts['shift'] = args
self._db.set("Schedule", "shift", self.ts)
await utils.answer(message, self.strings('setShift'))
@loader.unrestricted
async def timecmd(self, message: Message) -> None:
"""Показать текущее время на сервере"""
TimeZone = pytz.timezone(self.ts.get('shift'))
shift = datetime.now(TimeZone)
current_time = shift.strftime("%H:%M")
await utils.answer(message, current_time)
async def setupcmd(self, message: Message) -> None:
"""Настроить расписание"""
args = utils.get_args_raw(message)
reply = await message.get_reply_message()
if args == "" and not reply:
await utils.answer(message, self.strings("noneSetuped"))
return
if args == "":
args = reply.text
lesson = re.split(". |\n", args)
num = []
less = []
for element in lesson:
if element.isnumeric():
num.append(int(element))
else:
less.append(element)
self.sh = dict(zip(num, less))
await utils.answer(message, self.strings("sucSetuped"))
self._db.set("Schedule", "setup", self.sh)
@loader.unrestricted
async def tscmd(self, message: Message) -> None:
"""Текущее расписание на день"""
strings = []
items = await self.days_schedule()
for key,item in items.items():
strings.append("{}: {}".format(str(key).capitalize(), item))
result = "\n".join(strings)
await utils.answer(message, result)
@loader.unrestricted
async def tlcmd(self, message: Message) -> None:
"""Конец/Начало близжайшего урока"""
lessonStarts = {}
lessonsEnds = {}
temp = await self.days_schedule()
for key, value in temp.items():
lessonStarts[key] = value.split('–', maxsplit=1)[0]
lessonsEnds[key] = value.split('–')[1]
start = await self.sort_time(await self.str_timing(lessonStarts))
end = await self.sort_time(await self.str_timing(lessonsEnds))
if start[1][1] < end[1][1]:
answer = f'До начала {start[0][0]} урока: <code>{datetime.strftime(datetime.utcfromtimestamp(start[0][1]), "%H:%M:%S")}</code>\nУрок начнётся в <code>{str(lessonStarts[start[0][0]])}:00</code>'
else:
answer = f'До конца {end[0][0]} урока: <code>{datetime.strftime(datetime.utcfromtimestamp(end[0][1]), "%H:%M:%S")}</code>\nУрок закончится в: <code>{str(lessonsEnds[end[0][0]])}:00</code>'
await utils.answer(message, answer)