message
This commit is contained in:
4
main.py
4
main.py
@@ -57,6 +57,10 @@ def main(chat_type: int):
|
|||||||
# epic
|
# epic
|
||||||
robot.onEveryTime("10:30", robot.sendEpicFreeGames)
|
robot.onEveryTime("10:30", robot.sendEpicFreeGames)
|
||||||
|
|
||||||
|
|
||||||
|
# message report
|
||||||
|
robot.onEveryTime("00:30", robot.messageCountToDB)
|
||||||
|
|
||||||
# 让机器人一直跑
|
# 让机器人一直跑
|
||||||
robot.keepRunningAndBlockProcess()
|
robot.keepRunningAndBlockProcess()
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,20 @@
|
|||||||
import redis
|
import redis
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from wcferry import WxMsg
|
||||||
|
|
||||||
# 连接到Redis
|
# 连接到Redis
|
||||||
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
|
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
|
||||||
|
|
||||||
|
|
||||||
def process_message(message):
|
def process_message(message:WxMsg):
|
||||||
# 示例message字符串
|
# 示例message字符串
|
||||||
|
current_date = datetime.now().strftime('%Y-%m-%d')
|
||||||
|
# 生成Redis key
|
||||||
|
key = f"{message.roomid}:{message.sender}:{current_date}:count"
|
||||||
|
# 使用Redis哈希(或字符串)增加发言次数
|
||||||
|
r.hincrby(key, 'count', 1) # 这里使用哈希,但也可以考虑用字符串的INCR操作
|
||||||
|
# 或者使用字符串:r.incr(key) # 如果只存储一个整数值,字符串类型可能更简单
|
||||||
|
|
||||||
# 使用正则表达式提取用户名称和聊天室标识
|
|
||||||
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.")
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ def write_to_db():
|
|||||||
|
|
||||||
# 遍历Redis中所有与昨天日期相关的key,并写入数据库
|
# 遍历Redis中所有与昨天日期相关的key,并写入数据库
|
||||||
for key in r.keys(f"*:*:{yesterday}:count"):
|
for key in r.keys(f"*:*:{yesterday}:count"):
|
||||||
|
print('user key:{key}')
|
||||||
parts = key.decode('utf-8').split(':')
|
parts = key.decode('utf-8').split(':')
|
||||||
group_id, wx_id, _date = parts[0], parts[1], parts[2] # _date应该是yesterday,但这里为了完整性还是保留
|
group_id, wx_id, _date = parts[0], parts[1], parts[2] # _date应该是yesterday,但这里为了完整性还是保留
|
||||||
count = int(r.hget(key, 'count')) if isinstance(r.hget(key, 'count'), bytes) else 0 # 处理可能的None或空值情况
|
count = int(r.hget(key, 'count')) if isinstance(r.hget(key, 'count'), bytes) else 0 # 处理可能的None或空值情况
|
||||||
@@ -39,7 +40,7 @@ def write_to_db():
|
|||||||
|
|
||||||
def generate_and_send_ranking(groupId,allContacts: dict):
|
def generate_and_send_ranking(groupId,allContacts: dict):
|
||||||
# 连接到SQLite数据库(假设数据库文件名为database.db)
|
# 连接到SQLite数据库(假设数据库文件名为database.db)
|
||||||
conn = sqlite3.connect('database.db')
|
conn = sqlite3.connect('message_stats.db')
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# 编写SQL查询来获取发言数量前20的用户
|
# 编写SQL查询来获取发言数量前20的用户
|
||||||
|
|||||||
@@ -20,3 +20,4 @@ protobuf~=5.29.1
|
|||||||
beautifulsoup4~=4.12.3
|
beautifulsoup4~=4.12.3
|
||||||
httpx~=0.28.1
|
httpx~=0.28.1
|
||||||
pyttsx3~=2.98
|
pyttsx3~=2.98
|
||||||
|
redis~=5.2.1
|
||||||
Reference in New Issue
Block a user