From c578851d36eb3729bdea58e446847a37a9c9d102 Mon Sep 17 00:00:00 2001 From: xaoyaoo Date: Tue, 6 Aug 2024 01:20:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A5=E6=94=B6=E3=80=81?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=E6=95=B0=E9=87=8F=E7=9A=84?= =?UTF-8?q?=E5=9B=BE=E8=A1=A8=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pywxdump/api/remote_server.py | 12 +++++------ pywxdump/db/dbMSG.py | 38 ++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/pywxdump/api/remote_server.py b/pywxdump/api/remote_server.py index a7d132c..b121747 100644 --- a/pywxdump/api/remote_server.py +++ b/pywxdump/api/remote_server.py @@ -456,17 +456,17 @@ def get_date_count(): 获取日期统计 :return: """ - if request.method == "GET": - word = request.args.get("wxid", "") - elif request.method == "POST": - word = request.json.get("wxid", "") - else: + if request.method not in ["GET", "POST"]: return ReJson(1003, msg="Unsupported method") + rq_data = request.json if request.method == "POST" else request.args + word = rq_data.get("wxid", "") + start_time = rq_data.get("start_time", 0) + end_time = rq_data.get("end_time", 0) my_wxid = get_conf(g.caf, g.at, "last") if not my_wxid: return ReJson(1001, body="my_wxid is required") db_config = get_conf(g.caf, my_wxid, "db_config") - date_count = DBHandler(db_config).get_date_count(word) + date_count = DBHandler(db_config).get_date_count(wxid=word, start_time=start_time, end_time=end_time) return ReJson(0, date_count) diff --git a/pywxdump/db/dbMSG.py b/pywxdump/db/dbMSG.py index 2ab6011..2867b20 100644 --- a/pywxdump/db/dbMSG.py +++ b/pywxdump/db/dbMSG.py @@ -291,10 +291,15 @@ class MsgHandler(DatabaseBase): return rdata, list(wxid_list) @db_error - def get_date_count(self, wxid=''): + def get_date_count(self, wxid='', start_time: int = 0, end_time: int = 0): """ 获取每日聊天记录数量,包括发送者数量、接收者数量和总数。 """ + if start_time and end_time and isinstance(start_time, str) \ + and isinstance(end_time, str) and start_time.isdigit() and end_time.isdigit(): + start_time = int(start_time) + end_time = int(end_time) + sql_base = ( "SELECT strftime('%Y-%m-%d', CreateTime, 'unixepoch', 'localtime') AS date, " " SUM(CASE WHEN IsSender = 1 THEN 1 ELSE 0 END) AS sender_count, " @@ -306,9 +311,12 @@ class MsgHandler(DatabaseBase): params = () sql_wxid = "AND StrTalker = ? " if wxid else "" - params += (wxid,) if wxid else params + params += params + (wxid,) if wxid else params - sql = f"{sql_base} {sql_wxid} GROUP BY date ORDER BY date ASC;" + sql_time = "AND CreateTime BETWEEN ? AND ? " if start_time and end_time else "" + params += params + (start_time, end_time) if start_time and end_time else params + + sql = f"{sql_base} {sql_wxid} {sql_time} GROUP BY date ORDER BY date ASC;" result = self.execute(sql, params) if not result: @@ -324,6 +332,30 @@ class MsgHandler(DatabaseBase): } return result_dict + @db_error + def get_top_talker_count(self, top: int = 10, start_time: int = 0, end_time: int = 0): + """ + 获取聊天记录数量最多的联系人,他们每天的聊天记录数量 + """ + if start_time and end_time and isinstance(start_time, str) \ + and isinstance(end_time, str) and start_time.isdigit() and end_time.isdigit(): + start_time = int(start_time) + end_time = int(end_time) + sql = ( + "SELECT StrTalker, COUNT(*) AS count " + "FROM MSG " + f"WHERE StrTalker NOT LIKE '%chatroom%' AND CreateTime BETWEEN {start_time} AND {end_time} " + "GROUP BY StrTalker " + "ORDER BY count DESC " + f"LIMIT {top};" + ) + result = self.execute(sql) + if not result: + return {} + # 将查询结果转换为字典 + result_dict = {row[0]: row[1] for row in result} + return result_dict + @db_error def decompress_CompressContent(data):