# baabaaox/scrapydouban architecture

> Architecture diagram of the GitHub repository baabaaox/scrapydouban, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/baabaaox/scrapydouban
- Repository: https://github.com/baabaaox/scrapydouban

## Overview

##### Main Components Identification
- Scrapy (Douban crawler) Service – This is the core of the system. It contains the spiders which are responsible for scraping subject IDs, metadata, and comments. Within the “scrapy/douban” folder, you have modules for setting up items, pipelines, settings, and various spiders.
- Docker-based Containers – There are three containers per the README:
- douban_scrapyd: Runs the Python 3.9 environment with Scrapy, Scrapyd (a service to deploy and schedule crawls), and additional libraries. It exposes port 6800 for the Scrapyd interface.
- douban_db: A MySQL 8 container that stores the scraped data. Its database is initialized with an SQL dump located in “docker/mysql/douban.sql.”
- douban_adminer: Runs Adminer on port 8080 for easy database administration.
- Data Storage – The image downloads are saved in a specific directory (in the scrapyd container at “/srv/ScrapyDouban/storage”) as handled by the pipeline.
- Proxy IP Support – Although not enabled by default, there’s a middleware defined (ProxyMiddleware) hinting at support for proxy IPs to overcome anti-crawling mechanisms.

##### Component Relationships and Data Flow
- The User/Developer interacts through the Docker Compose interface. When containers are started, the Scrapy project (deployed on the douban_scrapyd container) connects to the MySQL container (douban_db) to persist metadata and comments.
- The spiders execute in the Scrapy service, process and download images via the pipeline (CoverPipeline), and store results in the MySQL database.
- The Adminer container (douban_adminer) provides an external UI to query and manage the MySQL data.
- Optionally, if using the scrapyd-client, the project can be deployed remotely on the douban_scrapyd container for scheduling tasks.

##### Architectural Patterns and Design Principles
- Containerization: Docker isolates each service (the crawler, database, admin interface) for modularity and ease of deployment.
- Separation of Concerns: The Scrapy layer is strictly responsible for crawling, processing, and storing data, while persistence is centralized in the MySQL container.
- Pipeline-based Processing: The Scrapy project uses pipelines to process items (e.g., image downloads and data validation), emphasizing a decoupled data processing design.

a. Components:
- Douban_Scrapyd Container: Inside, show sub-components like:
- Spiders: movie_subject, movie_meta, movie_comment, book_subject, book_meta, book_comment.
- Pipelines: CoverPipeline for image downloads.
- Settings & Middlewares: ProxyMiddleware (optional proxy) and other configuration files.
- MySQL Database Container (douban_db): Mark that it’s initialized with “douban.sql” and stores scraped data.
- Adminer Container (douban_adminer): Use as a UI for database management.
b. Relationships:
c. Technology Stack Labels:
- Python3, Scrapy, Scrapyd (in douban_scrapyd).
- MySQL 8 for persistence.
- Docker as the orchestration technology.
- Adminer for database management.
d. Interaction Flow:
- Typical flow: Crawling (subject collection → metadata capture → comments fetching) with subsequent pipeline processing (image download) and then data storage in MySQL.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["User/Developer"]:::external
    subgraph "Docker Compose Orchestrator"
        DC["Docker Compose (docker/docker-compose.yml)"]:::orchestrator
    end

    A --> DC

    subgraph "douban_scrapyd Container"
        direction TB
        SC["Core Scrapy Project"]:::app
        SCFG["Scrapyd Config"]:::config
        SP["Spiders (movie_subject, movie_meta, movie_comment, book_subject, book_meta, book_comment)"]:::module
        PL["Pipelines (CoverPipeline)"]:::module
        ST["Settings"]:::module
        MW["Middlewares"]:::module
        IMG["Image Storage (/srv/ScrapyDouban/storage)"]:::storage
    end

    subgraph "douban_db Container"
        DB["MySQL Database"]:::database
    end

    subgraph "douban_adminer Container"
        AD["Adminer UI (Port 8080)"]:::ui
    end

    DC --> SC
    DC --> DB
    DC --> AD

    SC --> SP
    SP --> PL
    PL --> DB
    PL --> IMG

    A --> SC
    A --> AD

    click SC "https://github.com/baabaaox/scrapydouban/tree/master/scrapy/douban"
    click SP "https://github.com/baabaaox/scrapydouban/tree/master/scrapy/douban/spiders"
    click PL "https://github.com/baabaaox/scrapydouban/blob/master/scrapy/douban/pipelines.py"
    click ST "https://github.com/baabaaox/scrapydouban/blob/master/scrapy/douban/settings.py"
    click MW "https://github.com/baabaaox/scrapydouban/blob/master/scrapy/douban/middlewares.py"
    click SCFG "https://github.com/baabaaox/scrapydouban/tree/master/docker/scrapyd"
    click DC "https://github.com/baabaaox/scrapydouban/blob/master/docker/docker-compose.yml"
    click DB "https://github.com/baabaaox/scrapydouban/tree/master/docker/mysql"

    classDef external fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef orchestrator fill:#AED581,stroke:#333,stroke-width:2px;
    classDef app fill:#BBDEFB,stroke:#333,stroke-width:2px;
    classDef module fill:#C5E1A5,stroke:#333,stroke-width:2px;
    classDef config fill:#F8BBD0,stroke:#333,stroke-width:2px;
    classDef database fill:#FFE082,stroke:#333,stroke-width:2px;
    classDef ui fill:#B2DFDB,stroke:#333,stroke-width:2px;
    classDef storage fill:#D7CCC8,stroke:#333,stroke-width:2px;
```
