mirror of
https://github.com/LC044/WeChatMsg
synced 2025-02-22 19:02:17 +08:00
Merge pull request #109 from LC044/aaaz
add is_annual_report in all analysis
This commit is contained in:
commit
f43faec662
@ -126,10 +126,18 @@ class Msg:
|
|||||||
# result.sort(key=lambda x: x[5])
|
# result.sort(key=lambda x: x[5])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_messages_by_type(self, username_, type_):
|
def get_messages_by_type(self, username_, type_, is_Annual_report_=False, year_='2023'):
|
||||||
if not self.open_flag:
|
if not self.open_flag:
|
||||||
return None
|
return None
|
||||||
sql = '''
|
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
|
||||||
|
from MSG
|
||||||
|
where StrTalker=? and Type=? and strftime('%Y',CreateTime,'unixepoch','localtime') = ?
|
||||||
|
order by CreateTime
|
||||||
|
'''
|
||||||
|
else:
|
||||||
|
sql = '''
|
||||||
select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime,MsgSvrID,BytesExtra
|
select localId,TalkerId,Type,SubType,IsSender,CreateTime,Status,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime,MsgSvrID,BytesExtra
|
||||||
from MSG
|
from MSG
|
||||||
where StrTalker=? and Type=?
|
where StrTalker=? and Type=?
|
||||||
@ -137,7 +145,10 @@ class Msg:
|
|||||||
'''
|
'''
|
||||||
try:
|
try:
|
||||||
lock.acquire(True)
|
lock.acquire(True)
|
||||||
self.cursor.execute(sql, [username_, type_])
|
if is_Annual_report_:
|
||||||
|
self.cursor.execute(sql, [username_, type_, year_])
|
||||||
|
else:
|
||||||
|
self.cursor.execute(sql, [username_, type_])
|
||||||
result = self.cursor.fetchall()
|
result = self.cursor.fetchall()
|
||||||
finally:
|
finally:
|
||||||
lock.release()
|
lock.release()
|
||||||
@ -186,37 +197,59 @@ class Msg:
|
|||||||
))
|
))
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def get_messages_by_days(self, username_, year_='2023'):
|
def get_messages_by_days(self, username_, is_Annual_report_=False, year_='2023'):
|
||||||
sql = '''
|
if is_Annual_report_:
|
||||||
SELECT strftime('%Y-%m-%d',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID)
|
sql = '''
|
||||||
from MSG
|
SELECT strftime('%Y-%m-%d',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID)
|
||||||
where StrTalker = ? and strftime('%Y',CreateTime,'unixepoch','localtime') = ?
|
|
||||||
group by days
|
|
||||||
'''
|
|
||||||
result = None
|
|
||||||
if not self.open_flag:
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
lock.acquire(True)
|
|
||||||
self.cursor.execute(sql, [username_, year_])
|
|
||||||
result = self.cursor.fetchall()
|
|
||||||
finally:
|
|
||||||
lock.release()
|
|
||||||
return result
|
|
||||||
|
|
||||||
def get_messages_by_month(self, username_, year_='2023'):
|
|
||||||
sql = '''
|
|
||||||
SELECT strftime('%Y-%m',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID)
|
|
||||||
from MSG
|
from MSG
|
||||||
where StrTalker = ? and strftime('%Y',CreateTime,'unixepoch','localtime') = ?
|
where StrTalker = ? and strftime('%Y',CreateTime,'unixepoch','localtime') = ?
|
||||||
group by days
|
group by days
|
||||||
'''
|
'''
|
||||||
|
else:
|
||||||
|
sql = '''
|
||||||
|
SELECT strftime('%Y-%m-%d',CreateTime,'unixepoch','localtime') as days,count(MsgSvrID)
|
||||||
|
from MSG
|
||||||
|
where StrTalker = ?
|
||||||
|
group by days
|
||||||
|
'''
|
||||||
result = None
|
result = None
|
||||||
if not self.open_flag:
|
if not self.open_flag:
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
lock.acquire(True)
|
lock.acquire(True)
|
||||||
self.cursor.execute(sql, [username_, year_])
|
if is_Annual_report_:
|
||||||
|
self.cursor.execute(sql, [username_, year_])
|
||||||
|
else:
|
||||||
|
self.cursor.execute(sql, [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 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 MSG
|
||||||
|
where StrTalker = ?
|
||||||
|
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_])
|
||||||
result = self.cursor.fetchall()
|
result = self.cursor.fetchall()
|
||||||
except sqlite3.DatabaseError:
|
except sqlite3.DatabaseError:
|
||||||
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
||||||
@ -225,11 +258,19 @@ class Msg:
|
|||||||
# result.sort(key=lambda x: x[5])
|
# result.sort(key=lambda x: x[5])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_messages_by_hour(self, username_, year_='2023'):
|
def get_messages_by_hour(self, username_, is_Annual_report_=False, year_='2023'):
|
||||||
sql = '''
|
if is_Annual_report_:
|
||||||
|
sql = '''
|
||||||
|
SELECT strftime('%H:00',CreateTime,'unixepoch','localtime') as hours,count(MsgSvrID)
|
||||||
|
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)
|
SELECT strftime('%H:00',CreateTime,'unixepoch','localtime') as hours,count(MsgSvrID)
|
||||||
from MSG
|
from MSG
|
||||||
where StrTalker = ? and strftime('%Y',CreateTime,'unixepoch','localtime') = ?
|
where StrTalker = ?
|
||||||
group by hours
|
group by hours
|
||||||
'''
|
'''
|
||||||
result = None
|
result = None
|
||||||
@ -237,7 +278,10 @@ class Msg:
|
|||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
lock.acquire(True)
|
lock.acquire(True)
|
||||||
self.cursor.execute(sql, [username_, year_])
|
if is_Annual_report_:
|
||||||
|
self.cursor.execute(sql, [username_, year_])
|
||||||
|
else:
|
||||||
|
self.cursor.execute(sql, [username_])
|
||||||
result = self.cursor.fetchall()
|
result = self.cursor.fetchall()
|
||||||
except sqlite3.DatabaseError:
|
except sqlite3.DatabaseError:
|
||||||
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
||||||
|
@ -7,7 +7,7 @@ sys.path.append('.')
|
|||||||
|
|
||||||
from app.DataBase import msg_db, MsgType
|
from app.DataBase import msg_db, MsgType
|
||||||
from pyecharts import options as opts
|
from pyecharts import options as opts
|
||||||
from pyecharts.charts import WordCloud, Calendar, Bar
|
from pyecharts.charts import WordCloud, Calendar, Bar, Line
|
||||||
from app.resources import resource_rc
|
from app.resources import resource_rc
|
||||||
|
|
||||||
var = resource_rc.qt_resource_name
|
var = resource_rc.qt_resource_name
|
||||||
@ -17,9 +17,9 @@ wordcloud_width = 780
|
|||||||
wordcloud_height = 720
|
wordcloud_height = 720
|
||||||
|
|
||||||
|
|
||||||
def wordcloud(wxid):
|
def wordcloud(wxid, is_Annual_report=False, year='2023'):
|
||||||
import jieba
|
import jieba
|
||||||
txt_messages = msg_db.get_messages_by_type(wxid, MsgType.TEXT)
|
txt_messages = msg_db.get_messages_by_type(wxid, MsgType.TEXT, is_Annual_report, year)
|
||||||
if not txt_messages:
|
if not txt_messages:
|
||||||
return {
|
return {
|
||||||
'chart_data': None,
|
'chart_data': None,
|
||||||
@ -77,23 +77,31 @@ def wordcloud(wxid):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def calendar_chart(wxid, year):
|
def calendar_chart(wxid, is_Annual_report=False, year='2023'):
|
||||||
calendar_data = msg_db.get_messages_by_days(wxid, year)
|
calendar_data = msg_db.get_messages_by_days(wxid, is_Annual_report, year)
|
||||||
|
|
||||||
if not calendar_data:
|
if not calendar_data:
|
||||||
return False
|
return False
|
||||||
min_ = min(map(lambda x: x[1], calendar_data))
|
min_ = min(map(lambda x: x[1], calendar_data))
|
||||||
max_ = max(map(lambda x: x[1], calendar_data))
|
max_ = max(map(lambda x: x[1], calendar_data))
|
||||||
|
start_date_ = calendar_data[0][0]
|
||||||
|
end_date_ = calendar_data[-1][0]
|
||||||
|
print(start_date_, '---->', end_date_)
|
||||||
|
if is_Annual_report:
|
||||||
|
calendar_days = year
|
||||||
|
calendar_title = f'{year}年聊天情况'
|
||||||
|
else:
|
||||||
|
calendar_days = (start_date_, end_date_)
|
||||||
|
calendar_title = '和Ta的聊天情况'
|
||||||
c = (
|
c = (
|
||||||
Calendar(init_opts=opts.InitOpts(width=f"{charts_width}px", height=f"{charts_height}px"))
|
Calendar(init_opts=opts.InitOpts(width=f"{charts_width}px", height=f"{charts_height}px"))
|
||||||
.add(
|
.add(
|
||||||
"",
|
"",
|
||||||
calendar_data,
|
calendar_data,
|
||||||
calendar_opts=opts.CalendarOpts(range_=year)
|
calendar_opts=opts.CalendarOpts(range_=calendar_days)
|
||||||
)
|
)
|
||||||
.set_global_opts(
|
.set_global_opts(
|
||||||
title_opts=opts.TitleOpts(title="2023年聊天情况"),
|
title_opts=opts.TitleOpts(title=calendar_title),
|
||||||
visualmap_opts=opts.VisualMapOpts(
|
visualmap_opts=opts.VisualMapOpts(
|
||||||
max_=max_,
|
max_=max_,
|
||||||
min_=min_,
|
min_=min_,
|
||||||
@ -111,11 +119,11 @@ def calendar_chart(wxid, year):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def month_count(wxid, year):
|
def month_count(wxid, is_Annual_report=False, year='2023'):
|
||||||
"""
|
"""
|
||||||
每月聊天条数
|
每月聊天条数
|
||||||
"""
|
"""
|
||||||
msg_data = msg_db.get_messages_by_month(wxid, year)
|
msg_data = msg_db.get_messages_by_month(wxid, is_Annual_report, year)
|
||||||
y_data = list(map(lambda x: x[1], msg_data))
|
y_data = list(map(lambda x: x[1], msg_data))
|
||||||
x_axis = list(map(lambda x: x[0], msg_data))
|
x_axis = list(map(lambda x: x[0], msg_data))
|
||||||
m = (
|
m = (
|
||||||
@ -146,11 +154,11 @@ def month_count(wxid, year):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def hour_count(wxid, year):
|
def hour_count(wxid, is_Annual_report=False, year='2023'):
|
||||||
"""
|
"""
|
||||||
小时计数聊天条数
|
小时计数聊天条数
|
||||||
"""
|
"""
|
||||||
msg_data = msg_db.get_messages_by_hour(wxid, year)
|
msg_data = msg_db.get_messages_by_hour(wxid, is_Annual_report, year)
|
||||||
print(msg_data)
|
print(msg_data)
|
||||||
y_data = list(map(lambda x: x[1], msg_data))
|
y_data = list(map(lambda x: x[1], msg_data))
|
||||||
x_axis = list(map(lambda x: x[0], msg_data))
|
x_axis = list(map(lambda x: x[0], msg_data))
|
||||||
@ -194,10 +202,13 @@ class Analysis:
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
msg_db.init_database(path='../DataBase/Msg/MSG.db')
|
msg_db.init_database(path='../DataBase/Msg/MSG.db')
|
||||||
# w = wordcloud('wxid_0o18ef858vnu22')
|
# w = wordcloud('wxid_0o18ef858vnu22')
|
||||||
c = calendar_chart('wxid_27hqbq7vx5hf22', '2023')
|
w_data = wordcloud('wxid_27hqbq7vx5hf22', True, '2023')
|
||||||
|
# print(w_data)
|
||||||
|
# w['chart_data'].render("./data/聊天统计/wordcloud.html")
|
||||||
|
c = calendar_chart('wxid_27hqbq7vx5hf22', False, '2023')
|
||||||
c['chart_data'].render("./data/聊天统计/calendar.html")
|
c['chart_data'].render("./data/聊天统计/calendar.html")
|
||||||
print('c:::', c)
|
# print('c:::', c)
|
||||||
m = month_count('wxid_27hqbq7vx5hf22', '2023')
|
m = month_count('wxid_27hqbq7vx5hf22', False, '2023')
|
||||||
m['chart_data'].render("./data/聊天统计/month_num.html")
|
m['chart_data'].render("./data/聊天统计/month_num.html")
|
||||||
h = hour_count('wxid_27hqbq7vx5hf22', '2023')
|
h = hour_count('wxid_27hqbq7vx5hf22')
|
||||||
h['chart_data'].render("./data/聊天统计/hour_count.html")
|
h['chart_data'].render("./data/聊天统计/hour_count.html")
|
||||||
|
Loading…
Reference in New Issue
Block a user