python_bot/db_logic/postcleaner.py

75 lines
2.6 KiB
Python
Raw Normal View History

2024-01-23 15:12:59 +10:00
from db_logic import collections
import datetime, logging
2024-01-23 15:12:59 +10:00
def append_post_to_cleaning_sequence(message, type=None):
try:
novenkiy = {
'msg_id': message.message_id,
'chat_id': message.chat.id,
'type': type,
'datetimes': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
metadata = collections.get_posts_removal_collection().insert(novenkiy, overwrite_mode='update')
logging.debug('Успешно добавлен пост на удаление')
2024-01-23 15:12:59 +10:00
except Exception as e2:
logging.error('ошибка DB :: добавление нового поста на удаление')
logging.error(e2)
2024-01-23 15:12:59 +10:00
2024-01-23 15:29:34 +10:00
def get_posts_to_be_removed(chat_id, type=None, max_id=None):
2024-01-23 15:12:59 +10:00
# собираем
try:
#posts = [p for p in collections.get_posts_removal_collection().all( ) ]
if type:
posts = [p for p in collections.get_posts_removal_collection().find({'chat_id': chat_id, 'type': type}, skip=0, limit=1100) ]
else:
posts = [p for p in collections.get_posts_removal_collection().find({'chat_id': chat_id}, skip=0, limit=1100) ]
2024-01-23 15:35:41 +10:00
posts_ret = [ p for p in posts ]
# # for p in posts:
# # last_time = datetime.datetime.strptime(p['datetimes'], '%Y-%m-%d %H:%M:%S')
# # timediff = (datetime.datetime.now() - last_time).total_seconds()
2024-01-23 15:12:59 +10:00
2024-01-23 15:35:41 +10:00
# # if timediff > 60:
# # posts_ret.append(p)
2024-01-23 15:29:34 +10:00
if max_id:
2024-01-23 15:31:23 +10:00
posts_ret = [ p for p in posts_ret if p['msg_id'] != max_id]
2024-01-23 15:29:34 +10:00
else:
posts_ret = [ p for p in posts_ret if p['msg_id'] != max([pp['msg_id'] for pp in posts_ret])]
2024-01-23 15:12:59 +10:00
2024-01-23 15:35:41 +10:00
#### TODO удалять все предыдущие без учёта времени
2024-01-23 15:12:59 +10:00
return posts_ret
except Exception as e:
logging.error('ошибка DB :: получение постов на удаление')
logging.error(e)
2024-01-23 15:12:59 +10:00
def del_post(msg_id, chat_id):
# удаляем из базы
try:
candidate_cursor = collections.get_posts_removal_collection().find(
{
'msg_id': msg_id,
'chat_id': chat_id,
},
skip = 0,
limit = 1488
)
if candidate_cursor.count() > 0:
pp = candidate_cursor.pop()
else:
pp = None
collections.get_posts_removal_collection().delete(pp)
except Exception as e:
logging.error('ошибка DB :: удаление поста на удаление')
logging.error(e)