年度报告圣诞特别版
This commit is contained in:
@@ -147,47 +147,53 @@ 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_:
|
||||
if year_ == 'all':
|
||||
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
|
||||
'''
|
||||
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=?
|
||||
order by CreateTime
|
||||
'''
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [username_, type_])
|
||||
finally:
|
||||
lock.release()
|
||||
result = self.cursor.fetchall()
|
||||
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,CompressContent
|
||||
from MSG
|
||||
where StrTalker=? and Type=?
|
||||
order by CreateTime
|
||||
'''
|
||||
try:
|
||||
lock.acquire(True)
|
||||
if is_Annual_report_:
|
||||
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
|
||||
'''
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [username_, type_, year_])
|
||||
else:
|
||||
self.cursor.execute(sql, [username_, type_])
|
||||
result = self.cursor.fetchall()
|
||||
finally:
|
||||
lock.release()
|
||||
finally:
|
||||
lock.release()
|
||||
result = self.cursor.fetchall()
|
||||
return result
|
||||
|
||||
def get_messages_by_keyword(self, username_, keyword, num=5, max_len=10):
|
||||
def get_messages_by_keyword(self, username_, keyword, num=5, max_len=10, year_='all'):
|
||||
if not self.open_flag:
|
||||
return None
|
||||
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
|
||||
from MSG
|
||||
where StrTalker=? and Type=1 and LENGTH(StrContent)<? and StrContent like ?
|
||||
{"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""}
|
||||
order by CreateTime desc
|
||||
'''
|
||||
temp = []
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [username_, max_len, f'%{keyword}%'])
|
||||
self.cursor.execute(sql, [username_, max_len, f'%{keyword}%'] if year_ == "all" else [username_, max_len,
|
||||
f'%{keyword}%',
|
||||
year_])
|
||||
messages = self.cursor.fetchall()
|
||||
finally:
|
||||
lock.release()
|
||||
@@ -222,6 +228,11 @@ class Msg:
|
||||
('', '', ['', ''], ''),
|
||||
('', '', '', '')
|
||||
))
|
||||
"""
|
||||
返回值为一个列表,每个列表元素是一个对话
|
||||
每个对话是一个元组数据
|
||||
('is_send','时间戳','以关键词为分割符的消息内容','格式化时间')
|
||||
"""
|
||||
return res
|
||||
|
||||
def get_contact(self, contacts):
|
||||
@@ -317,18 +328,11 @@ 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:
|
||||
def get_messages_by_hour(self, username_, year_='all'):
|
||||
result = []
|
||||
if not self.open_flag:
|
||||
return result
|
||||
if year_ == 'all':
|
||||
sql = '''
|
||||
SELECT strftime('%H:00',CreateTime,'unixepoch','localtime') as hours,count(MsgSvrID)
|
||||
from (
|
||||
@@ -338,21 +342,32 @@ class Msg:
|
||||
)
|
||||
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:
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [username_])
|
||||
result = self.cursor.fetchall()
|
||||
except sqlite3.DatabaseError:
|
||||
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
||||
finally:
|
||||
lock.release()
|
||||
# result.sort(key=lambda x: x[5])
|
||||
except sqlite3.DatabaseError:
|
||||
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
||||
finally:
|
||||
lock.release()
|
||||
result = self.cursor.fetchall()
|
||||
else:
|
||||
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
|
||||
'''
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [username_, year_])
|
||||
except sqlite3.DatabaseError:
|
||||
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
||||
finally:
|
||||
lock.release()
|
||||
result = self.cursor.fetchall()
|
||||
return result
|
||||
|
||||
def get_first_time_of_message(self, username_):
|
||||
@@ -373,6 +388,38 @@ class Msg:
|
||||
lock.release()
|
||||
return result
|
||||
|
||||
def get_latest_time_of_message(self, username_, year_='all'):
|
||||
if not self.open_flag:
|
||||
return None
|
||||
sql = f'''
|
||||
SELECT isSender,StrContent,strftime('%Y-%m-%d %H:%M:%S',CreateTime,'unixepoch','localtime') as StrTime,
|
||||
strftime('%H:%M:%S', CreateTime,'unixepoch','localtime') as hour
|
||||
FROM MSG
|
||||
WHERE StrTalker = ? AND Type=1 AND
|
||||
hour BETWEEN '00:00:00' AND '05:00:00'
|
||||
{"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""}
|
||||
ORDER BY hour DESC
|
||||
LIMIT 20;
|
||||
'''
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [username_, year_] if year_ != "all" else [username_])
|
||||
except sqlite3.DatabaseError:
|
||||
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
||||
finally:
|
||||
lock.release()
|
||||
result = self.cursor.fetchall()
|
||||
if not result:
|
||||
return []
|
||||
res = []
|
||||
is_sender = result[0][0]
|
||||
res.append(result[0])
|
||||
for msg in result[1:]:
|
||||
if msg[0] != is_sender:
|
||||
res.append(msg)
|
||||
break
|
||||
return res
|
||||
|
||||
def get_send_messages_type_number(self, year_="all") -> list:
|
||||
"""
|
||||
统计自己发的各类型消息条数,按条数降序,精确到subtype\n
|
||||
@@ -401,6 +448,28 @@ class Msg:
|
||||
lock.release()
|
||||
return result
|
||||
|
||||
def get_messages_number(self, username_, year_="all") -> int:
|
||||
sql = f"""
|
||||
SELECT Count(MsgSvrID)
|
||||
from MSG
|
||||
where StrTalker = ?
|
||||
{"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""}
|
||||
group by type, subtype
|
||||
order by Count(MsgSvrID) desc
|
||||
"""
|
||||
result = None
|
||||
if not self.open_flag:
|
||||
return None
|
||||
try:
|
||||
lock.acquire(True)
|
||||
self.cursor.execute(sql, [username_,year_] if year_ != "all" else [username_])
|
||||
result = self.cursor.fetchone()
|
||||
except sqlite3.DatabaseError:
|
||||
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
|
||||
finally:
|
||||
lock.release()
|
||||
return result[0] if result else 0
|
||||
|
||||
def get_chatted_top_contacts(self, year_="all", contain_chatroom=False, top_n=10) -> list:
|
||||
"""
|
||||
统计聊天最多的 n 个联系人(默认不包含群组),按条数降序\n
|
||||
@@ -429,7 +498,7 @@ class Msg:
|
||||
finally:
|
||||
lock.release()
|
||||
return result
|
||||
|
||||
|
||||
def get_send_messages_length(self, year_="all") -> int:
|
||||
"""
|
||||
统计自己总共发消息的字数,包含type=1的文本和type=49,subtype=57里面自己发的文本
|
||||
@@ -539,15 +608,5 @@ if __name__ == '__main__':
|
||||
db_path = "./app/database/Msg/MSG.db"
|
||||
msg = Msg()
|
||||
msg.init_database()
|
||||
print(msg.get_chatted_top_contacts(year_="2023"))
|
||||
print(msg.get_chatted_top_contacts(year_="2023", contain_chatroom=True, top_n=20))
|
||||
# result = msg.get_message_by_num("wxid_vtz9jk9ulzjt22", 9999999)
|
||||
# print(result)
|
||||
# result = msg.get_messages_by_type("wxid_vtz9jk9ulzjt22", 49)
|
||||
# for r in result:
|
||||
# type_ = r[2]
|
||||
# sub_type = r[3]
|
||||
# if type_ == 49 and sub_type == 57:
|
||||
# print(r)
|
||||
# print(r[-1])
|
||||
# break
|
||||
print(msg.get_latest_time_of_message('wxid_0o18ef858vnu22', year_='2023'))
|
||||
print(msg.get_messages_number('wxid_0o18ef858vnu22', year_='2023'))
|
||||
|
||||
Reference in New Issue
Block a user