29 lines
1.0 KiB
Python
29 lines
1.0 KiB
Python
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.")
|
||
|