AI掘金头条新闻系统 (Toutiao News)-相关推荐

1. crud/news.py

python 复制代码
async def get_related_news(db: AsyncSession, news_id: int, category_id: int, limit: int = 5):
    """
    新闻详情-相关推荐
    """
    stmt = (
        select(News)
        .where(
            News.id != news_id,
            News.category_id == category_id
        )
        .order_by(
            News.views.desc(),
            News.publish_time.desc()
        )
        .limit(limit)
    )

    result = await db.execute(stmt)
    related_news = result.scalars().all()

    return [
        {
            "id": item.id,
            "title": item.title,
            "content": item.content,
            "image": item.image,
            "author": item.author,
            "publishTime": item.publish_time,
            "categoryId": item.category_id,
            "views": item.views
        }
        for item in related_news
    ]

2. routers/news.py

python 复制代码
    # 新闻详情-相关推荐
    related_news = await news.get_related_news(
        db,
        news_detail.id,
        news_detail.category_id
    )

    data = {
        "id": news_detail.id,
        "title": news_detail.title,
        "description": news_detail.description,
        "content": news_detail.content,
        "image": news_detail.image,
        "author": news_detail.author,
        "publishTime": news_detail.publish_time,
        "categoryId": news_detail.category_id,
        "views": news_detail.views + 1,
        "relatedNews": related_news
    }

    return Result.success(data)

完整代码

python 复制代码
@router.get("/detail", response_model=Result)
async def get_news_detail(
        news_id: int = Query(..., alias="id"),
        db: AsyncSession = Depends(get_db)
):
    """
    获取新闻详情、新闻浏览量 + 1、相关推荐
    """

    # 获取新闻详情
    news_detail = await news.get_news_detail(db, news_id)

    if news_detail is None:
        return Result.error(code=404, data="该新闻不存在")

    # 新闻浏览量 + 1
    news_res = await news.increase_new_views(db, news_id)

    if not news_res:
        return Result.error(code=404, data="更新浏览量失败")

    # 新闻详情-相关推荐
    related_news = await news.get_related_news(
        db,
        news_detail.id,
        news_detail.category_id
    )

    data = {
        "id": news_detail.id,
        "title": news_detail.title,
        "description": news_detail.description,
        "content": news_detail.content,
        "image": news_detail.image,
        "author": news_detail.author,
        "publishTime": news_detail.publish_time,
        "categoryId": news_detail.category_id,
        "views": news_detail.views + 1,
        "relatedNews": related_news
    }

    return Result.success(data)
相关推荐
云贝贝贝41 分钟前
腾讯云 TDSQL(MySQL 版)运维高频 6 坑:代理路由、读写分离、分片键、监控、PITR
运维·mysql·腾讯云
wxwx_bscxy32244 分钟前
基于Python新冠疫情可视化分析系统的设计与实现
java·数据库·spring boot·python·微信小程序·sqlite
TechWayfarer2 小时前
Cloudflare 9·15新政倒计时:用IP风险画像识别AI爬虫,防止误伤Googlebot
网络·人工智能·爬虫·python·tcp/ip·网络安全
禹凕2 小时前
滑动窗口算法实战指南
python·算法
论文复现现场2 小时前
RTX 4090 24GB 能跑 Qwen3.8-27B 吗?单卡显存计算与云端部署指南
人工智能·python·云计算·llama·gpu算力
Patrick在香港3 小时前
模型路由器实测:12 个任务省 77%,旗舰过载时的降级要打出显式标记
python·llm·智能路由器·api·架构设计·成本优化
Looooking3 小时前
Python 之 jwt 令牌生成和校验
python·jwt
IT_陈寒3 小时前
React的状态更新坑得我差点加班到天亮
前端·人工智能·后端
码事漫谈4 小时前
一天搭出 80% 的 Agent,然后卡在 95% 到死
后端
2601_962295994 小时前
嵌入式开发语言用哪个最好,C,python或者其他
c语言·python·嵌入式开发·开发效率·系统资源