python_bot/db_logic/postcleaner.py

73 lines
2.5 KiB
Python
Raw Normal View History

2024-01-23 15:12:59 +10:00
from db_logic import collections
import datetime
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')
print(f'Успешно добавлен пост на удаление')
except Exception as e2:
print(f'ошибка DB :: добавление нового поста на удаление')
print(e2)
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) ]
posts_ret = []
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()
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
### оставлять последний пост
return posts_ret
except Exception as e:
print('ошибка DB :: получение постов на удаление')
print(e)
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:
print('ошибка DB :: удаление поста на удаление')
print(e)