Files
konjacpotato 5267db8a0d
All checks were successful
Gitea Actions Demo / deploy (push) Successful in 15s
import edward
2025-11-12 21:19:26 +08:00

77 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from database.thotcontent.model import THotContent
from log.log_manager import logger
def create_hot_content(db, hot_content: THotContent):
db.add(hot_content)
db.commit()
db.refresh(hot_content)
return hot_content
# 插入数据库之前判断数据库中是否已经存在根据news.url 判断
def create_content_if_url_not_exists(db, hot_content: THotContent):
# 检查是否已经存在具有相同 URL 的记录
existing_content = db.query(THotContent).filter(THotContent.url == hot_content.url).first()
if existing_content:
# 如果记录已存在,直接返回已有的记录
return existing_content
# 如果记录不存在,插入新的记录
db.add(hot_content)
db.commit()
db.refresh(hot_content)
return hot_content
def create_contents_top3_if_url_not_exists(db, contents: list[THotContent]):
logger.info(f"采集到内容数量:{len(contents)},存入数据库前三")
# 按照 THotContent.content_upvote_count 对contents进行排序
contents.sort(key=lambda x: x.content_upvote_count, reverse=True)
# 保留 contents 的前3条
contents = contents[:3]
inserted_contents = [] # 用于保存实际插入的新闻记录
for content in contents:
# 检查是否已经存在具有相同 URL 的记录
existing_content = db.query(THotContent).filter(THotContent.url == content.url).first()
if not existing_content:
# 如果记录不存在,插入新的记录
db.add(content)
inserted_contents.append(content)
# 批量提交所有插入的记录
db.commit()
# 刷新所有新插入的记录
for content in inserted_contents:
db.refresh(content)
return inserted_contents
def get_hot_content_by_id(db, hot_content_id: int):
return db.query(THotContent).filter(THotContent.id == hot_content_id).first()
def get_hot_content_by_topic_id(db, topic_id: int):
return db.query(THotContent).filter(THotContent.topic_id == topic_id).all()
def get_hot_contents(db, skip: int = 0, limit: int = 100):
return db.query(THotContent).offset(skip).limit(limit).all()
def update_hot_content(db, hot_content_id: int, updates: dict):
hot_content = db.query(THotContent).filter(THotContent.id == hot_content_id).first()
if hot_content:
for key, value in updates.items():
setattr(hot_content, key, value)
db.commit()
db.refresh(hot_content)
return hot_content
def delete_hot_content(db, hot_content_id: int):
hot_content = db.query(THotContent).filter(THotContent.id == hot_content_id).first()
if hot_content:
db.delete(hot_content)
db.commit()