📖完善文档,增加超时时间,继续尝试新方式自部署

This commit is contained in:
柳神 2024-07-27 17:14:28 +08:00
parent 2056f642f9
commit 547f72d8a9
5 changed files with 24 additions and 26 deletions

View File

@ -9,7 +9,7 @@ headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
} }
timeout = (5, 10) # 连接超时和读取超时防止requests接受时间过长 timeout = (10, 15) # 连接超时和读取超时防止requests接受时间过长
def format_published_time(time_str): def format_published_time(time_str):
""" """
@ -184,6 +184,7 @@ def process_friend(friend, session, count):
""" """
name, blog_url, avatar = friend name, blog_url, avatar = friend
feed_type, feed_url = check_feed(blog_url, session) feed_type, feed_url = check_feed(blog_url, session)
print(f"========“{name}”的博客“{blog_url}”的feed类型为“{feed_type}”========")
if feed_type != 'none': if feed_type != 'none':
feed_info = parse_feed(feed_url, session, count) feed_info = parse_feed(feed_url, session, count)

View File

@ -1,3 +1,3 @@
2024-07-22 10:21:56,649 - INFO - 开始抓取文章... 2024-07-27 17:00:52,303 - INFO - 开始抓取文章...
2024-07-22 10:21:56,653 - INFO - 正在从 https://blog.qyliu.top/friend.json 中获取,每个博客获取 5 篇文章 2024-07-27 17:00:52,306 - INFO - 正在从 https://blog.qyliu.top/friend.json 中获取,每个博客获取 5 篇文章
2024-07-22 10:22:32,855 - INFO - 文章抓取成功 2024-07-27 17:01:52,138 - INFO - 文章抓取成功

View File

@ -12,6 +12,8 @@
* 自部署添加跨域请求 * 自部署添加跨域请求
* 添加`/rss.xml``/feed/``feed.xml`接口的爬取,提高兼容性 * 添加`/rss.xml``/feed/``feed.xml`接口的爬取,提高兼容性
* 修复PJAX下会多次出现模态框的问题并且切换页面不消失
* 修复模态框宽度问题,添加日历图标以更加美观
### 2024-07-25 ### 2024-07-25

View File

@ -1,30 +1,17 @@
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.responses import JSONResponse, HTMLResponse from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware import schedule
from apscheduler.schedulers.asyncio import AsyncIOScheduler import time
from threading import Lock
import logging import logging
import os import os
import json
import random import random
from threading import Lock, Thread
from friend_circle_lite.get_info import fetch_and_process_data, sort_articles_by_time from friend_circle_lite.get_info import fetch_and_process_data, sort_articles_by_time
from friend_circle_lite.get_conf import load_config from friend_circle_lite.get_conf import load_config
app = FastAPI() app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 允许的域
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 配置APScheduler
scheduler = AsyncIOScheduler()
scheduler.start()
# 配置日志记录 # 配置日志记录
log_file = "grab.log" log_file = "grab.log"
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@ -58,9 +45,6 @@ def fetch_articles():
except Exception as e: except Exception as e:
logging.error(f"抓取文章时出错: {e}") logging.error(f"抓取文章时出错: {e}")
# 每四个小时抓取一次文章
scheduler.add_job(fetch_articles, 'interval', hours=4)
@app.get("/", response_class=HTMLResponse) @app.get("/", response_class=HTMLResponse)
async def root(): async def root():
html_content = """ html_content = """
@ -74,7 +58,7 @@ async def root():
<p>这是一个轻量版友链朋友圈有两种部署方式其中自部署使用 fastAPI还有 github action 部署方式可以很方便的从友链中获取文章并展示到前端</p> <p>这是一个轻量版友链朋友圈有两种部署方式其中自部署使用 fastAPI还有 github action 部署方式可以很方便的从友链中获取文章并展示到前端</p>
<ul> <ul>
<li><a href="/all">查看所有文章按照时间进行排序</a></li> <li><a href="/all">查看所有文章按照时间进行排序</a></li>
<li><a href="/errors">查看出错数据包含所有的错误友链信息可自行发挥</a></li> <li><a href="/errors">查看出错数据包含所有的错误友链信息可自行发挥</a></li>
<li><a href="/random">随机文章</a></li> <li><a href="/random">随机文章</a></li>
</ul> </ul>
</body> </body>
@ -101,13 +85,24 @@ async def get_random_article():
else: else:
return JSONResponse(content={"error": "No articles available"}, status_code=404) return JSONResponse(content={"error": "No articles available"}, status_code=404)
if __name__ == '__main__': def schedule_tasks():
import uvicorn schedule.every(4).hours.do(fetch_articles)
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == '__main__':
# 清空日志文件 # 清空日志文件
if os.path.exists(log_file): if os.path.exists(log_file):
with open(log_file, 'w'): with open(log_file, 'w'):
pass pass
fetch_articles() # 启动时立即抓取一次 fetch_articles() # 启动时立即抓取一次
# 启动调度任务线程
task_thread = Thread(target=schedule_tasks)
task_thread.start()
# 启动FastAPI应用
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=1223) uvicorn.run(app, host='0.0.0.0', port=1223)