-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
48 lines (38 loc) · 1.45 KB
/
utils.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
# utils.py
from datetime import datetime, timedelta
from functools import wraps
DAY_OF_WEEK_MAPPING = {
"Monday": "Понедельник",
"Tuesday": "Вторник",
"Wednesday": "Среда",
"Thursday": "Четверг",
"Friday": "Пятница",
"Saturday": "Суббота",
"Sunday": "Воскресенье"
}
# Измененная функция is_odd_week принимает дату
def is_odd_week(date=None):
if date is None:
date = datetime.now()
week_number = date.isocalendar()[1]
return "even" if week_number % 2 != 0 else "odd"
# Декоратор для ограничения запросов
def rate_limit(limit: int, time_window: timedelta):
user_requests = {}
def decorator(func):
@wraps(func)
async def wrapper(message, *args, **kwargs):
user_id = message.from_user.id
now = datetime.now()
if user_id in user_requests:
request_times = user_requests[user_id]
user_requests[user_id] = [time for time in request_times if now - time < time_window]
else:
user_requests[user_id] = []
if len(user_requests[user_id]) < limit:
user_requests[user_id].append(now)
return await func(message, *args, **kwargs)
else:
return
return wrapper
return decorator