# nya-wsl/bili_rec_monitor architecture

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

- Interactive diagram: https://gitdiagram.com/nya-wsl/bili_rec_monitor
- Repository: https://github.com/nya-wsl/bili_rec_monitor

## Overview

##### Project type and purpose
- This is a Python-based background service (not a web frontend) whose job is to monitor Bilibili live streams, react to state‐change webhooks emitted by BililiveRecorder, send notifications via the WxPusher WeChat service, manage local recording files, and optionally upload them to Baidu Netdisk using an OpenAPI client.

##### File structure
- **Entry point**:
- bili_rec_monitor.py – the main service script
- **Configuration**:
- config.example.yml – holds all user‐tweakable settings (rooms, users, commands, Baidu credentials, WxPusher tokens)
- **Auth & Upload**:
- pcs_auth.py – Baidu Netdisk authentication helper
- pcs.py – wrapper around Baidu OpenAPI calls (file upload, move)
- openapi_client/… – auto-generated OpenAPI client code (models, API endpoints)
- **Dependencies & Builds**:
- requirements.txt / pyproject.toml – Python dependencies (watchdog optional, requests, pyyaml, etc.)
- **External or auxiliary tools**:
- Potential use of pm2 or systemd to daemonize / watch the process

##### Extract architecture hints from the README
- The service subscribes to webhooks from BililiveRecorder.
- Once an event arrives, it triggers zero or more of:
a) send “live start/end” and “recording start/end/write start/end” notifications to multiple WxPusher recipients
b) execute custom commands (user-defined hooks)
c) move recordings on the local filesystem
d) upload recordings to Baidu Netdisk (with authentication managed by pcs_auth + openapi_client)
- Supports multiple users and multiple rooms concurrently.

- BililiveRecorder (webhook emitter)
- WxPusher (WeChat Service API)
- Baidu Netdisk (OpenAPI REST endpoints)
1. HTTP Webhook Receiver – part of bili_rec_monitor.py
2. Event Dispatcher / Handler Registry – routes each event type
3. Notification Module – wraps HTTP calls to WxPusher using tokens from config
4. Command Executor – runs user‐configured shell commands
5. File Manager – moves/renames files on local disk
6. Upload Module – pcs_auth + pcs.py + openapi_client handles OAuth and file uploads
7. Configuration Loader – reads config.yml into memory at startup
- **Local Environment**
- File System – for temporary or final storage of recordings
- Logs – capture events, errors, HTTP requests

- BililiveRecorder → (HTTP POST) → Webhook Receiver
- Webhook Receiver → Event Dispatcher
- Dispatcher → Notification Module → WxPusher → WeChat users
- Dispatcher → Command Executor → Operating System
- Dispatcher → File Manager → Local FS
- Dispatcher → Upload Module → pcs_auth → OpenAPI Client → Baidu Netdisk
- Bi-directional auth/token flows between Upload Module and Baidu Netdisk

##### Architectural patterns & important principles
- **Event‐driven**: Each webhook is an event triggering multiple independent side-effects
- **Modular**: Separate concerns (notification vs. file operations vs. upload vs. commands)
- **Configuration-driven**: Behavior flexibly controlled via YAML
- **External integration points**: clearly separate external APIs into their own modules

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "External Systems"
        BL["BililiveRecorder"]:::external
        WP["WxPusher (WeChat Service API)"]:::external
        BD["Baidu Netdisk (OpenAPI REST)"]:::external
    end

    subgraph "Local Environment"
        FS["File System"]:::datastore
        LG["Logs"]:::datastore
    end

    subgraph "Process Supervisor"
        PM["pm2 / systemd"]:::config
    end

    subgraph "Monitor Service"
        CFG["Configuration Loader"]:::config
        WR["HTTP Webhook Receiver"]:::internal
        ED["Event Dispatcher / Handler Registry"]:::internal
        NM["Notification Module"]:::internal
        CE["Command Executor"]:::internal
        FM["File Manager"]:::internal
        AM["Upload Module"]:::internal
        PA["pcs_auth (Auth Helper)"]:::internal
        PC["pcs.py (API Wrapper)"]:::internal
        OC["openapi_client/"]:::internal
        REQ["requirements.txt"]:::config
        PY["pyproject.toml"]:::config
    end

    BL -->|"HTTP POST /webhook"| WR
    WR -->|"parse and route event"| ED
    ED -->|"send notifications"| NM
    NM -->|"HTTP REST"| WP
    ED -->|"execute hooks"| CE
    ED -->|"file operations"| FM
    FM -->|"move/rename"| FS
    ED -->|"trigger upload"| AM
    AM -->|"authenticate"| PA
    PA -->|"OAuth tokens"| OC
    OC -->|"upload files"| BD

    WR -->|writes logs| LG
    ED -->|writes logs| LG
    NM -->|writes logs| LG
    CE -->|writes logs| LG
    FM -->|writes logs| LG
    AM -->|writes logs| LG

    PM -->|"daemonize / supervise"| WR
    CFG -->|"provides settings"| WR

    click CFG "https://github.com/nya-wsl/bili_rec_monitor/blob/main/config.example.yml"
    click WR "https://github.com/nya-wsl/bili_rec_monitor/blob/main/bili_rec_monitor.py"
    click ED "https://github.com/nya-wsl/bili_rec_monitor/blob/main/bili_rec_monitor.py"
    click NM "https://github.com/nya-wsl/bili_rec_monitor/blob/main/bili_rec_monitor.py"
    click CE "https://github.com/nya-wsl/bili_rec_monitor/blob/main/bili_rec_monitor.py"
    click FM "https://github.com/nya-wsl/bili_rec_monitor/blob/main/bili_rec_monitor.py"
    click PA "https://github.com/nya-wsl/bili_rec_monitor/blob/main/pcs_auth.py"
    click PC "https://github.com/nya-wsl/bili_rec_monitor/blob/main/pcs.py"
    click OC "https://github.com/nya-wsl/bili_rec_monitor/tree/main/openapi_client/"
    click REQ "https://github.com/nya-wsl/bili_rec_monitor/blob/main/requirements.txt"
    click PY "https://github.com/nya-wsl/bili_rec_monitor/blob/main/pyproject.toml"

    classDef external fill:#ADD8E6,stroke:#000
    classDef internal fill:#90EE90,stroke:#000
    classDef datastore fill:#FFD700,stroke:#000
    classDef config fill:#D3D3D3,stroke:#000
```
