import peter

This commit is contained in:
konjacpotato
2025-11-12 20:42:16 +08:00
commit 8c1a740f0b
147 changed files with 2763 additions and 0 deletions

View File

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,90 @@
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 hot_topic_not_exists(db, url_list: list) -> list:
"""
url如果在数据库中已经存在则去除掉
:param db:
:param url_list:
:return:
"""
hot_topics = db.query(THotTopic).filter(THotTopic.url.in_(url_list)).all()
for hot_topic in hot_topics:
url_list.remove(hot_topic.url)
return url_list
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()
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

View 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})>"