Refactoring: extracting SQLAlchemy-related code to its own modules
This commit is contained in:
parent
edbb92ff9a
commit
a2dce2bba9
85
models.py
Normal file
85
models.py
Normal file
@ -0,0 +1,85 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import ForeignKey, func, Integer, Boolean, String, Text, DateTime
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
"""Base déclarative pour SQLAlchemy 2.0."""
|
||||
pass
|
||||
|
||||
|
||||
class TvShowDB(Base):
|
||||
"""Modèle pour le stockage des épisodes (SQLAlchemy 2.0)."""
|
||||
|
||||
__tablename__: str = "tvshows"
|
||||
|
||||
id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
primary_key=True,
|
||||
autoincrement=True
|
||||
)
|
||||
post_id: Mapped[str] = mapped_column(
|
||||
String(length=255),
|
||||
nullable=False,
|
||||
unique=True,
|
||||
index=True
|
||||
)
|
||||
post_title: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False
|
||||
)
|
||||
title: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False,
|
||||
index=True
|
||||
)
|
||||
date: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
nullable=False,
|
||||
index=True
|
||||
)
|
||||
summary: Mapped[str | None] = mapped_column(
|
||||
Text,
|
||||
nullable=True
|
||||
)
|
||||
image_url: Mapped[str | None] = mapped_column(
|
||||
String(255),
|
||||
nullable=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
server_default=func.datetime('now'),
|
||||
nullable=False
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
server_default=func.datetime('now'),
|
||||
onupdate=func.datetime('now'),
|
||||
nullable=False
|
||||
)
|
||||
links: Mapped[list["LinkDB"]] = relationship(back_populates="show")
|
||||
|
||||
|
||||
class LinkDB(Base):
|
||||
"""Modèle pour le stockage des liens de téléchargement (SQLAlchemy 2.0)."""
|
||||
|
||||
__tablename__: str = "links"
|
||||
|
||||
id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
primary_key=True,
|
||||
autoincrement=True
|
||||
)
|
||||
link: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False
|
||||
)
|
||||
is_downloaded: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
default=False
|
||||
)
|
||||
show_id: Mapped[int] = mapped_column(ForeignKey("tvshows.id"))
|
||||
show: Mapped["TvShowDB"] = relationship(back_populates="links")
|
||||
|
||||
|
||||
81
scrarls.py
81
scrarls.py
@ -10,88 +10,11 @@ from scrapy.crawler import CrawlerProcess
|
||||
from scrapy.spiders import CrawlSpider, Rule
|
||||
from scrapy.linkextractors import LinkExtractor
|
||||
|
||||
from sqlalchemy import ForeignKey, create_engine, func, select, Engine, Integer, Boolean, String, Text, DateTime
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||
from sqlalchemy import create_engine, select, Engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
"""Base déclarative pour SQLAlchemy 2.0."""
|
||||
pass
|
||||
|
||||
|
||||
class TvShowDB(Base):
|
||||
"""Modèle pour le stockage des épisodes (SQLAlchemy 2.0)."""
|
||||
|
||||
__tablename__: str = "tvshows"
|
||||
|
||||
id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
primary_key=True,
|
||||
autoincrement=True
|
||||
)
|
||||
post_id: Mapped[str] = mapped_column(
|
||||
String(length=255),
|
||||
nullable=False,
|
||||
unique=True,
|
||||
index=True
|
||||
)
|
||||
post_title: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False
|
||||
)
|
||||
title: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False,
|
||||
index=True
|
||||
)
|
||||
date: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
nullable=False,
|
||||
index=True
|
||||
)
|
||||
summary: Mapped[str | None] = mapped_column(
|
||||
Text,
|
||||
nullable=True
|
||||
)
|
||||
image_url: Mapped[str | None] = mapped_column(
|
||||
String(255),
|
||||
nullable=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
server_default=func.datetime('now'),
|
||||
nullable=False
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
server_default=func.datetime('now'),
|
||||
onupdate=func.datetime('now'),
|
||||
nullable=False
|
||||
)
|
||||
links: Mapped[list["LinkDB"]] = relationship(back_populates="show")
|
||||
|
||||
|
||||
class LinkDB(Base):
|
||||
"""Modèle pour le stockage des liens de téléchargement (SQLAlchemy 2.0)."""
|
||||
|
||||
__tablename__: str = "links"
|
||||
|
||||
id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
primary_key=True,
|
||||
autoincrement=True
|
||||
)
|
||||
link: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False
|
||||
)
|
||||
is_downloaded: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
default=False
|
||||
)
|
||||
show_id: Mapped[int] = mapped_column(ForeignKey("tvshows.id"))
|
||||
show: Mapped["TvShowDB"] = relationship(back_populates="links")
|
||||
from models import Base, TvShowDB, LinkDB
|
||||
|
||||
|
||||
class TvShowItem(scrapy.Item):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user