# amtoaer/bili-sync architecture

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

- Interactive diagram: https://gitdiagram.com/amtoaer/bili-sync
- Repository: https://github.com/amtoaer/bili-sync

## Overview

##### Main Components
- Core Application Logic (crates/bili_sync):
– Contains modules for handling authentication, API endpoints (handling REST requests), configuration parsing, and task execution.
– Submodules include adapters (for converting different content types like “collection,” “favorite,” “submission,” and “watch later”), bilibili client implementations (networking with Bilibili services, including authentication and video streaming selection), and downloader tasks.
- Data Model and Persistence (crates/bili_sync_entity):
– Holds entity definitions, representing the data structures (collections, favorites, videos, etc.) used across the system.
- Database Migration and Schema Management (crates/bili_sync_migration):
– Manages database migrations, ensuring that the schema stays in sync with the codebase.
- Async Task Scheduling and HTTP Server (inside src/task):
– Uses Tokio and Reqwest to concurrently download, refresh tokens, and handle HTTP requests (likely for control or monitoring).
- External Dependencies/Integrations:
– Integration with external tools like FFmpeg (for merging audio/video streams), and signaling/logging for error handling.
- Web UI and Documentation:
– The “web” directory contains build assets that might be used to provide a status or management UI, and the “docs” directory holds detailed documentation and design guides.

- User Interface / Web Client (the “web” folder, possibly serving a UI dashboard).
- API / HTTP Server (within the task and API submodules in the core “bili_sync” crate).
- Core Logic and Download Manager (including the downloader, bilibili client, workflow, and adapter modules).
- Configuration & Credential Management (configuration modules handling CLI parameters and persistent settings).
- Database and ORM Layer (linking the core application logic with the persistence layer – including migrations and entities).
- External Systems (Bilibili’s API endpoints, FFmpeg for postprocessing, and potentially logging/monitoring services).
- Data flows from the API/HTTP server to the core logic, which then interacts with external Bilibili services.
- The core logic reads configuration and credentials, initiates downloads, processes and merges streams via FFmpeg, and then updates the database.
- Additionally, there may be periodic/polling/retry loops, so indicate channels for error reporting and scheduling.
- External dependencies:
- Labels for technologies such as Rust, Tokio, Reqwest, and FFmpeg.

##### Architectural Patterns and Design Principles
- Separation of Concerns/Micro-crates:
- Each crate (bili_sync, bili_sync_entity, bili_sync_migration) is responsible for a specific domain: application logic, data model, and database migration, respectively.
- Asynchronous/Concurrent Execution:
- Adapter Pattern:
- The “adapter” submodules show transformation between different domains (e.g., converting raw API responses from Bilibili into internal data representations).
- Modular and Extensible Design:
- The clear separation into APIs, configuration, tasks, and utilities suggests the application is designed with modularity in mind, allowing for extension and easy maintenance.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Core System
    subgraph "Core System"
        BSCore["BiliSync Core"]:::rust
        TaskMgr["Task Manager"]:::rust
        BSCore -->|"schedules_tasks"| TaskMgr
    end

    %% Database Layer
    subgraph "Database Layer"
        DataModels["Data Models"]:::rust
        MigrationMgr["Migration Manager"]:::rust
        DataModels <-->|"schema_sync"| MigrationMgr
    end

    %% External Services
    subgraph "External Services"
        BilibiliAPI["Bilibili API"]:::external
        FFmpeg["FFmpeg"]:::external
        Logging["Logging & Monitoring"]:::external
    end

    %% UI & Documentation
    subgraph "UI & Documentation"
        WebUI["Web UI"]:::ui
        Docs["Documentation"]:::ui
    end

    %% Connections between layers
    BSCore -->|"updates"| DataModels
    BSCore -->|"fetchData"| BilibiliAPI
    BSCore -->|"processStreams"| FFmpeg
    BSCore -->|"dashboard"| WebUI
    TaskMgr -->|"reportStatus"| Logging

    %% Style Definitions
    classDef rust fill:#a8e6cf,stroke:#379683,stroke-width:2px
    classDef external fill:#add8e6e,stroke:#0000ff,stroke-width:2px
    classDef ui fill:#d3d3d3,stroke:#696969,stroke-width:2px

    %% Click Events
    click BSCore "https://github.com/amtoaer/bili-sync/tree/main/crates/bili_sync"
    click DataModels "https://github.com/amtoaer/bili-sync/tree/main/crates/bili_sync_entity"
    click MigrationMgr "https://github.com/amtoaer/bili-sync/tree/main/crates/bili_sync_migration"
    click TaskMgr "https://github.com/amtoaer/bili-sync/tree/main/crates/bili_sync/src/task"
    click WebUI "https://github.com/amtoaer/bili-sync/tree/main/web"
    click Docs "https://github.com/amtoaer/bili-sync/tree/main/docs"
```
