magic-wand_4/bot_io_classes.py

129 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Bot I/O classes v1.00
# 14/06/2021
# https://t.me/ssleg © 2020 2021
from datetime import datetime
class TgBotUsers:
"""класс хранения пользователей бота"""
__slots__ = ['__users', '__timestamps', '__con', '__cursor']
def __init__(self, con, cursor):
self.__users = {}
self.__timestamps = []
self.__con = con
self.__cursor = cursor
self.__cursor.execute('select * from contacts')
for row in cursor.fetchall():
user_id = row[0]
f_name = row[1]
l_name = row[2]
user_acc = row[3]
indx = len(self.__timestamps)
self.__users[user_id] = (indx, f_name, l_name, user_acc)
self.__timestamps.append(datetime(year=2020, month=1, day=1))
def is_bot_user(self, user_id):
tmp = self.__users.get(user_id)
if tmp is not None:
return True
else:
return False
def new_user_store(self, userinfo):
user_id = userinfo.id
f_name = userinfo.first_name
l_name = userinfo.last_name
user_acc = userinfo.username
entry = (user_id, f_name, l_name, user_acc)
self.__cursor.execute('''insert into contacts
(user_id, first_name, last_name, account_name)
values (?,?,?,?)''', entry)
self.__con.commit()
timestamp = datetime.now()
indx = len(self.__timestamps)
self.__users[user_id] = (indx, f_name, l_name, user_acc)
self.__timestamps.append(timestamp)
def update_user_mess_timestamp(self, user_id):
timestamp = datetime.now()
user = self.__users.get(user_id)
if user is not None:
indx = user[0]
self.__timestamps[indx] = timestamp
def get_user_mess_timestamp(self, user_id):
user = self.__users.get(user_id)
if user is not None:
indx = user[0]
return self.__timestamps[indx]
else:
return datetime(year=2020, month=1, day=1)
def get_users_list(self):
excluded_users = []
now = datetime.now()
self.__cursor.execute('select user_id, ban_to_date from banlist')
for row in self.__cursor.fetchall():
banned_to = datetime.strptime(row[1], '%Y-%m-%d %H:%M:%S')
delta = now - banned_to
if delta.total_seconds() < 0:
excluded_users.append(row[0])
user_list = []
for key in self.__users:
if key not in excluded_users:
user_list.append(key)
return user_list
class OutMessagesQueue:
"""класс очереди исходящих сообщений"""
__slots__ = ['__queue', '__position_i']
def __init__(self):
self.__queue = []
self.__position_i = 0
def queue_empty(self):
if len(self.__queue) == 0:
return True
return False
def is_user_in_queue(self, user_id):
i = 0
finded_flag = False
while i < len(self.__queue):
if self.__queue[i][0] == user_id:
finded_flag = True
break
i += 1
return finded_flag, i
def add_message(self, user_id, message_text, file_name):
finded_flag, i = self.is_user_in_queue(user_id)
if not finded_flag:
self.__queue.append([user_id])
self.__queue[i].append((user_id, message_text, file_name))
def get_next_message(self):
element = self.__queue[self.__position_i][1]
return element
def set_sending_result(self, flag):
if flag:
user_queue = self.__queue[self.__position_i]
user_queue.pop(1)
if len(user_queue) == 1:
self.__queue.pop(self.__position_i)
else:
self.__position_i += 1
if self.__position_i >= len(self.__queue):
self.__position_i = 0