-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.py
More file actions
executable file
·72 lines (61 loc) · 2.48 KB
/
Copy pathroot.py
File metadata and controls
executable file
·72 lines (61 loc) · 2.48 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
#!/home/undead404/vntu-schedule-bot/venv/bin/python3
"""
acquiring today's schedules and sending it to channels
"""
from datetime import datetime
import json
from decouple import config
import telepot
# ANCHOR_DATE is a date of some Monday in an odd week
ANCHOR_DATE = datetime.strptime(config("ANCHOR_DATE"), "%d.%m.%Y").date()
BOT = telepot.Bot(config("API_TOKEN"))
DAYS_IN_WEEK = 7
ODD_WEEK = 0
EVEN_WEEK = 1
def get_announcement(day_schedule):
""" returns announcement message from certain day's schedule """
if not day_schedule:
return "Приємного відпочинку!"
lessons_announcements = []
for i, lesson in enumerate(day_schedule):
if lesson is None:
lessons_announcements.append("{num}. ВІКНО".format(num=i + 1))
else:
lessons_announcements.append(
"{num}. {room}: {subject} ({teacher}) {type}".format(num=i + 1, **lesson))
return "\n".join(lessons_announcements)
def get_day_schedule(date, schedule):
""" returns certain day's schedule """
try:
weekday_schedule = schedule["schedule"][date.weekday()]
except IndexError:
return []
ODDITY_EVENNESS = get_oddity_evenness(date)
day_schedule = []
for lesson in weekday_schedule:
if isinstance(lesson, list):
lesson = lesson[ODDITY_EVENNESS]
day_schedule.append(lesson)
day_date_str = date.strftime("%d.%m.%Y")
if day_date_str in schedule["specialCases"]:
for lesson_num in schedule["specialCases"][day_date_str]:
while len(day_schedule) < int(lesson_num):
day_schedule.append(None)
day_schedule[int(
lesson_num) - 1] = schedule["specialCases"][day_date_str][lesson_num]
return day_schedule
def get_oddity_evenness(date):
""" returns EVEN_WEEK if a date's week is even and ODD_WEEK if a week is odd."""
return EVEN_WEEK if (date - ANCHOR_DATE).days // DAYS_IN_WEEK % 2 else ODD_WEEK
def load_schedule(filename="/home/undead404/vntu-schedule-bot/schedule.json"):
""" loading schedule data from a file """
with open(filename) as infile:
return json.load(infile)
if __name__ == "__main__":
TODAY = datetime.now().date()
data = load_schedule()
for schedule in data["schedules"]:
channel_id = int(schedule["channelId"])
today_schedule = get_day_schedule(TODAY, schedule)
announcement = get_announcement(today_schedule)
BOT.sendMessage(channel_id, announcement)