-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcalendar.py
129 lines (111 loc) · 4.35 KB
/
tcalendar.py
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
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from messages import days, CALENDAR_CALLBACK, CALLBACK_ERROR
from utils import separate_callback_data, translate_date_to_fa
import jdatetime as datetime
def create_callback_data(action, year=0, month=0, day=0):
return CALENDAR_CALLBACK + ";" + ";".join([action, str(year), str(month), str(day)])
def create_calendar(year=None, month=None):
now = datetime.datetime.now()
if not year: year = now.year
if not month: month = now.month
keyboard = []
#First row - Month and Year
row=[]
datetime.set_locale('fa_IR')
row.append(
InlineKeyboardButton(
datetime.date(year, month, 1).strftime("%B") + " " + str(year),
callback_data=create_callback_data("IGNORE")
)
)
keyboard.append(row)
#Second row - Week Days
row=[]
for day in days:
row.append(InlineKeyboardButton(day,callback_data=create_callback_data("IGNORE")))
keyboard.append(row)
month_weeks = monthcalendar(year, month)
for week in month_weeks:
row = []
for day in week:
if day == 0:
row.append(InlineKeyboardButton(" ", callback_data=create_callback_data("IGNORE")))
else:
row.append(
InlineKeyboardButton(
str(day),
callback_data=create_callback_data("DAY", year, month, day)
)
)
keyboard.append(row)
#Last row - Buttons
row = []
if month != now.month:
row.append(
InlineKeyboardButton(
"<",
callback_data=create_callback_data("PREV-MONTH", year, month, day)
)
)
else:
row.append(InlineKeyboardButton(" ", callback_data=create_callback_data("IGNORE")))
row.append(
InlineKeyboardButton(
">",
callback_data=create_callback_data("NEXT-MONTH", year, month, day)
)
)
keyboard.append(row)
return InlineKeyboardMarkup(keyboard)
def process_calendar_selection(bot, update):
out = (False, None)
query = update.callback_query
(_, action, year, month, day) = separate_callback_data(query.data)
curr = datetime.datetime(int(year), int(month), 1)
if action == "IGNORE":
bot.answer_callback_query(callback_query_id=query.id)
elif action == "DAY":
bot.edit_message_text(
text=query.message.text,
chat_id=query.message.chat_id,
message_id=query.message.message_id
)
out = True, translate_date_to_fa(
datetime.datetime(int(year), int(month), int(day)).strftime('%A %d %B'))
elif action == "PREV-MONTH":
pre = curr - datetime.timedelta(days=1)
bot.edit_message_text(text=query.message.text,
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=create_calendar(int(pre.year), int(pre.month)))
elif action == "NEXT-MONTH":
ne = curr + datetime.timedelta(days=31)
bot.edit_message_text(text=query.message.text,
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=create_calendar(int(ne.year), int(ne.month)))
else:
bot.answer_callback_query(callback_query_id= query.id, text=CALLBACK_ERROR)
return out
def monthcalendar(year=datetime.datetime.today().year, month=datetime.datetime.today().month):
start_day_week_day = datetime.date(year, month, 1).weekday()
weeks = []
weeks.append([0] * start_day_week_day + list(range(1, 8 - start_day_week_day)))
days_left = (
datetime.date(year, month, 1) - datetime.timedelta(days=1)
).day - weeks[0][-1]
for i in range(days_left // 7):
weeks.append(list(range(weeks[i][-1] + 1, weeks[i][-1] + 8)))
if days_left % 7:
weeks.append(list(range(weeks[-1][-1] + 1, weeks[-1][-1] + 1 + (days_left % 7))) + [0] * (7 - days_left % 7))
# remove days before today
if datetime.date.today().month == month and \
datetime.date.today().year == year:
today = datetime.date.today().day
for week in weeks:
for i in range(7):
if week[i] <= today: week[i] = 0
else: break
else: continue
break
return weeks