2021-04-09 10:17:38 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
class UserManager:
|
|
|
|
def __init__(self):
|
|
|
|
self.last_widget = {}
|
|
|
|
self.search_times = {}
|
|
|
|
self.search_ban_times = {}
|
|
|
|
|
2021-05-01 11:10:45 +02:00
|
|
|
def add_search_time(self, user_id: str, search_time: float):
|
2021-04-09 10:17:38 +02:00
|
|
|
current_time = time.time()
|
|
|
|
search_times = self.search_times.get(user_id, [])
|
|
|
|
search_times.append(search_time)
|
|
|
|
counter = 0
|
|
|
|
|
|
|
|
for i in reversed(search_times):
|
|
|
|
if i > current_time - 10:
|
|
|
|
counter = counter + 1
|
|
|
|
if counter > 5:
|
|
|
|
self.search_ban_times[user_id] = current_time + int(60)
|
|
|
|
del self.search_times[user_id]
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if counter == 1:
|
|
|
|
del self.search_times[user_id]
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(search_times) > 20:
|
|
|
|
self.search_ban_times[user_id] = current_time + int(120)
|
|
|
|
del self.search_times[user_id]
|
|
|
|
return
|
|
|
|
|
|
|
|
self.search_times[user_id] = search_times
|
|
|
|
|
2021-05-01 11:10:45 +02:00
|
|
|
def check_search_ban_timeout(self, user_id: str):
|
2021-04-09 10:17:38 +02:00
|
|
|
ban_time = self.search_ban_times.get(user_id)
|
|
|
|
if ban_time:
|
|
|
|
timeout = int(ban_time - time.time())
|
|
|
|
if timeout > 0:
|
|
|
|
return timeout
|
|
|
|
del self.search_ban_times[user_id]
|