2023-12-05 00:13:20 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2023-12-03 21:25:50 +08:00
|
|
|
|
2023-12-05 00:13:20 +08:00
|
|
|
from flask import Flask, render_template, send_file
|
2023-12-07 20:35:51 +08:00
|
|
|
|
2023-12-03 21:25:50 +08:00
|
|
|
from app.DataBase import msg_db
|
|
|
|
from app.analysis import analysis
|
2023-12-13 22:12:50 +08:00
|
|
|
from app.person import ContactPC, MePC
|
2023-12-03 21:25:50 +08:00
|
|
|
|
2023-11-28 21:32:24 +08:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2023-12-05 00:13:20 +08:00
|
|
|
wxid = ''
|
|
|
|
contact: ContactPC = None
|
|
|
|
|
2023-11-28 21:32:24 +08:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
|
|
|
# 渲染模板,并传递图表的 HTML 到模板中
|
2023-12-07 20:35:51 +08:00
|
|
|
return render_template("index.html")
|
2023-11-28 21:32:24 +08:00
|
|
|
|
2023-12-13 22:12:50 +08:00
|
|
|
|
2023-11-28 21:32:24 +08:00
|
|
|
@app.route('/home')
|
|
|
|
def home():
|
2023-12-05 20:39:21 +08:00
|
|
|
try:
|
2023-12-06 21:29:12 +08:00
|
|
|
first_message, first_time = msg_db.get_first_time_of_message(contact.wxid)
|
2023-12-06 21:59:23 +08:00
|
|
|
except TypeError:
|
2023-12-05 20:39:21 +08:00
|
|
|
return set_text('咱就是说,一次都没聊过就别分析了')
|
2023-12-06 21:29:12 +08:00
|
|
|
data = {
|
|
|
|
'sub_title': '二零二三年度报告',
|
|
|
|
'avatar_path': contact.avatar_path,
|
|
|
|
'nickname': contact.remark,
|
|
|
|
'first_time': first_time,
|
|
|
|
}
|
|
|
|
return render_template('home.html', **data)
|
2023-11-28 21:32:24 +08:00
|
|
|
|
|
|
|
|
2023-12-13 19:24:05 +08:00
|
|
|
@app.route('/wordcloud/<who>/')
|
|
|
|
def one(who):
|
2023-12-05 00:13:20 +08:00
|
|
|
wxid = contact.wxid
|
2023-12-03 22:19:25 +08:00
|
|
|
# wxid = 'wxid_lltzaezg38so22'
|
2023-12-13 19:24:05 +08:00
|
|
|
# print('wxid:'+wxid)
|
2023-12-13 22:12:50 +08:00
|
|
|
world_cloud_data = analysis.wordcloud(wxid, who=who) # 获取与Ta的对话数据
|
2023-12-13 19:24:05 +08:00
|
|
|
# print(world_cloud_data)
|
|
|
|
who = "你" if who == '1' else "TA"
|
2023-12-05 00:13:20 +08:00
|
|
|
with open('wordcloud.html', 'w', encoding='utf-8') as f:
|
|
|
|
f.write(render_template('wordcloud.html', **world_cloud_data))
|
2023-12-13 22:12:50 +08:00
|
|
|
return render_template('wordcloud.html', **world_cloud_data, who=who)
|
2023-12-03 21:25:50 +08:00
|
|
|
|
|
|
|
|
2023-12-05 20:39:21 +08:00
|
|
|
def set_text(text):
|
|
|
|
html = '''
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Centered Text</title>
|
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
height: 100vh;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.centered-text {
|
|
|
|
font-size: 2em;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="centered-text">
|
|
|
|
<!-- 这里是要显示的四个大字 -->
|
|
|
|
%s
|
2023-12-09 22:48:15 +08:00
|
|
|
<img src="https://res.wx.qq.com/t/wx_fed/we-emoji/res/v1.2.8/assets/newemoji/Yellowdog.png" id="旺柴" class="emoji_img">
|
2023-12-05 20:39:21 +08:00
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
''' % (text)
|
|
|
|
return html
|
|
|
|
|
|
|
|
|
2023-12-03 21:25:50 +08:00
|
|
|
@app.route('/test')
|
|
|
|
def test():
|
2023-12-09 22:48:15 +08:00
|
|
|
return set_text('以下内容仅对VIP开放')
|
2023-11-28 21:32:24 +08:00
|
|
|
|
|
|
|
|
2023-12-05 00:13:20 +08:00
|
|
|
def run(port=21314):
|
|
|
|
app.run(debug=True, host='0.0.0.0', port=port, use_reloader=False)
|
|
|
|
|
|
|
|
|
|
|
|
def resource_path(relative_path):
|
|
|
|
""" Get absolute path to resource, works for dev and for PyInstaller """
|
|
|
|
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
return os.path.join(base_path, relative_path)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/data/avatar/<filename>')
|
|
|
|
def get_image(filename):
|
|
|
|
try:
|
|
|
|
# 返回动态生成的图片
|
|
|
|
return send_file(os.path.join("../../data/avatar/", filename), mimetype='image/png')
|
|
|
|
except:
|
|
|
|
return send_file(os.path.join(f"{os.getcwd()}/data/avatar/", filename), mimetype='image/png')
|
|
|
|
|
|
|
|
|
2023-11-28 21:32:24 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=True, host='0.0.0.0')
|