聊天记录统计功能

This commit is contained in:
liuwei
2025-02-05 12:43:51 +08:00
parent b059aaa160
commit 64830f958c
5 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
import redis
from datetime import datetime
import re
# 连接到Redis
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
def process_message(message):
# 示例message字符串
# 使用正则表达式提取用户名称和聊天室标识
user_info_regex = re.compile(r"(\w+)\[([^\]]+)\]")
match = user_info_regex.search(message)
if match:
user_name = match.group(1)
chatroom_id = match.group(2)
print(f"用户名称: {user_name}")
print(f"聊天室标识: {chatroom_id}")
current_date = datetime.now().strftime('%Y-%m-%d')
# 生成Redis key
key = f"{chatroom_id}:{user_name}:{current_date}:count"
# 使用Redis哈希或字符串增加发言次数
print(key)
r.hincrby(key, 'count', 1) # 这里使用哈希但也可以考虑用字符串的INCR操作
# 或者使用字符串r.incr(key) # 如果只存储一个整数值,字符串类型可能更简单
else:
print("Failed to extract user name or chatroom ID.")