签到功能初始化
消息计数定时任务入库
This commit is contained in:
@@ -3,9 +3,11 @@ import time
|
||||
import sqlite3
|
||||
from datetime import datetime, timedelta
|
||||
import redis
|
||||
|
||||
# 连接到Redis
|
||||
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
|
||||
|
||||
|
||||
def write_to_db():
|
||||
# 连接到SQLite数据库
|
||||
conn = sqlite3.connect('message_stats.db')
|
||||
@@ -25,7 +27,7 @@ def write_to_db():
|
||||
|
||||
# 遍历Redis中所有与昨天日期相关的key,并写入数据库
|
||||
for key in r.keys(f"*:*:{yesterday}:count"):
|
||||
print('user key:'+ key)
|
||||
print('user key:' + key)
|
||||
parts = key.decode('utf-8').split(':')
|
||||
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或空值情况
|
||||
@@ -38,7 +40,7 @@ def write_to_db():
|
||||
conn.close()
|
||||
|
||||
|
||||
def generate_and_send_ranking(groupId,allContacts: dict):
|
||||
def generate_and_send_ranking(groupId, allContacts: dict):
|
||||
# 连接到SQLite数据库(假设数据库文件名为database.db)
|
||||
conn = sqlite3.connect('message_stats.db')
|
||||
cursor = conn.cursor()
|
||||
@@ -56,19 +58,16 @@ def generate_and_send_ranking(groupId,allContacts: dict):
|
||||
"""
|
||||
|
||||
# 执行查询并获取结果
|
||||
cursor.execute(query,(groupId,))
|
||||
cursor.execute(query, (groupId,))
|
||||
results = cursor.fetchall()
|
||||
|
||||
# 格式化输出字符串
|
||||
ranking_str = yesterday + "发言数量前20的用户排名:\n"
|
||||
for rank, (username, speech_count) in enumerate(results, start=1):
|
||||
ranking_str += f"{rank}. {allContacts[username]}: {speech_count} 次发言\n"
|
||||
|
||||
ranking_str += f"{rank}. {allContacts[username]}: {speech_count} 次发言\n"
|
||||
# 关闭数据库连接
|
||||
conn.close()
|
||||
|
||||
# 这里我们没有实际“发送”排名,只是返回字符串
|
||||
# 如果需要发送,可以在此添加发送逻辑
|
||||
return ranking_str
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user