PyBot/web/app.py

114 lines
4.2 KiB
Python
Raw Normal View History

2024-12-10 16:24:41 +08:00
from flask import Flask, jsonify, render_template
2025-01-15 11:02:39 +08:00
from flask import send_from_directory
2025-01-14 18:16:59 +08:00
from collections import deque
2024-12-10 16:24:41 +08:00
import os
2024-12-25 18:40:20 +08:00
import logging
2024-12-10 16:24:41 +08:00
app = Flask(__name__)
# 配置文件路径
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PARENT_DIR = os.path.dirname(BASE_DIR) # 上一个文件夹
2025-01-02 17:11:11 +08:00
SEC_NEWS_PATH = os.path.join(PARENT_DIR, 'resources', 'history', 'sec_news.md')
TECH_PASSAGE_PATH = os.path.join(PARENT_DIR, 'resources', 'history', 'tech_passage.md')
2025-01-15 11:02:39 +08:00
BAIDU_NEWS_PATH = os.path.join(PARENT_DIR, 'resources', 'history', 'baidu_news.md')
GITHUB_PATH = os.path.join(PARENT_DIR, 'resources', 'history', 'github.md')
WX_NEWS_PATH = os.path.join(PARENT_DIR, 'resources', 'history', 'wx_news.md')
2025-01-02 17:11:11 +08:00
CORE_LOG_PATH = os.path.join(PARENT_DIR, 'resources', 'log', 'core.log')
WEB_LOG_PATH = os.path.join(PARENT_DIR, 'resources', 'log', 'app.log')
2024-12-25 18:40:20 +08:00
# 配置日志记录器
logging.basicConfig(
filename=WEB_LOG_PATH,
level=logging.INFO,
2025-01-15 11:02:39 +08:00
format='%(asctime)s - %(levelname)s - %(message)s'
2024-12-25 18:40:20 +08:00
)
2024-12-10 16:24:41 +08:00
2024-12-15 02:14:39 +08:00
@app.route('/')
def index():
2024-12-25 18:40:20 +08:00
logging.info("访问主页")
2024-12-15 02:14:39 +08:00
return render_template('index.html')
2025-01-15 11:02:39 +08:00
@app.route('/static/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'templates/static'), 'favicon.ico')
def replace_content(content):
content = content.replace('####', '###')
content = content.replace(r"e:\Self-Tool-Code\PyBot", '.') # 修改: 使用原始字符串避免转义问题
return content
2025-01-14 18:16:59 +08:00
def get_records(content, num_records=20, delimiter='-'*40):
records = content.split(delimiter)
selected_records = records[:num_records]
return delimiter.join(selected_records)
def read_last_lines(file_path, num_lines=100):
with open(file_path, 'r', encoding='utf-8') as file:
return deque(file, maxlen=num_lines)
2025-01-15 11:02:39 +08:00
def read_md_file(file_path):
2024-12-10 16:24:41 +08:00
try:
2025-01-15 11:02:39 +08:00
with open(file_path, 'r', encoding='utf-8') as file:
2024-12-10 16:24:41 +08:00
content = file.read()
content = replace_content(content)
2025-01-14 18:16:59 +08:00
content = get_records(content)
2025-01-15 11:02:39 +08:00
return content, 200
2024-12-10 16:24:41 +08:00
except FileNotFoundError:
2025-01-15 11:02:39 +08:00
logging.error(f"文件缺失: {file_path}")
return f"{os.path.basename(file_path)} 文件缺失!", 404
2024-12-10 16:24:41 +08:00
except Exception as e:
2025-01-15 11:02:39 +08:00
logging.error(f"读取时出错: {file_path}, 原因: {str(e)}")
return str(e), 500
2024-12-10 16:24:41 +08:00
2024-12-15 02:14:39 +08:00
@app.route('/log')
def get_log():
2024-12-25 18:40:20 +08:00
logging.info(f"尝试打开核心日志文件: {CORE_LOG_PATH}")
2024-12-15 02:14:39 +08:00
# 读取日志文件内容
2025-01-14 18:16:59 +08:00
log_content = read_last_lines(CORE_LOG_PATH)
log_content = '\n'.join(log_content)
2024-12-15 02:14:39 +08:00
# 将日志内容传递给模板
return render_template('log.html', log_content=log_content)
2024-12-25 18:40:20 +08:00
@app.route('/weblog')
def get_weblog():
logging.info(f"尝试打开Web应用日志文件: {WEB_LOG_PATH}")
2025-01-15 11:02:39 +08:00
with open(WEB_LOG_PATH, 'r') as file:
log_content = deque(file, maxlen=100)
2025-01-14 18:16:59 +08:00
log_content = '\n'.join(log_content)
2024-12-25 18:40:20 +08:00
log_content = replace_content(log_content)
return render_template('log.html', log_content=log_content)
2025-01-15 11:02:39 +08:00
@app.route('/get-sec-news')
def get_sec_news():
logging.info(f"尝试打开安全新闻历史推送文件: {SEC_NEWS_PATH}")
content, status_code = read_md_file(SEC_NEWS_PATH)
return jsonify({'content': content}), status_code
@app.route('/get-tech-passage')
def get_tech_passage():
logging.info(f"尝试打开技术文章历史推送文件: {TECH_PASSAGE_PATH}")
content, status_code = read_md_file(TECH_PASSAGE_PATH)
return jsonify({'content': content}), status_code
@app.route('/get-baidu-news')
def get_baidu_news():
logging.info(f"尝试打开百度新闻历史推送文件: {BAIDU_NEWS_PATH}")
content, status_code = read_md_file(BAIDU_NEWS_PATH)
return jsonify({'content': content}), status_code
@app.route('/get-github-news')
def get_github_news():
logging.info(f"尝试打开GitHub历史推送文件: {GITHUB_PATH}")
content, status_code = read_md_file(GITHUB_PATH)
return jsonify({'content': content}), status_code
@app.route('/get-wx-news')
def get_wx_news():
logging.info(f"尝试打开微信历史推送文件: {WX_NEWS_PATH}")
content, status_code = read_md_file(WX_NEWS_PATH)
return jsonify({'content': content}), status_code
2024-12-10 16:24:41 +08:00
if __name__ == '__main__':
2025-01-24 17:36:53 +08:00
app.run(debug=True, port=666) # 在生产环境中应设置为 False