From c40db420a10bc250d220f74d536079b3a8d9365b Mon Sep 17 00:00:00 2001 From: DzhiWang <2429634486@qq.com> Date: Wed, 20 Dec 2023 23:11:41 +0800 Subject: [PATCH] delete redundant parameters --- app/DataBase/msg.py | 241 +++++++++++++-------------------------- app/analysis/analysis.py | 50 ++++---- 2 files changed, 104 insertions(+), 187 deletions(-) diff --git a/app/DataBase/msg.py b/app/DataBase/msg.py index 90cf2fa..74a70d9 100644 --- a/app/DataBase/msg.py +++ b/app/DataBase/msg.py @@ -124,26 +124,18 @@ class Msg: lock.release() return result[0] - def get_messages_length_with_ta(self, username_, is_Annual_report_=False, year_='2023'): - if is_Annual_report_: - sql = ''' - select count(*) - from MSG - WHERE StrTalker = ? AND strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ? - ''' - else: - sql = ''' + def get_messages_length_with_ta(self, username_, year_='all'): + sql = f''' select count(*) from MSG + WHERE StrTalker = ? + {"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""} ''' if not self.open_flag: return None try: lock.acquire(True) - if is_Annual_report_: - self.cursor.execute(sql, [username_, year_]) - else: - self.cursor.execute(sql, [username_]) + self.cursor.execute(sql, [username_, year_] if year_ != "all" else [username_]) result = self.cursor.fetchone() except Exception as e: result = None @@ -173,29 +165,20 @@ class Msg: # result.sort(key=lambda x: x[5]) return result - def get_messages_by_type(self, username_, type_, is_Annual_report_=False, year_='2023'): + def get_messages_by_type(self, username_, type_, year_='all'): if not self.open_flag: return None - if is_Annual_report_: - sql = ''' - select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime,MsgSvrID,BytesExtra,CompressContent - from MSG - where StrTalker=? and Type=? and strftime('%Y',CreateTime,'unixepoch','localtime') = ? - order by CreateTime - ''' - else: - sql = ''' + + sql = f''' select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime,MsgSvrID,BytesExtra,CompressContent from MSG - where StrTalker=? and Type=? + where StrTalker=? and Type=? + {"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""} order by CreateTime ''' try: lock.acquire(True) - if is_Annual_report_: - self.cursor.execute(sql, [username_, type_, year_]) - else: - self.cursor.execute(sql, [username_, type_]) + self.cursor.execute(sql, [username_, type_, year_]if year_ != "all" else [username_, type_]) result = self.cursor.fetchall() finally: lock.release() @@ -270,71 +253,46 @@ class Msg: contacts.sort(key=lambda cur_contact: cur_contact[-1], reverse=True) return contacts - def get_messages_by_days(self, username_, is_Annual_report_=False, year_='2023'): - if is_Annual_report_: - sql = ''' - SELECT strftime('%Y-%m-%d',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID) - from ( - SELECT MsgSvrID, CreateTime - FROM MSG - WHERE StrTalker = ? AND strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ? - ) - group by days - ''' - else: - sql = ''' - SELECT strftime('%Y-%m-%d',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID) - from ( - SELECT MsgSvrID, CreateTime - FROM MSG - WHERE StrTalker = ? - ) - group by days - ''' + def get_messages_by_days(self, username_, year_='all'): + sql = f''' + SELECT strftime('%Y-%m-%d',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID) + from ( + SELECT MsgSvrID, CreateTime + FROM MSG + WHERE StrTalker = ? + {"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""} + ) + group by days + ''' + result = None if not self.open_flag: return None try: lock.acquire(True) - if is_Annual_report_: - self.cursor.execute(sql, [username_, year_]) - else: - self.cursor.execute(sql, [username_]) + self.cursor.execute(sql, [username_, year_]if year_ != "all" else [username_]) result = self.cursor.fetchall() finally: lock.release() return result - def get_messages_by_month(self, username_, is_Annual_report_=False, year_='2023'): - if is_Annual_report_: - sql = ''' - SELECT strftime('%Y-%m',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID) - from ( - SELECT MsgSvrID, CreateTime - FROM MSG - WHERE StrTalker = ? AND strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ? - ) - group by days - ''' - else: - sql = ''' - SELECT strftime('%Y-%m',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID) - from ( - SELECT MsgSvrID, CreateTime - FROM MSG - WHERE StrTalker = ? - ) - group by days + def get_messages_by_month(self, username_, year_='all'): + sql = f''' + SELECT strftime('%Y-%m',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID) + from ( + SELECT MsgSvrID, CreateTime + FROM MSG + WHERE StrTalker = ? + {"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""} + ) + group by days ''' result = None if not self.open_flag: return None try: lock.acquire(True) - if is_Annual_report_: - self.cursor.execute(sql, [username_, year_]) - else: - self.cursor.execute(sql, [username_]) + self.cursor.execute(sql, [username_, year_] if year_ != "all" else [username_]) result = self.cursor.fetchall() except sqlite3.DatabaseError: logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试') @@ -343,36 +301,23 @@ class Msg: # result.sort(key=lambda x: x[5]) return result - def get_messages_by_hour(self, username_, is_Annual_report_=False, year_='2023'): - if is_Annual_report_: - sql = ''' - SELECT strftime('%H:00',CreateTime,'unixepoch','localtime') as hours,count(MsgSvrID) - from ( - SELECT MsgSvrID, CreateTime - FROM MSG - where StrTalker = ? and strftime('%Y',CreateTime,'unixepoch','localtime') = ? - ) - group by hours - ''' - else: - sql = ''' - SELECT strftime('%H:00',CreateTime,'unixepoch','localtime') as hours,count(MsgSvrID) - from ( - SELECT MsgSvrID, CreateTime - FROM MSG - where StrTalker = ? - ) - group by hours + def get_messages_by_hour(self, username_, year_='all'): + sql = f''' + SELECT strftime('%H:00',CreateTime,'unixepoch','localtime') as hours,count(MsgSvrID) + from ( + SELECT MsgSvrID, CreateTime + FROM MSG + where StrTalker = ? + {"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""} + ) + group by hours ''' result = None if not self.open_flag: return None try: lock.acquire(True) - if is_Annual_report_: - self.cursor.execute(sql, [username_, year_]) - else: - self.cursor.execute(sql, [username_]) + self.cursor.execute(sql, [username_, year_] if year_ != "all" else [username_]) result = self.cursor.fetchall() except sqlite3.DatabaseError: logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试') @@ -381,47 +326,29 @@ class Msg: # result.sort(key=lambda x: x[5]) return result - def get_lateDay_messages(self, username_, is_Annual_report_=False, year_='2023'): - if is_Annual_report_: - sql = ''' - SELECT strftime('%Y-%m-%d %H:%M:%S', CreateTime, 'unixepoch', 'localtime') as time, IsSender, Status, StrContent - from( - SELECT CreateTime, strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') as time, IsSender, Status, StrContent - FROM MSG - WHERE StrTalker = ? AND strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ? - AND (strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') > ? - or strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') < ? ) AND Type=1 - ORDER BY CASE - WHEN strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') < ? THEN 0 - ELSE 1 - END, time DESC - LIMIT 4 - ) - ''' - else: - sql = ''' - SELECT strftime('%Y-%m-%d %H:%M:%S', CreateTime, 'unixepoch', 'localtime') as time, IsSender, Status, StrContent - from( - SELECT CreateTime, strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') as time, IsSender, Status, StrContent - FROM MSG - WHERE StrTalker = ? AND (strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') > ? - or strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') < ? ) AND Type=1 - ORDER BY CASE - WHEN strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') < ? THEN 0 - ELSE 1 - END, time DESC - LIMIT 4 - ) + def get_lateDay_messages(self, username_, year_='all'): + sql = f''' + SELECT strftime('%Y-%m-%d %H:%M:%S', CreateTime, 'unixepoch', 'localtime') as Strtime, IsSender, Status, StrContent + from( + SELECT CreateTime, strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') as time, IsSender, Status, StrContent + FROM MSG + WHERE StrTalker = ? + {"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""} + AND (strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') > ? + or strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') < ? ) AND Type=1 + ORDER BY CASE + WHEN strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') < ? THEN 0 + ELSE 1 + END, time DESC + LIMIT 4 + ) ''' result = None if not self.open_flag: return None try: lock.acquire(True) - if is_Annual_report_: - self.cursor.execute(sql, [username_, year_, '21:00:00', '05:00:00', '05:00:00']) - else: - self.cursor.execute(sql, [username_, '21:00:00', '05:00:00', '05:00:00']) + self.cursor.execute(sql, [username_, year_, '21:00:00', '05:00:00', '05:00:00'] if year_ != "all" else [username_, '21:00:00', '05:00:00', '05:00:00']) result = self.cursor.fetchall() except sqlite3.DatabaseError: logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试') @@ -430,39 +357,25 @@ class Msg: # result.sort(key=lambda x: x[5]) return result - def get_earlyDay_messages(self, username_, is_Annual_report_=False, year_='2023'): - if is_Annual_report_: - sql = ''' - SELECT strftime('%Y-%m-%d %H:%M:%S', CreateTime, 'unixepoch', 'localtime') as strtime, IsSender, Status, StrContent - from ( - SELECT CreateTime, strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') as time, IsSender, Status, StrContent - FROM MSG - WHERE StrTalker = ? AND strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ? - AND strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') > ? AND Type=1 - ORDER BY time - LIMIT 4 - ) - ''' - else: - sql = ''' - SELECT strftime('%Y-%m-%d %H:%M:%S', CreateTime, 'unixepoch', 'localtime') as strtime, IsSender, Status, StrContent - from ( - SELECT CreateTime, strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') as time, IsSender, Status, StrContent - FROM MSG - WHERE StrTalker = ? AND strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') > ? AND Type=1 - ORDER BY time - LIMIT 4 - ) - ''' + def get_earlyDay_messages(self, username_, year_='all'): + sql = f''' + SELECT strftime('%Y-%m-%d %H:%M:%S', CreateTime, 'unixepoch', 'localtime') as strtime, IsSender, Status, StrContent + from ( + SELECT CreateTime, strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') as time, IsSender, Status, StrContent + FROM MSG + WHERE StrTalker = ? + {"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""} + AND strftime('%H:%M:%S', CreateTime, 'unixepoch', 'localtime') > ? AND Type=1 + ORDER BY time + LIMIT 4 + ) + ''' result = None if not self.open_flag: return None try: lock.acquire(True) - if is_Annual_report_: - self.cursor.execute(sql, [username_, year_, '05:00:00']) - else: - self.cursor.execute(sql, [username_, '05:00:00']) + self.cursor.execute(sql, [username_, year_, '05:00:00'] if year_ != "all" else [username_, '05:00:00']) result = self.cursor.fetchall() except sqlite3.DatabaseError: logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试') diff --git a/app/analysis/analysis.py b/app/analysis/analysis.py index e635155..faab225 100644 --- a/app/analysis/analysis.py +++ b/app/analysis/analysis.py @@ -21,9 +21,9 @@ wordcloud_width = 780 wordcloud_height = 720 -def wordcloud(wxid, is_Annual_report=False, year='2023', who='1'): +def wordcloud(wxid, year='all', who='1'): import jieba - txt_messages = msg_db.get_messages_by_type(wxid, MsgType.TEXT, is_Annual_report, year) + txt_messages = msg_db.get_messages_by_type(wxid, MsgType.TEXT, year) if not txt_messages: return { 'chart_data': None, @@ -75,10 +75,10 @@ def wordcloud(wxid, is_Annual_report=False, year='2023', who='1'): } -def calendar_chart(wxid, is_Annual_report=False, year='2023'): - data_length = msg_db.get_messages_length_with_ta(wxid, is_Annual_report, year) # 获取和他的聊天条数 +def calendar_chart(wxid, year='all'): + data_length = msg_db.get_messages_length_with_ta(wxid, year) # 获取和他的聊天条数 print(f'聊天总数:{data_length}') - calendar_data = msg_db.get_messages_by_days(wxid, is_Annual_report, year) + calendar_data = msg_db.get_messages_by_days(wxid, year) if not calendar_data: return False @@ -99,7 +99,7 @@ def calendar_chart(wxid, is_Annual_report=False, year='2023'): date_num = (date2 - date1).days + 1 print(date_num) - if is_Annual_report: + if year != 'all': calendar_days = year calendar_title = f'{year}年聊天情况' else: @@ -127,7 +127,7 @@ def calendar_chart(wxid, is_Annual_report=False, year='2023'): ) ) return { - 'chart_data': c, + 'chart_data': c.dump_options_with_quotes(), 'data_length': data_length, # 和xx的聊天记录总数 'max_date': formatted_date, 'max_num': str(max_), @@ -136,19 +136,23 @@ def calendar_chart(wxid, is_Annual_report=False, year='2023'): } -def month_count(wxid, is_Annual_report=False, year='2023'): +def month_count(wxid, year='all'): """ 每月聊天条数 """ - msg_data = msg_db.get_messages_by_month(wxid, is_Annual_report, year) + msg_data = msg_db.get_messages_by_month(wxid, year) y_data = list(map(lambda x: x[1], msg_data)) x_axis = list(map(lambda x: x[0], msg_data)) # 获取聊天的月数 - if all(y > 0 for y in y_data): - conc = "我们这一年每个月都有在聊天" + if year != 'all': + if all(y > 0 for y in y_data): + conc = "我们这一年每个月都有在聊天" + else: + months_with_chat = sum(1 for y in y_data if y > 0) + conc = f"我们这一年有{months_with_chat}个月都在聊天" else: months_with_chat = sum(1 for y in y_data if y > 0) - conc = f"我们这一年有{months_with_chat}个月都在聊天" + conc = f"我们有{months_with_chat}个月都在聊天" print("聊天月数", conc) # 月平均聊天条数 average_num = round(sum(y_data)/12) @@ -194,11 +198,11 @@ def month_count(wxid, is_Annual_report=False, year='2023'): } -def hour_count(wxid, is_Annual_report=False, year='2023'): +def hour_count(wxid, year='all'): """ 小时计数聊天条数 """ - msg_data = msg_db.get_messages_by_hour(wxid, is_Annual_report, year) + msg_data = msg_db.get_messages_by_hour(wxid, year) print(msg_data) y_data = list(map(lambda x: x[1], msg_data)) x_axis = list(map(lambda x: x[0], msg_data)) @@ -232,8 +236,8 @@ def hour_count(wxid, is_Annual_report=False, year='2023'): ) ) ) - late_data = msg_db.get_lateDay_messages(wxid, is_Annual_report, year) # 最晚的消息记录 - early_data = msg_db.get_earlyDay_messages(wxid, is_Annual_report, year) # 早上最早的记录 + late_data = msg_db.get_lateDay_messages(wxid, year) # 最晚的消息记录 + early_data = msg_db.get_earlyDay_messages(wxid, year) # 早上最早的记录 print(late_data) print(early_data) return { @@ -245,9 +249,9 @@ def hour_count(wxid, is_Annual_report=False, year='2023'): } -def emoji_count(wxid, is_Annual_report=False, year='2023'): +def emoji_count(wxid, year='all'): # 最常发的表情 - txt_messages = msg_db.get_messages_by_type(wxid, MsgType.TEXT, is_Annual_report, year) + txt_messages = msg_db.get_messages_by_type(wxid, MsgType.TEXT, year) me_txt_messages = ''.join(map(lambda x: x[7] if x[4] == 1 else '', txt_messages)) ta_txt_messages = ''.join(map(lambda x: x[7] if x[4] == 0 else '', txt_messages)) @@ -296,16 +300,16 @@ class Analysis: if __name__ == '__main__': msg_db.init_database(path='../DataBase/Msg/MSG.db') # w = wordcloud('wxid_0o18ef858vnu22') - # w_data = wordcloud('wxid_27hqbq7vx5hf22', True, '2023') + w_data = wordcloud('wxid_27hqbq7vx5hf22', '2023') # # print(w_data) # # w['chart_data'].render("./data/聊天统计/wordcloud.html") - # c = calendar_chart('wxid_27hqbq7vx5hf22', False, '2023') + c = calendar_chart('wxid_27hqbq7vx5hf22', '2023') # c['chart_data'].render("./data/聊天统计/calendar.html") # # print('c:::', c) - # m = month_count('wxid_27hqbq7vx5hf22', False, '2023') + m = month_count('wxid_27hqbq7vx5hf22', False, '2023') # m['chart_data'].render("./data/聊天统计/month_num.html") - # h = hour_count('wxid_27hqbq7vx5hf22',is_Annual_report=False) + # h = hour_count('wxid_27hqbq7vx5hf22') # h['chart_data'].render("./data/聊天统计/hour_count.html") - h = emoji_count('wxid_27hqbq7vx5hf22',is_Annual_report=False) + h = emoji_count('wxid_27hqbq7vx5hf22') # h['chart_data'].render("./data/聊天统计/hour_count.html")