welcomecenterbot/utils/store.py

41 lines
1.1 KiB
Python
Raw Normal View History

2024-09-26 11:07:00 +00:00
from bot.config import REDIS_URL
2024-09-27 05:26:37 +00:00
import redis.asyncio as aredis
2024-09-26 17:39:35 +00:00
import logging
# Create a logger instance
logger = logging.getLogger('store')
logging.basicConfig(level=logging.DEBUG)
2024-09-26 11:07:00 +00:00
# Connect to Redis
2024-09-27 05:26:37 +00:00
redis = aredis.Redis.from_url(REDIS_URL)
2024-09-26 11:07:00 +00:00
async def get_all_removed(uid):
2024-09-26 12:20:22 +00:00
pattern = f"removed:{uid}:*"
2024-09-26 11:07:00 +00:00
# Create a dictionary to hold the keys and values
texts = []
# Use scan_iter to find all keys matching the pattern
2024-09-26 14:47:20 +00:00
async for key in redis.scan_iter(pattern):
2024-09-26 11:07:00 +00:00
# Fetch the value for each key
value = await redis.get(key)
if value:
2024-09-26 19:45:22 +00:00
texts.append(value.decode('utf-8'))
2024-09-26 11:07:00 +00:00
return texts
2024-09-26 17:28:16 +00:00
async def get_average_toxic(msg):
uid = msg['from']['id']
cid = msg['chat']['id']
pattern = f"toxic:{cid}:{uid}:*"
scores = []
scoring_msg_id = 0
async for key in redis.scan_iter(pattern):
scr = await redis.get(key)
if isinstance(scr, int):
scores.append(scr)
logger.debug(f'found {len(scores)} messages')
toxic_score = math.floor(sum(scores)/len(scores)) if scores else 0
return toxic_score