aredis+
This commit is contained in:
41
utils/store.py
Normal file
41
utils/store.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from bot.config import REDIS_URL
|
||||
from redis import asyncio as aredis
|
||||
import logging
|
||||
|
||||
# Create a logger instance
|
||||
logger = logging.getLogger('store')
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
# Connect to Redis
|
||||
redis = aredis.Redis.from_url(REDIS_URL)
|
||||
|
||||
|
||||
async def get_all_removed(uid):
|
||||
pattern = f"removed:{uid}:*"
|
||||
|
||||
# Create a dictionary to hold the keys and values
|
||||
texts = []
|
||||
|
||||
# Use scan_iter to find all keys matching the pattern
|
||||
async for key in redis.scan_iter(pattern):
|
||||
# Fetch the value for each key
|
||||
value = await redis.get(key)
|
||||
if value:
|
||||
texts.append(value.decode('utf-8'))
|
||||
|
||||
return texts
|
||||
|
||||
|
||||
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
|
Reference in New Issue
Block a user