This commit is contained in:
87
database/thottopic/crud.py
Normal file
87
database/thottopic/crud.py
Normal file
@ -0,0 +1,87 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import func
|
||||
|
||||
from database.thottopic.model import THotTopic
|
||||
|
||||
|
||||
def create_hot_topic(db, hot_topic: THotTopic):
|
||||
db.add(hot_topic)
|
||||
db.commit()
|
||||
db.refresh(hot_topic)
|
||||
return hot_topic
|
||||
|
||||
|
||||
# 插入数据库之前判断数据库中是否已经存在,根据news.url 判断
|
||||
def create_topic_if_url_not_exists(db, hot_topic: THotTopic):
|
||||
# 检查是否已经存在具有相同 URL 的记录
|
||||
existing_topic = db.query(THotTopic).filter(THotTopic.url == hot_topic.url).first()
|
||||
|
||||
if existing_topic:
|
||||
# 如果记录已存在,直接返回已有的记录
|
||||
return existing_topic
|
||||
|
||||
# 如果记录不存在,插入新的记录
|
||||
db.add(hot_topic)
|
||||
db.commit()
|
||||
db.refresh(hot_topic)
|
||||
return hot_topic
|
||||
|
||||
|
||||
def create_topics_if_url_not_exists(db, topics: list[THotTopic]):
|
||||
inserted_topics = [] # 用于保存实际插入的新闻记录
|
||||
|
||||
for topic in topics:
|
||||
# 检查是否已经存在具有相同 URL 的记录
|
||||
existing_topic = db.query(THotTopic).filter(THotTopic.url == topic.url).first()
|
||||
|
||||
if not existing_topic:
|
||||
# 如果记录不存在,插入新的记录
|
||||
db.add(topic)
|
||||
inserted_topics.append(topic)
|
||||
|
||||
# 批量提交所有插入的记录
|
||||
db.commit()
|
||||
|
||||
# 刷新所有新插入的记录
|
||||
for topic in inserted_topics:
|
||||
db.refresh(topic)
|
||||
|
||||
return inserted_topics
|
||||
|
||||
|
||||
def get_hot_topic_by_id(db, hot_topic_id: int):
|
||||
return db.query(THotTopic).filter(THotTopic.id == hot_topic_id).first()
|
||||
|
||||
|
||||
def get_hot_topics(db, skip: int = 0, limit: int = 100):
|
||||
return db.query(THotTopic).offset(skip).limit(limit).all()
|
||||
|
||||
# 根据THotTopic.update_time排序,获取最新的THotTopic
|
||||
def get_latest_hot_topic(db):
|
||||
return db.query(THotTopic).order_by(THotTopic.update_time.desc()).first()
|
||||
|
||||
# 根据THotTopic.create_time获取今日的所有THotTopic
|
||||
def get_today_hot_topic(db):
|
||||
today = datetime.now().date()
|
||||
return db.query(THotTopic).filter(func.date(THotTopic.create_time) == today).all()
|
||||
|
||||
def update_hot_topic(db, hot_topic: THotTopic):
|
||||
db.merge(hot_topic)
|
||||
db.commit()
|
||||
db.refresh(hot_topic)
|
||||
return hot_topic
|
||||
|
||||
|
||||
# def update_hot_topic(db, hot_topic_id: int, updates: dict):
|
||||
# db.query(THotTopic).filter(THotTopic.id == hot_topic_id).update(updates)
|
||||
# db.commit()
|
||||
# return db.query(THotTopic).filter(THotTopic.id == hot_topic_id).first()
|
||||
|
||||
|
||||
def delete_hot_topic(db, hot_topic_id: int):
|
||||
hot_topic = db.query(THotTopic).filter(THotTopic.id == hot_topic_id).first()
|
||||
if hot_topic:
|
||||
db.delete(hot_topic)
|
||||
db.commit()
|
||||
return hot_topic
|
||||
34
database/thottopic/model.py
Normal file
34
database/thottopic/model.py
Normal file
@ -0,0 +1,34 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import Column, String, Integer, TIMESTAMP, func
|
||||
from sqlalchemy.dialects.postgresql import BIGINT
|
||||
|
||||
from database.database import Base
|
||||
|
||||
|
||||
@dataclass
|
||||
class THotTopic(Base):
|
||||
__tablename__ = 't_hot_topic'
|
||||
|
||||
id: int = Column(BIGINT, primary_key=True, autoincrement=True, comment='序号')
|
||||
topic: str = Column(String, nullable=False, comment='话题')
|
||||
topic_description: Optional[str] = Column(String, nullable=True, comment='话题描述')
|
||||
url: Optional[str] = Column(String, nullable=True, comment='话题链接')
|
||||
source: Optional[str] = Column(String, nullable=True, comment='话题来源')
|
||||
keywords: Optional[str] = Column(String, nullable=True, comment='话题关键词')
|
||||
content_count: int = Column(Integer, default=0, nullable=False, comment='话题内容数量')
|
||||
comment_count: int = Column(Integer, default=0, nullable=False, comment='话题评论数量')
|
||||
follower_count: int = Column(Integer, default=0, nullable=False, comment='话题关注者数量')
|
||||
date_created: Optional[datetime] = Column(TIMESTAMP(timezone=True), nullable=True, comment='话题创建时间')
|
||||
date_modified: Optional[datetime] = Column(TIMESTAMP(timezone=True), nullable=True, comment='话题修改时间')
|
||||
top_content_url: Optional[str] = Column(String, nullable=True, comment='热内内容链接')
|
||||
top_content_upvote_count: Optional[int] = Column(BIGINT, nullable=True, comment='热门内容点赞数量')
|
||||
top_content_comment_count: Optional[int] = Column(Integer, nullable=True, comment='热门内容评论数量')
|
||||
create_time: datetime = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False, comment='创建时间')
|
||||
update_time: Optional[datetime] = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False, comment='更新时间')
|
||||
ai_script: Optional[str] = Column(String, nullable=True, comment='内容脚本')
|
||||
|
||||
def __repr__(self):
|
||||
return f"<THotTopic(topic={self.topic}, url={self.url}, id={self.id}, source={self.source}, content_count={self.content_count})>"
|
||||
Reference in New Issue
Block a user