mirror of
https://github.com/stardrophere/InsightRadar.git
synced 2026-06-05 23:56:36 +08:00
big update
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
# 公关修改追踪 API:查询热搜标题被偷偷修改的历史记录
|
||||
from datetime import timedelta
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.dependencies import get_db
|
||||
from app.models.models import HeadlineRevision, InfoSource, TrendingEvent, utcnow
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from datetime import datetime
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class HeadlineRevisionResponse(BaseModel):
|
||||
"""标题修改记录响应体"""
|
||||
id: int
|
||||
event_id: int
|
||||
previous_headline: str
|
||||
revised_headline: str
|
||||
source_name: Optional[str] = None
|
||||
platform_icon: Optional[str] = None
|
||||
created_at: datetime
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
@router.get("/headline-revisions", response_model=List[HeadlineRevisionResponse])
|
||||
def list_headline_revisions(
|
||||
hours: int = Query(48, ge=1, le=720, description="查询最近多少小时内的修改记录"),
|
||||
limit: int = Query(50, ge=1, le=500, description="最多返回条数"),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""
|
||||
获取最近的标题修改记录列表。
|
||||
用于公关监测:发现哪些平台偷偷改了热搜标题。
|
||||
"""
|
||||
time_limit = utcnow() - timedelta(hours=hours)
|
||||
|
||||
rows = (
|
||||
db.query(HeadlineRevision, InfoSource.source_name)
|
||||
.join(TrendingEvent, HeadlineRevision.event_id == TrendingEvent.id)
|
||||
.join(InfoSource, TrendingEvent.source_id == InfoSource.id)
|
||||
.filter(HeadlineRevision.created_at >= time_limit)
|
||||
.order_by(HeadlineRevision.created_at.desc())
|
||||
.limit(limit)
|
||||
.all()
|
||||
)
|
||||
|
||||
# 平台名到图标的简单映射
|
||||
icon_map = {
|
||||
"微博热搜": "weibo",
|
||||
"知乎热榜": "zhihu",
|
||||
"百度热搜": "baidu",
|
||||
"今日头条": "toutiao",
|
||||
"抖音热榜": "douyin",
|
||||
"B站热搜": "bilibili",
|
||||
}
|
||||
|
||||
results: list[HeadlineRevisionResponse] = []
|
||||
for revision, source_name in rows:
|
||||
results.append(
|
||||
HeadlineRevisionResponse(
|
||||
id=revision.id,
|
||||
event_id=revision.event_id,
|
||||
previous_headline=revision.previous_headline,
|
||||
revised_headline=revision.revised_headline,
|
||||
source_name=source_name,
|
||||
platform_icon=icon_map.get(source_name, "newspaper"),
|
||||
created_at=revision.created_at,
|
||||
)
|
||||
)
|
||||
|
||||
return results
|
||||
Reference in New Issue
Block a user