# smallzombie/bilibili-favorite-sync architecture

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

- Interactive diagram: https://gitdiagram.com/smallzombie/bilibili-favorite-sync
- Repository: https://github.com/smallzombie/bilibili-favorite-sync

## Overview

##### Main components
- CLI Interface: Represented by files such as index.js, CLI.js, and the command subdirectory (with multiple CommandNode files such as HelpCommandNode.js, StartCommandNode.js, StopCommandNode.js, etc.). This is the entry point and command interpreter.
- Main Controller: Main.js acts as the core component that likely manages overall workflow and coordination of sync tasks.
- Push Service: PushService.js is responsible for handling notifications (for example, video expiration notices).
- Command Management: In the “src/command” folder, CommandManager.js coordinates various command nodes, each implementing specific commands like start, pause, reload, and library management (blacklist, whitelist, export, clean, etc).
- Configuration Module: The “src/config/Global.js” manages global settings for the synchronization process.
- Utility Modules: Under “src/util” there are multiple utility classes (BaseUtil, BiliUtil, DownloadUtil, LibraryUtil, Logger, RequestUtil) – these help with logging, communication with Bilibili API, downloading content, handling files, and more.

##### Relationships and interactions
- The CLI Interface receives user commands and passes them to the Command Manager.
- The Command Manager, by interpreting the command nodes, triggers operations in the Main Controller.
- The Main Controller coordinates workflow and can call into the Push Service to send notifications.
- Utility modules wrap around common tasks (logging, downloading, HTTP requests, etc.) that may be utilized by both the Main Controller and individual command nodes.
- The Global configuration module ensures consistent settings are applied across various operations.
- Additionally, the tool interacts with the local library structure (as described in the README under the library section) to read/write JSON files, manage directories for favorites, videos, recycled files, etc.

##### Important architectural patterns and principles
- Modular design: The separation of concerns is evident – the CLI, command parsing, synchronization logic, utility functions, and configuration are each contained in dedicated modules.
- Command Pattern: With multiple command nodes and a Command Manager, the project likely employs the Command design pattern to process different user actions.
- Layered Architecture: The tool is organized into layers where the CLI layer handles input, a business logic layer (Main and PushService) processes operations, and a utility layer handles ancillary tasks.

##### Technologies, frameworks, and libraries
- Node.js is the main runtime environment (as seen with index.js and package.json).
- Various custom utilities (e.g., for networking and downloads) portray the lightweight and specific use of libraries or in-house solutions rather than major frameworks.
- The README suggests timing and retry logic, implying careful handling of external HTTP requests and API rate limiting.

##### Detail
- Every major file grouping (CLI, Command Management, Utility, Core Sync, Config) is represented.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    U("User"):::user

    subgraph "CLI Layer"
        idx("CLI Entry"):::cli
        cli("CLI Implementation"):::cli
        subgraph "Command Nodes"
            help("Help Command"):::command
            start("Start Command"):::command
            stop("Stop Command"):::command
            pause("Pause Command"):::command
            lib("Library Command"):::command
            libBlack("Library BlackList Command"):::command
            libWhite("Library WhiteList Command"):::command
            libExport("Library Export Command"):::command
            libRec("Library Recycle Command"):::command
            reload("Reload Command"):::command
        end
    end

    cm("Command Manager"):::management
    mc("Main Controller"):::core
    ps("Push Service"):::core
    config("Global Configuration"):::config

    subgraph "Utility Modules"
        base("BaseUtil"):::util
        bili("BiliUtil"):::util
        download("DownloadUtil"):::util
        library("LibraryUtil"):::util
        logger("Logger"):::util
        request("RequestUtil"):::util
    end

    ls("Local Storage"):::storage

    U -->|"issues_command"| idx
    idx -->|"initiates"| cli
    cli -->|"parses_commands"| cm

    cm -->|"executes:Help"| help
    cm -->|"executes:Start"| start
    cm -->|"executes:Stop"| stop
    cm -->|"executes:Pause"| pause
    cm -->|"executes:Library"| lib
    cm -->|"executes:LibraryBlackList"| libBlack
    cm -->|"executes:LibraryWhiteList"| libWhite
    cm -->|"executes:LibraryExport"| libExport
    cm -->|"executes:LibraryRecycle"| libRec
    cm -->|"executes:Reload"| reload

    cm -->|"delegates_to"| mc
    mc -->|"triggers_notification"| ps
    mc -->|"reads_config"| config
    cm -->|"reads_config"| config

    mc -->|"uses:BaseUtil"| base
    mc -->|"uses:BiliUtil"| bili
    mc -->|"uses:DownloadUtil"| download
    mc -->|"uses:Logger"| logger
    mc -->|"uses:RequestUtil"| request
    cm -->|"uses:Logger"| logger

    mc -->|"syncs_library"| ls
    library ---|"manages_files"| ls

    classDef cli fill:#AED6F1,stroke:#21618C,stroke-width:2px;
    classDef command fill:#D6EAF8,stroke:#1B4F72,stroke-width:2px;
    classDef management fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef core fill:#A9DFBF,stroke:#1E8449,stroke-width:2px;
    classDef config fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef util fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
    classDef storage fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef user fill:#F5CBA7,stroke:#BA4A00,stroke-width:2px;

    click idx "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/index.js"
    click cli "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/CLI.js"
    click help "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/HelpCommandNode.js"
    click start "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/StartCommandNode.js"
    click stop "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/StopCommandNode.js"
    click pause "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/PauseCommandNode.js"
    click lib "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/LibraryCommandNode.js"
    click libBlack "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/LibraryBlackListCommandNode.js"
    click libWhite "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/LibraryWhiteListCommandNode.js"
    click libExport "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/LibraryExportCommandNode.js"
    click libRec "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/LibraryRecCommandNode.js"
    click reload "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/ReloadCommandNode.js"
    click cm "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/command/CommandManager.js"
    click mc "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/Main.js"
    click ps "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/PushService.js"
    click config "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/config/Global.js"
    click base "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/util/BaseUtil.js"
    click bili "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/util/BiliUtil.js"
    click download "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/util/DownloadUtil.js"
    click library "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/util/LibraryUtil.js"
    click logger "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/util/Logger.js"
    click request "https://github.com/smallzombie/bilibili-favorite-sync/blob/main/src/util/RequestUtil.js"
```
