mirror of
https://github.com/stardrophere/InsightRadar.git
synced 2026-06-06 00:00:05 +08:00
修改成在启动的时候可以自动初始化数据
This commit is contained in:
@@ -44,6 +44,8 @@ MANIFEST
|
|||||||
pip-log.txt
|
pip-log.txt
|
||||||
pip-delete-this-directory.txt
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
**/logs/*
|
||||||
|
|
||||||
# Unit test / coverage reports
|
# Unit test / coverage reports
|
||||||
htmlcov/
|
htmlcov/
|
||||||
.tox/
|
.tox/
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
"""
|
"""
|
||||||
信息源 CRUD:对 InfoSource 的增删改查,供 API 与爬虫使用
|
信息源 CRUD:对 InfoSource 的增删改查,供 API 与爬虫使用
|
||||||
"""
|
"""
|
||||||
|
from sqlite3 import IntegrityError
|
||||||
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from typing import List, Optional
|
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:
|
def create(db: Session, obj_in: InfoSourceCreate) -> InfoSource:
|
||||||
"""创建新的信息源"""
|
"""创建新的信息源"""
|
||||||
db_obj = InfoSource(**obj_in.model_dump())
|
db_obj = InfoSource(**obj_in.model_dump())
|
||||||
db.add(db_obj)
|
try:
|
||||||
db.commit()
|
db.add(db_obj)
|
||||||
db.refresh(db_obj)
|
db.commit()
|
||||||
return db_obj
|
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:
|
def update(db: Session, db_obj: InfoSource, obj_in: InfoSourceUpdate) -> InfoSource:
|
||||||
|
|||||||
+35
-39
@@ -1,46 +1,42 @@
|
|||||||
import requests
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
# 请将此处的 URL 替换为您实际的 API 基础域名
|
from app.database import SessionLocal
|
||||||
api_url = "http://10.252.130.135:8000/api/v1/sources/"
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
# 解析后的数据源列表
|
def init():
|
||||||
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"}
|
|
||||||
]
|
|
||||||
|
|
||||||
# 遍历数据并发送 POST 请求
|
# 解析后的数据源列表
|
||||||
for item in sources_data:
|
sources_data = [
|
||||||
payload = {
|
{"name": "今日头条", "url": "toutiao"},
|
||||||
"source_name": item["name"],
|
{"name": "百度热搜", "url": "baidu"},
|
||||||
"source_type": "HOT_TREND",
|
{"name": "华尔街见闻", "url": "wallstreetcn-hot"},
|
||||||
"home_url": item["url"],
|
{"name": "澎湃新闻", "url": "thepaper"},
|
||||||
"is_enabled": True
|
{"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:
|
# 遍历数据并发送 POST 请求
|
||||||
response = requests.post(api_url, headers=headers, data=json.dumps(payload))
|
for item in sources_data:
|
||||||
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}")
|
|
||||||
|
|
||||||
print("执行完毕!")
|
try:
|
||||||
|
|
||||||
|
with SessionLocal() as db:
|
||||||
|
|
||||||
|
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}")
|
||||||
|
|||||||
+6
-1
@@ -1,6 +1,7 @@
|
|||||||
# app/main.py
|
# app/main.py
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import httpx
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
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.services.delivery_service import check_and_deliver
|
||||||
from app.database import engine
|
from app.database import engine
|
||||||
from app.models.models import Base
|
from app.models.models import Base
|
||||||
|
from app.initialize import init
|
||||||
|
|
||||||
# 路由总线
|
# 路由总线
|
||||||
from app.api.router import api_router
|
from app.api.router import api_router
|
||||||
@@ -42,6 +44,10 @@ async def lifespan(app: FastAPI):
|
|||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
logging.info("数据库表初始化完成!")
|
logging.info("数据库表初始化完成!")
|
||||||
|
|
||||||
|
logging.info("初始化订阅源")
|
||||||
|
init()
|
||||||
|
logging.info("订阅源初始化完毕")
|
||||||
|
|
||||||
# 2. 配置并启动定时任务
|
# 2. 配置并启动定时任务
|
||||||
scheduler.add_job(
|
scheduler.add_job(
|
||||||
fetch_and_save_trending_data,
|
fetch_and_save_trending_data,
|
||||||
@@ -106,7 +112,6 @@ app.add_middleware(
|
|||||||
# 版本控制
|
# 版本控制
|
||||||
app.include_router(api_router, prefix="/api/v1")
|
app.include_router(api_router, prefix="/api/v1")
|
||||||
|
|
||||||
|
|
||||||
# 健康检查
|
# 健康检查
|
||||||
@app.get("/", tags=["健康检查"])
|
@app.get("/", tags=["健康检查"])
|
||||||
async def root():
|
async def root():
|
||||||
|
|||||||
@@ -94,6 +94,10 @@ class InfoSource(Base):
|
|||||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
|
||||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow, onupdate=utcnow)
|
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow, onupdate=utcnow)
|
||||||
|
|
||||||
|
__table_args__ = (
|
||||||
|
UniqueConstraint("source_name", name="uix_source_name"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# 模块二:AI 语义聚类中枢 (大事件池)
|
# 模块二:AI 语义聚类中枢 (大事件池)
|
||||||
|
|||||||
Reference in New Issue
Block a user