update
This commit is contained in:
1 parent
61f505d439
commit
ba00741c11
17 files changed
+1646
-1168
No files matched your search
+53
-33
@@ -1,4 +1,5 @@
|
||||
from flask import Flask, jsonify, render_template
|
||||
from flask import send_from_directory
|
||||
from collections import deque
|
||||
import os
|
||||
import logging
|
||||
@@ -10,6 +11,9 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
PARENT_DIR = os.path.dirname(BASE_DIR) # 上一个文件夹
|
||||
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')
|
||||
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')
|
||||
CORE_LOG_PATH = os.path.join(PARENT_DIR, 'resources', 'log', 'core.log')
|
||||
WEB_LOG_PATH = os.path.join(PARENT_DIR, 'resources', 'log', 'app.log')
|
||||
|
||||
@@ -17,23 +21,25 @@ WEB_LOG_PATH = os.path.join(PARENT_DIR, 'resources', 'log', 'app.log')
|
||||
logging.basicConfig(
|
||||
filename=WEB_LOG_PATH,
|
||||
level=logging.INFO,
|
||||
format= '%(asctime)s - %(levelname)s - %(message)s'
|
||||
format='%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
def replace_content(content):
|
||||
content = content.replace('####', '###')
|
||||
content = content.replace(r"e:\Self-Tool-Code\PyBot", '.') # 修改: 使用原始字符串避免转义问题
|
||||
return content
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
logging.info("访问主页")
|
||||
return render_template('index.html')
|
||||
|
||||
@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
|
||||
|
||||
def get_records(content, num_records=20, delimiter='-'*40):
|
||||
records = content.split(delimiter)
|
||||
# 去除空字符串,去除之后MD格式会混乱
|
||||
# records = [record.strip() for record in records if record.strip()]
|
||||
selected_records = records[:num_records]
|
||||
return delimiter.join(selected_records)
|
||||
|
||||
@@ -41,37 +47,20 @@ 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)
|
||||
|
||||
@app.route('/get-sec-news')
|
||||
def get_sec_news():
|
||||
logging.info(f"尝试打开安全新闻历史推送文件: {SEC_NEWS_PATH}")
|
||||
def read_md_file(file_path):
|
||||
try:
|
||||
with open(SEC_NEWS_PATH, 'r', encoding='utf-8') as file:
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
content = file.read()
|
||||
content = replace_content(content)
|
||||
content = get_records(content)
|
||||
return jsonify({'content': content}), 200
|
||||
return content, 200
|
||||
except FileNotFoundError:
|
||||
logging.error(f"文件缺失: {SEC_NEWS_PATH}")
|
||||
return jsonify({'error': '安全新闻历史推送文件缺失!'}), 404
|
||||
logging.error(f"文件缺失: {file_path}")
|
||||
return f"{os.path.basename(file_path)} 文件缺失!", 404
|
||||
except Exception as e:
|
||||
logging.error(f"读取时出错: {SEC_NEWS_PATH}, 原因: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
logging.error(f"读取时出错: {file_path}, 原因: {str(e)}")
|
||||
return str(e), 500
|
||||
|
||||
@app.route('/get-tech-passage')
|
||||
def get_tech_passage():
|
||||
logging.info(f"尝试打开技术文章历史推送文件: {TECH_PASSAGE_PATH}")
|
||||
try:
|
||||
with open(TECH_PASSAGE_PATH, 'r', encoding='utf-8') as file:
|
||||
content = file.read()
|
||||
content = replace_content(content)
|
||||
content = get_records(content)
|
||||
return jsonify({'content': content}), 200
|
||||
except FileNotFoundError:
|
||||
logging.error(f"文件缺失: {TECH_PASSAGE_PATH}")
|
||||
return jsonify({'error': '技术文章历史推送文件缺失!'}), 404
|
||||
except Exception as e:
|
||||
logging.error(f"读取时出错: {TECH_PASSAGE_PATH}, 原因: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@app.route('/log')
|
||||
def get_log():
|
||||
@@ -85,11 +74,42 @@ def get_log():
|
||||
@app.route('/weblog')
|
||||
def get_weblog():
|
||||
logging.info(f"尝试打开Web应用日志文件: {WEB_LOG_PATH}")
|
||||
log_content = read_last_lines(WEB_LOG_PATH)
|
||||
with open(WEB_LOG_PATH, 'r') as file:
|
||||
log_content = deque(file, maxlen=100)
|
||||
log_content = '\n'.join(log_content)
|
||||
log_content = replace_content(log_content)
|
||||
return render_template('log.html', log_content=log_content)
|
||||
|
||||
@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
|
||||
|
||||
def run_server():
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
|
||||
|
||||
+54
-43
@@ -4,6 +4,7 @@
|
||||
<meta charset="UTF-8">
|
||||
<title>资讯推送Web端</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
|
||||
<!-- 引入 Layui 的 CSS -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src/dist/css/layui.css">
|
||||
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
|
||||
@@ -75,7 +76,13 @@
|
||||
<body>
|
||||
<div class="layui-container">
|
||||
<center><h1 class="layui-title" id="page-title">历史推送读取</h1><br>
|
||||
<button id="toggle-fetch-btn" class="layui-btn">切换读取源</button><br>
|
||||
<div id="button-container" class="layui-btn-group">
|
||||
<button id="sec-news-btn" class="layui-btn">安全新闻</button>
|
||||
<button id="tech-passage-btn" class="layui-btn">技术文章</button>
|
||||
<button id="baidu-news-btn" class="layui-btn">百度新闻</button>
|
||||
<button id="github-news-btn" class="layui-btn">GitHub新闻</button>
|
||||
<button id="wx-news-btn" class="layui-btn">微信新闻</button>
|
||||
</div>
|
||||
<h4>为保证网页运作性能,默认仅读取前20条记录,若您有需求可自行更改app.py</h4></center>
|
||||
<div id="markdown-content" class="layui-card-body"></div>
|
||||
</div>
|
||||
@@ -88,26 +95,38 @@
|
||||
<script>
|
||||
layui.use(['layer'], function(){
|
||||
var layer = layui.layer;
|
||||
var currentUrl = '/get-sec-news';
|
||||
|
||||
var buttonMap = {
|
||||
'sec-news-btn': '/get-sec-news',
|
||||
'tech-passage-btn': '/get-tech-passage',
|
||||
'baidu-news-btn': '/get-baidu-news',
|
||||
'github-news-btn': '/get-github-news',
|
||||
'wx-news-btn': '/get-wx-news'
|
||||
};
|
||||
|
||||
var titleMap = {
|
||||
'/get-sec-news': '安全新闻',
|
||||
'/get-tech-passage': '技术文章'
|
||||
'/get-tech-passage': '技术文章',
|
||||
'/get-baidu-news': '百度新闻',
|
||||
'/get-github-news': 'GitHub监控',
|
||||
'/get-wx-news': '微信文章监控'
|
||||
};
|
||||
|
||||
function updateTitle(url) {
|
||||
document.getElementById('page-title').innerText = titleMap[url] || '历史推送读取';
|
||||
}
|
||||
|
||||
document.getElementById('toggle-fetch-btn').addEventListener('click', function() {
|
||||
if (currentUrl === '/get-sec-news') {
|
||||
currentUrl = '/get-tech-passage';
|
||||
} else {
|
||||
currentUrl = '/get-sec-news';
|
||||
}
|
||||
function fetchContent(url, buttonId) {
|
||||
// Disable all buttons
|
||||
Object.keys(buttonMap).forEach(key => {
|
||||
document.getElementById(key).disabled = false;
|
||||
});
|
||||
// Enable the clicked button
|
||||
document.getElementById(buttonId).disabled = true;
|
||||
|
||||
updateTitle(currentUrl);
|
||||
updateTitle(url);
|
||||
|
||||
fetch(currentUrl)
|
||||
fetch(url)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.content) {
|
||||
@@ -122,45 +141,37 @@
|
||||
document.getElementById('markdown-content').innerHTML = '<p>无法加载历史推送文件!</p>';
|
||||
layer.msg('Failed to load markdown.', {icon: 5});
|
||||
});
|
||||
}
|
||||
|
||||
// Add event listeners to buttons
|
||||
Object.keys(buttonMap).forEach(key => {
|
||||
document.getElementById(key).addEventListener('click', function() {
|
||||
fetchContent(buttonMap[key], key);
|
||||
});
|
||||
});
|
||||
|
||||
// Initial fetch on page load
|
||||
updateTitle(currentUrl);
|
||||
var initialButtonId = 'sec-news-btn'; // Default button
|
||||
fetchContent(buttonMap[initialButtonId], initialButtonId);
|
||||
});
|
||||
|
||||
fetch(currentUrl)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.content) {
|
||||
const htmlContent = marked.parse(data.content);
|
||||
document.getElementById('markdown-content').innerHTML = htmlContent;
|
||||
} else {
|
||||
document.getElementById('markdown-content').innerHTML = '<p>加载历史推送文件时出错!(推送历史记录为空)</p>';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('获取源文件失败:', error);
|
||||
document.getElementById('markdown-content').innerHTML = '<p>无法加载历史推送文件!</p>';
|
||||
layer.msg('Failed to load markdown.', {icon: 5});
|
||||
});
|
||||
// 显示或隐藏返回顶部按钮
|
||||
window.onscroll = function() {
|
||||
scrollFunction();
|
||||
};
|
||||
|
||||
// 显示或隐藏返回顶部按钮
|
||||
window.onscroll = function() {
|
||||
scrollFunction();
|
||||
};
|
||||
|
||||
function scrollFunction() {
|
||||
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
|
||||
document.getElementById("back-to-top").style.display = "block";
|
||||
} else {
|
||||
document.getElementById("back-to-top").style.display = "none";
|
||||
}
|
||||
function scrollFunction() {
|
||||
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
|
||||
document.getElementById("back-to-top").style.display = "block";
|
||||
} else {
|
||||
document.getElementById("back-to-top").style.display = "none";
|
||||
}
|
||||
}
|
||||
|
||||
// 返回顶部按钮点击事件
|
||||
document.getElementById("back-to-top").addEventListener("click", function() {
|
||||
document.body.scrollTop = 0;
|
||||
document.documentElement.scrollTop = 0;
|
||||
});
|
||||
// 返回顶部按钮点击事件
|
||||
document.getElementById("back-to-top").addEventListener("click", function() {
|
||||
document.body.scrollTop = 0;
|
||||
document.documentElement.scrollTop = 0;
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in new issue
Block a user