forked from Pavel_Durov/python_bot
75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
from db_logic import collections
|
|
import datetime, logging
|
|
|
|
|
|
|
|
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('Успешно добавлен пост на удаление')
|
|
except Exception as e2:
|
|
logging.error('ошибка DB :: добавление нового поста на удаление')
|
|
logging.error(e2)
|
|
|
|
|
|
def get_posts_to_be_removed(chat_id, type=None, max_id=None):
|
|
# собираем
|
|
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 = [ 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()
|
|
|
|
# # if timediff > 60:
|
|
# # posts_ret.append(p)
|
|
|
|
if max_id:
|
|
posts_ret = [ p for p in posts_ret if p['msg_id'] != max_id]
|
|
else:
|
|
posts_ret = [ p for p in posts_ret if p['msg_id'] != max([pp['msg_id'] for pp in posts_ret])]
|
|
|
|
|
|
#### TODO удалять все предыдущие без учёта времени
|
|
|
|
return posts_ret
|
|
except Exception as e:
|
|
logging.error('ошибка DB :: получение постов на удаление')
|
|
logging.error(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:
|
|
logging.error('ошибка DB :: удаление поста на удаление')
|
|
logging.error(e) |