🤪解决内存异常占用问题,美化页面

This commit is contained in:
2024-07-28 21:46:12 +08:00
parent e5384ce6c0
commit b32673d78a
7 changed files with 532 additions and 153 deletions

View File

@ -1,9 +1,11 @@
from fastapi import FastAPI
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.responses import HTMLResponse, JSONResponse
from starlette.middleware.cors import CORSMiddleware
import schedule
import time
import logging
import os
import json
import random
from threading import Lock, Thread
@ -12,20 +14,22 @@ from friend_circle_lite.get_conf import load_config
app = FastAPI()
# 添加 CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 配置日志记录
log_file = "grab.log"
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 全局变量
articles_data = {
"statistical_data": {},
"article_data": []
}
error_friends_info = []
data_lock = Lock()
def fetch_articles():
global articles_data, error_friends_info
logging.info("开始抓取文章...")
try:
config = load_config("./conf.yaml")
@ -35,10 +39,10 @@ def fetch_articles():
logging.info(f"正在从 {json_url} 中获取,每个博客获取 {article_count} 篇文章")
result, errors = fetch_and_process_data(json_url=json_url, count=article_count)
sorted_result = sort_articles_by_time(result)
with data_lock:
articles_data["article_data"] = sorted_result["article_data"]
articles_data["statistical_data"] = sorted_result["statistical_data"]
error_friends_info = errors
with open("all.json", "w", encoding="utf-8") as f:
json.dump(sorted_result, f, ensure_ascii=False, indent=2)
with open("errors.json", "w", encoding="utf-8") as f:
json.dump(errors, f, ensure_ascii=False, indent=2)
logging.info("文章抓取成功")
else:
logging.warning("抓取设置在配置中被禁用。")
@ -47,43 +51,49 @@ def fetch_articles():
@app.get("/", response_class=HTMLResponse)
async def root():
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Friend Circle Lite</title>
</head>
<body>
<h1>欢迎使用 Friend Circle Lite</h1>
<p>这是一个轻量版友链朋友圈,有两种部署方式,其中自部署使用 fastAPI还有 github action 部署方式,可以很方便的从友链中获取文章并展示到前端。</p>
<ul>
<li><a href="/all">查看所有文章,按照时间进行排序</a></li>
<li><a href="/errors">查看出错数据,包含所有的错误友链信息,可自行发挥</a></li>
<li><a href="/random">随机文章</a></li>
</ul>
</body>
</html>
"""
return HTMLResponse(content=html_content)
try:
with open('./static/deploy-home.html', 'r', encoding='utf-8') as f:
html_content = f.read()
return HTMLResponse(content=html_content)
except FileNotFoundError:
return HTMLResponse(content="<h1>File not found</h1>", status_code=404)
@app.get('/all')
async def get_all_articles():
with data_lock:
try:
with open('all.json', 'r', encoding='utf-8') as f:
articles_data = json.load(f)
return JSONResponse(content=articles_data)
except FileNotFoundError:
return JSONResponse(content={"error": "File not found"}, status_code=404)
except json.JSONDecodeError:
return JSONResponse(content={"error": "Failed to decode JSON"}, status_code=500)
@app.get('/errors')
async def get_error_friends():
with data_lock:
return JSONResponse(content=error_friends_info)
try:
with open('errors.json', 'r', encoding='utf-8') as f:
errors_data = json.load(f)
return JSONResponse(content=errors_data)
except FileNotFoundError:
return JSONResponse(content={"error": "File not found"}, status_code=404)
except json.JSONDecodeError:
return JSONResponse(content={"error": "Failed to decode JSON"}, status_code=500)
@app.get('/random')
async def get_random_article():
with data_lock:
if articles_data["article_data"]:
try:
with open('all.json', 'r', encoding='utf-8') as f:
articles_data = json.load(f)
if articles_data.get("article_data"):
random_article = random.choice(articles_data["article_data"])
return JSONResponse(content=random_article)
else:
return JSONResponse(content={"error": "No articles available"}, status_code=404)
except FileNotFoundError:
return JSONResponse(content={"error": "File not found"}, status_code=404)
except json.JSONDecodeError:
return JSONResponse(content={"error": "Failed to decode JSON"}, status_code=500)
def schedule_tasks():
schedule.every(4).hours.do(fetch_articles)