This repository has been archived on 2026-01-07. You can view files and clone it, but cannot push or open issues or pull requests.
Files
resources/scrapy_proj/my_sqlalchemy/models/resources.py
2025-11-10 15:22:45 +08:00

123 lines
6.1 KiB
Python
Raw 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 sqlalchemy import Column, Integer, Text, String, Float, DateTime, ForeignKey, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, Mapped
from typing import List, Optional
# 基础模型基类(如果已有全局 Base 可直接复用)
ResourceBase = declarative_base()
class U3C3(ResourceBase):
"""对应 sqlite 中的 u3c3 表"""
__tablename__ = "u3c3" # 表名与原表保持一致
# 字段定义(严格映射原表结构)
id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
sites = Column(Text, comment="站点名称")
category = Column(Text, comment="分类")
title = Column(Text, comment="标题")
# 关键修改:给 String 加长度(如 512根据实际链接长度调整
url = Column(String(512), unique=True, comment="资源链接(唯一)")
torrent_url = Column(Text, comment="种子链接")
magnet_url = Column(Text, comment="磁力链接")
size_text = Column(Text, comment="大小文本描述")
size_gb = Column(Float, comment="大小GB")
update_date = Column(Text, comment="更新日期")
# 补充MySQL 中建议用 func.now() 替代 func.datetime(...),兼容性更好
created_at = Column(DateTime, default=func.now(), comment="创建时间(本地时间)")
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间(本地时间)")
def __repr__(self):
"""打印实例时显示的信息"""
return f"<U3c3(id={self.id}, title='{self.title[:20]}...')>"
class Sis(ResourceBase):
"""对应 MySQL 中的 sis 表"""
__tablename__ = "sis" # 表名与原表保持一致
# 字段定义(严格映射原表结构)
id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
plate_name = Column(Text, comment="板块名称")
title = Column(Text, comment="标题")
url = Column(String(512), unique=True, comment="资源链接(唯一)")
size_text = Column(Text, comment="大小文本描述")
size_gb = Column(Float, comment="大小GB")
update_date = Column(Text, comment="更新日期")
# 补充MySQL 中建议用 func.now() 替代 func.datetime(...),兼容性更好
created_at = Column(DateTime, default=func.now(), comment="创建时间(本地时间)")
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间(本地时间)")
def __repr__(self):
return f"<Sis(id={self.id}, title='{self.title[:20]}...')>"
class ClmIndex(ResourceBase):
"""对应 MySQL 中的 clm_index 表"""
__tablename__ = "clm_index" # 表名与原表保持一致
# 字段定义(严格映射原表结构)
id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
category = Column(Text, comment="分类")
title = Column(Text, comment="标题")
href = Column(String(512), unique=True, comment="资源链接(唯一)")
magnet_href = Column(Text, comment="磁力链接")
size_text = Column(Text, comment="大小文本描述")
size_gb = Column(Float, comment="大小GB")
heat = Column(Integer, default=0, comment="热度")
add_date = Column(Text, comment="添加日期")
last_down_date = Column(Text, comment="最后下载日期")
created_at = Column(DateTime, default=func.now(), comment="创建时间(本地时间)")
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间(本地时间)")
# 关系定义:用 Mapped[List["ClmKeywordsIndex"]] 替代 List["ClmKeywordsIndex"]
clm_keywords_index: Mapped[List["ClmKeywordsIndex"]] = relationship(
"ClmKeywordsIndex", back_populates="index"
)
def __repr__(self):
return f"<ClmIndex(id={self.id}, title='{self.title[:20]}...')>"
class ClmKeywords(ResourceBase):
"""对应 MySQL 中的 clm_keywords 表"""
__tablename__ = "clm_keywords" # 表名与原表保持一致
# 字段定义(严格映射原表结构)
id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
words = Column(String(512), unique=True, comment="关键词(唯一)")
groups = Column(Text, comment="关键词分组")
tags = Column(Text, comment="标签")
index_count = Column(Integer, default=0, comment="关联索引数量")
created_at = Column(DateTime, default=func.now(), comment="创建时间(本地时间)")
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间(本地时间)")
# 关系定义:用 Mapped[List["ClmKeywordsIndex"]] 替代 List["ClmKeywordsIndex"]
clm_keywords_index: Mapped[List["ClmKeywordsIndex"]] = relationship(
"ClmKeywordsIndex", back_populates="words"
)
def __repr__(self):
return f"<ClmKeywords(id={self.id}, words='{self.words[:20]}...')>"
class ClmKeywordsIndex(ResourceBase):
"""对应 MySQL 中的 clm_keywords_index 表(关联表)"""
__tablename__ = "clm_keywords_index" # 表名与原表保持一致
# 字段定义(严格映射原表结构)
id = Column(Integer, primary_key=True, autoincrement=True, comment="主键ID")
words_id = Column(Integer, ForeignKey("clm_keywords.id"), comment="关键词ID外键")
index_id = Column(Integer, ForeignKey("clm_index.id"), comment="索引ID外键")
wid_iid = Column(String(255), unique=True, comment="关键词与索引的关联标识")
tags = Column(Text, comment="关联标签")
created_at = Column(DateTime, default=func.now(), comment="创建时间(本地时间)")
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间(本地时间)")
# 关系定义:用 Mapped 包装单个对象类型
index: Mapped["ClmIndex"] = relationship("ClmIndex", back_populates="clm_keywords_index")
words: Mapped["ClmKeywords"] = relationship("ClmKeywords", back_populates="clm_keywords_index")
def __repr__(self):
return f"<ClmKeywordsIndex(id={self.id}, words_id={self.words_id}, index_id={self.index_id})>"