forked from Pavel_Durov/python_bot
#5 удаляем!
This commit is contained in:
@@ -7,12 +7,32 @@ from arango import ArangoClient
|
||||
|
||||
|
||||
def get_dicks_collection():
|
||||
arango_client = ArangoClient(hosts=CONFIG['databaso']['host'] )
|
||||
pipisa_db = arango_client.db(
|
||||
CONFIG['databaso']['base'],
|
||||
username=CONFIG['databaso']['user'],
|
||||
password=CONFIG['databaso']['pass']
|
||||
)
|
||||
dicks_collection = pipisa_db.collection(CONFIG['databaso']['collection'])
|
||||
try:
|
||||
arango_client = ArangoClient(hosts=CONFIG['databaso']['host'] )
|
||||
pipisa_db = arango_client.db(
|
||||
CONFIG['databaso']['base'],
|
||||
username=CONFIG['databaso']['user'],
|
||||
password=CONFIG['databaso']['pass']
|
||||
)
|
||||
dicks_collection = pipisa_db.collection(CONFIG['databaso']['collection'])
|
||||
|
||||
return dicks_collection
|
||||
return dicks_collection
|
||||
except Exception as e:
|
||||
print('ошибка DB при взятии коллекции пипис')
|
||||
print(e)
|
||||
|
||||
|
||||
def get_posts_removal_collection():
|
||||
try:
|
||||
arango_client = ArangoClient(hosts=CONFIG['databaso']['host'] )
|
||||
pipisa_db = arango_client.db(
|
||||
CONFIG['databaso']['base'],
|
||||
username=CONFIG['databaso']['user'],
|
||||
password=CONFIG['databaso']['pass']
|
||||
)
|
||||
posts_removal_collection = pipisa_db.collection(CONFIG['databaso']['posts_removal_collection'])
|
||||
|
||||
return posts_removal_collection
|
||||
except Exception as e:
|
||||
print('ошибка DB при взятии коллекции постов-на-удаление')
|
||||
print(e)
|
||||
|
||||
71
db_logic/postcleaner.py
Normal file
71
db_logic/postcleaner.py
Normal file
@@ -0,0 +1,71 @@
|
||||
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)
|
||||
|
||||
|
||||
def get_posts_to_be_removed(chat_id, type=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 = []
|
||||
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)
|
||||
|
||||
print(posts_ret)
|
||||
posts_ret = [ p for p in posts_ret if p['msg_id'] != max([pp['msg_id'] for pp in posts_ret])]
|
||||
### оставлять последний пост
|
||||
|
||||
|
||||
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)
|
||||
@@ -1,6 +1,4 @@
|
||||
from global_conf import CONFIG
|
||||
from db_logic import collections
|
||||
|
||||
import datetime
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user