import peter
This commit is contained in:
0
database/tnews/__init__.py
Normal file
0
database/tnews/__init__.py
Normal file
BIN
database/tnews/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
database/tnews/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
database/tnews/__pycache__/crud.cpython-312.pyc
Normal file
BIN
database/tnews/__pycache__/crud.cpython-312.pyc
Normal file
Binary file not shown.
BIN
database/tnews/__pycache__/model.cpython-312.pyc
Normal file
BIN
database/tnews/__pycache__/model.cpython-312.pyc
Normal file
Binary file not shown.
87
database/tnews/crud.py
Normal file
87
database/tnews/crud.py
Normal file
@ -0,0 +1,87 @@
|
||||
from database.tnews.model import TNews
|
||||
|
||||
|
||||
def create_news(db, news: TNews):
|
||||
db.add(news)
|
||||
db.commit()
|
||||
db.refresh(news)
|
||||
return news
|
||||
|
||||
|
||||
# 插入数据库之前判断数据库中是否已经存在,根据news.url 判断
|
||||
def create_news_if_url_not_exists(db, news: TNews):
|
||||
# 检查是否已经存在具有相同 URL 的记录
|
||||
existing_news = db.query(TNews).filter(TNews.url == news.url).first()
|
||||
|
||||
if existing_news:
|
||||
# 如果记录已存在,直接返回已有的记录
|
||||
return existing_news
|
||||
|
||||
# 如果记录不存在,插入新的记录
|
||||
db.add(news)
|
||||
db.commit()
|
||||
db.refresh(news)
|
||||
return news
|
||||
|
||||
|
||||
def create_news_list_if_url_not_exists(db, news_list: list[TNews]):
|
||||
inserted_news = [] # 用于保存实际插入的新闻记录
|
||||
|
||||
for news in news_list:
|
||||
# 检查是否已经存在具有相同 URL 的记录
|
||||
existing_news = db.query(TNews).filter(TNews.url == news.url).first()
|
||||
|
||||
if not existing_news:
|
||||
# 如果记录不存在,插入新的记录
|
||||
db.add(news)
|
||||
inserted_news.append(news)
|
||||
|
||||
# 批量提交所有插入的记录
|
||||
db.commit()
|
||||
|
||||
# 刷新所有新插入的记录
|
||||
for news in inserted_news:
|
||||
db.refresh(news)
|
||||
|
||||
return inserted_news
|
||||
|
||||
|
||||
def get_news_by_id(db, news_id: int):
|
||||
return db.query(TNews).filter(TNews.id == news_id).first()
|
||||
|
||||
def get_news_need_content(db):
|
||||
return db.query(TNews).filter(TNews.content == None).all()
|
||||
|
||||
def get_news_need_summary(db):
|
||||
return db.query(TNews).filter(TNews.ai_summary == None).all()
|
||||
|
||||
|
||||
def get_news_for_generate_reference_message(db, news_type: str) -> list[TNews]:
|
||||
return db.query(TNews).filter(
|
||||
TNews.type == news_type,
|
||||
TNews.ai_summary != None,
|
||||
TNews.is_usage == False
|
||||
).order_by(TNews.occurrence_date.desc()).all()
|
||||
|
||||
|
||||
def update_news_by_id(db, news: TNews):
|
||||
db.merge(news)
|
||||
db.commit()
|
||||
|
||||
|
||||
def update_news(db, news_id: int, updates: dict):
|
||||
news = db.query(TNews).filter(TNews.id == news_id).first()
|
||||
if news:
|
||||
for key, value in updates.items():
|
||||
setattr(news, key, value)
|
||||
db.commit()
|
||||
db.refresh(news)
|
||||
return news
|
||||
|
||||
|
||||
def delete_news(db, news_id: int):
|
||||
news = db.query(TNews).filter(TNews.id == news_id).first()
|
||||
if news:
|
||||
db.delete(news)
|
||||
db.commit()
|
||||
return news
|
||||
25
database/tnews/model.py
Normal file
25
database/tnews/model.py
Normal file
@ -0,0 +1,25 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import Column, String, Boolean, DateTime, BigInteger, text, INT
|
||||
from database.database import Base
|
||||
|
||||
@dataclass
|
||||
class TNews(Base):
|
||||
__tablename__ = 't_news'
|
||||
|
||||
id: int = Column(BigInteger, primary_key=True, autoincrement=True, comment='编号')
|
||||
title: Optional[str] = Column(String, nullable=True, comment='标题')
|
||||
summary: Optional[str] = Column(String, nullable=True, comment='摘要')
|
||||
url: Optional[str] = Column(String, nullable=True, comment='链接')
|
||||
content: Optional[str] = Column(String, nullable=True, comment='内容/正文')
|
||||
occurrence_date: Optional[datetime] = Column(DateTime(timezone=True), nullable=True, comment='发布日期')
|
||||
source: Optional[str] = Column(String, nullable=True, comment='来源')
|
||||
primary_category: str = Column(String, nullable=True, comment='一级类别')
|
||||
secondary_category: str = Column(String, nullable=True, comment='二级类别')
|
||||
tertiary_category: str = Column(String, nullable=True, comment='三级类别')
|
||||
label: str = Column(String, nullable=True, comment='标签')
|
||||
lang: str = Column(String, nullable=False, default='zh', comment='语言')
|
||||
is_usage: bool = Column(Boolean, nullable=False, default=False, server_default=text('false'), comment='是否已用')
|
||||
create_time: datetime = Column(DateTime(timezone=True), nullable=False, server_default=text('now()'), comment='创建日期')
|
||||
Reference in New Issue
Block a user