From 97c97b7bae27c527ed726bec218591c83f37c4f7 Mon Sep 17 00:00:00 2001 From: csf123321 Date: Mon, 30 Mar 2026 22:01:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=88=90=E5=9C=A8=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E7=9A=84=E6=97=B6=E5=80=99=E5=8F=AF=E4=BB=A5=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + backend/app/crud/crud_source.py | 16 +++++-- backend/app/initialize.py | 74 ++++++++++++++++----------------- backend/app/main.py | 9 +++- backend/app/models/models.py | 4 ++ 5 files changed, 60 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index c630f3a..783ab42 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,8 @@ MANIFEST pip-log.txt pip-delete-this-directory.txt +**/logs/* + # Unit test / coverage reports htmlcov/ .tox/ diff --git a/backend/app/crud/crud_source.py b/backend/app/crud/crud_source.py index acd157e..f183c39 100644 --- a/backend/app/crud/crud_source.py +++ b/backend/app/crud/crud_source.py @@ -2,6 +2,8 @@ """ 信息源 CRUD:对 InfoSource 的增删改查,供 API 与爬虫使用 """ +from sqlite3 import IntegrityError + from sqlalchemy.orm import Session from typing import List, Optional @@ -22,10 +24,16 @@ def get_multi(db: Session, skip: int = 0, limit: int = 100) -> List[InfoSource]: def create(db: Session, obj_in: InfoSourceCreate) -> InfoSource: """创建新的信息源""" db_obj = InfoSource(**obj_in.model_dump()) - db.add(db_obj) - db.commit() - db.refresh(db_obj) - return db_obj + try: + db.add(db_obj) + db.commit() + db.refresh(db_obj) + return db_obj + except IntegrityError: + db.rollback() + finally: + db.close() + return db_obj def update(db: Session, db_obj: InfoSource, obj_in: InfoSourceUpdate) -> InfoSource: diff --git a/backend/app/initialize.py b/backend/app/initialize.py index 59629f1..2e728bb 100644 --- a/backend/app/initialize.py +++ b/backend/app/initialize.py @@ -1,46 +1,42 @@ -import requests import json -# 请将此处的 URL 替换为您实际的 API 基础域名 -api_url = "http://10.252.130.135:8000/api/v1/sources/" +from app.database import SessionLocal +from app.crud.crud_source import create +from app.models.models import SourceType +from app.schemas.source_schema import InfoSourceCreate -# 请求头 -headers = { - "Content-Type": "application/json", - # "Authorization": "Bearer YOUR_TOKEN" # 如果接口需要鉴权,请取消注释并填入 Token -} -# 解析后的数据源列表 -sources_data = [ - {"name": "今日头条", "url": "toutiao"}, - {"name": "百度热搜", "url": "baidu"}, - {"name": "华尔街见闻", "url": "wallstreetcn-hot"}, - {"name": "澎湃新闻", "url": "thepaper"}, - {"name": "bilibili 热搜", "url": "bilibili-hot-search"}, - {"name": "财联社热门", "url": "cls-hot"}, - {"name": "凤凰网", "url": "ifeng"}, - {"name": "贴吧", "url": "tieba"}, - {"name": "微博", "url": "weibo"}, - {"name": "抖音", "url": "douyin"}, - {"name": "知乎", "url": "zhihu"} -] +def init(): -# 遍历数据并发送 POST 请求 -for item in sources_data: - payload = { - "source_name": item["name"], - "source_type": "HOT_TREND", - "home_url": item["url"], - "is_enabled": True - } + # 解析后的数据源列表 + sources_data = [ + {"name": "今日头条", "url": "toutiao"}, + {"name": "百度热搜", "url": "baidu"}, + {"name": "华尔街见闻", "url": "wallstreetcn-hot"}, + {"name": "澎湃新闻", "url": "thepaper"}, + {"name": "bilibili 热搜", "url": "bilibili-hot-search"}, + {"name": "财联社热门", "url": "cls-hot"}, + {"name": "凤凰网", "url": "ifeng"}, + {"name": "贴吧", "url": "tieba"}, + {"name": "微博", "url": "weibo"}, + {"name": "抖音", "url": "douyin"}, + {"name": "知乎", "url": "zhihu"} + ] - try: - response = requests.post(api_url, headers=headers, data=json.dumps(payload)) - if response.status_code in (200, 201): - print(f"✅ 成功创建: {item['name']}") - else: - print(f"❌ 创建失败: {item['name']} - 状态码: {response.status_code} - 详情: {response.text}") - except Exception as e: - print(f"⚠️ 请求异常: {item['name']} - 错误: {e}") + # 遍历数据并发送 POST 请求 + for item in sources_data: + + try: + + with SessionLocal() as db: -print("执行完毕!") + create(db, InfoSourceCreate( + source_name=item["name"], + source_type=SourceType.HOT_TREND, + home_url=item["url"], + is_enabled=True + )) + print(f"创建订阅源{item['name']}") + + except Exception as e: + print(f"⚠️ 请求异常: {item['name']} - 错误: {e}") diff --git a/backend/app/main.py b/backend/app/main.py index b74b4ed..82c1d00 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1,6 +1,7 @@ # app/main.py import logging import os +import httpx from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware @@ -21,6 +22,7 @@ from app.services.summary_service import generate_unified_summaries from app.services.delivery_service import check_and_deliver from app.database import engine from app.models.models import Base +from app.initialize import init # 路由总线 from app.api.router import api_router @@ -41,7 +43,11 @@ async def lifespan(app: FastAPI): logging.info("正在初始化数据库表...") Base.metadata.create_all(bind=engine) logging.info("数据库表初始化完成!") - + + logging.info("初始化订阅源") + init() + logging.info("订阅源初始化完毕") + # 2. 配置并启动定时任务 scheduler.add_job( fetch_and_save_trending_data, @@ -106,7 +112,6 @@ app.add_middleware( # 版本控制 app.include_router(api_router, prefix="/api/v1") - # 健康检查 @app.get("/", tags=["健康检查"]) async def root(): diff --git a/backend/app/models/models.py b/backend/app/models/models.py index a70942d..667be79 100644 --- a/backend/app/models/models.py +++ b/backend/app/models/models.py @@ -93,6 +93,10 @@ class InfoSource(Base): created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow) updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow, onupdate=utcnow) + + __table_args__ = ( + UniqueConstraint("source_name", name="uix_source_name"), + ) # ==========================================