# lvzhenbo/screc architecture

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

- Interactive diagram: https://gitdiagram.com/lvzhenbo/screc
- Repository: https://github.com/lvzhenbo/screc

## Overview

##### Project Type and Purpose
- This is an open-source, AI-driven, Rust-based CLI tool (“screc”) for monitoring and recording live streams from StripChat.
- It provides automated state polling, HLS segment downloading, decryption, retry logic, FFmpeg conversion, multi-user concurrency, proxy support, and a robust logging system.

##### File Structure Analysis
- Top level:
- Cargo.toml / src/ : Rust project
- .github/workflows : CI and release pipelines
- src/ modules:
- main.rs       — entry point, CLI parsing (clap)
- config.rs     — loads merges CLI args + JSON config (serde)
- stripchat.rs  — StripChat API client, state machine, recording logic
- downloader.rs — HLS segment fetcher, retry, decryption
- utils.rs      — shared helpers (URL parsing, time, logging setup)
- External integrations: StripChat REST API, HLS/CDN, FFmpeg CLI, filesystem, proxies, environment variables.

##### Key Components
A. CLI Layer
- User invokes `screc` with flags or `--generate-config`
- clap parser → dispatch to config loader or generator.
B. Configuration Manager
- Loads JSON file + merges CLI, env vars (proxy settings, cookies, resolution, intervals).
C. Scheduler / State Monitor
- Tokio runtime spawns concurrent tasks per username
- Polls StripChat API (reqwest) periodically
- State machine (Public, Private, Offline, Error, etc.)
D. Recording Pipeline
1. HLS Playlist Fetch
- downloader.fetch_playlist(url)
2. Segment Download & Decrypt
- downloader.download_segment() + MOUFLON decrypt
3. Retry Logic & Backoff
4. FFmpeg Merge & Conversion
- Shell out to ffmpeg for final MP4
E. I/O and Persistence
- File system: output_dir/<username>/<username>-<timestamp>.mp4
F. External Services
- StripChat API (HTTP + cookies)
- CDN / HLS servers
- FFmpeg binary
- Proxy endpoints (HTTP/HTTPS/SOCKS5)

##### Relationships and Flow
- The CLI triggers the Configuration Manager → yields a Config object.
- The Scheduler spawns one Worker Task per username.
- Each Worker loops: call StripChat API → if live, initiate Recording Pipeline → download segments → decrypt → pass to FFmpeg.
- Errors bubble up to retry logic; SIGINT triggers graceful shutdown broadcast.

##### Architectural Patterns and Principles
- Modular design: each Rust module has a single responsibility (SRP).
- Asynchronous concurrency: Tokio + futures for multi-stream recording.
- State machine for stream status transitions.
- Dependency inversion: HTTP clients (reqwest) and file I/O behind traits in utils.
- Resilience: retry/backoff strategy, skip logic, graceful shutdown.

##### Technologies & Libraries
- Language / Runtime: Rust 2024 + Tokio async
- HTTP & Proxies: reqwest
- JSON: serde / serde_json
- CLI: clap
- HLS parsing: m3u8-rs
- Error handling: anyhow
- Crypto: sha2 / base64 (MOUFLON decrypt)
- Time: chrono
- URL handling: url
- Build / CI: Cargo, GitHub Actions

##### Essential Elements to Capture
- CLI entry point and flag handling
- Config load + merging order (file → env → CLI)
- Concurrency model (one task per user)
- State machine driving recording logic
- Downloader + decryptor + FFmpeg chain
- Logging and graceful shutdown mechanism
- External dependencies and network flows

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Rust Modules
    subgraph "Rust Modules"
        CLI["CLI Layer"]:::internal
        Config["Configuration Manager"]:::internal
        Scheduler["Scheduler / State Monitor"]:::internal
        Downloader["Recording Pipeline"]:::internal
        Utils["Shared Utilities"]:::internal
    end

    %% External Services
    subgraph "External Services"
        API["StripChat API"]:::external
        CDN["HLS CDN"]:::external
        FFmpegBin["FFmpeg"]:::external
        Proxies["Proxy Endpoints"]:::external
    end

    %% I/O Systems
    subgraph "I/O Systems"
        FS["File System"]:::io
        Logs["Logging System"]:::io
        subgraph "CI/CD Pipelines"
            CI["CI Pipeline"]:::io
            RelPipeline["Release Pipeline"]:::io
        end
        Manifest["Cargo.toml"]:::io
        LICENSE["LICENSE"]:::io
        README["README.md"]:::io
    end

    %% Application Flow
    CLI -->|"parse flags / commands"| Config
    Config -->|"provides Config object"| Scheduler
    Scheduler -->|"spawn worker tasks"| Downloader
    Scheduler -->|"HTTP GET /status"| API
    API -->|"JSON response"| Scheduler
    Downloader -->|"fetch playlist M3U8"| CDN
    CDN -->|"segments"| Downloader
    Downloader -->|"decrypt & retry"| Downloader
    Downloader -->|"segments for conversion"| Utils
    Utils -->|"shell out MP4"| FFmpegBin
    Downloader -->|"write .ts/.key"| FS
    Utils -->|"log events"| Logs
    Scheduler -->|"log state changes"| Logs
    CLI -->|"log startup"| Logs

    %% Optional / Retry flows
    Downloader -.->|"retry/backoff"| Downloader

    %% Documentation & Manifest
    CLI --> Manifest
    CLI --> LICENSE
    CLI --> README

    %% Pipelines to Logs
    CI --> Logs
    RelPipeline --> Logs

    %% Click Events
    click CLI "https://github.com/lvzhenbo/screc/blob/master/src/main.rs"
    click Config "https://github.com/lvzhenbo/screc/blob/master/src/config.rs"
    click Scheduler "https://github.com/lvzhenbo/screc/blob/master/src/stripchat.rs"
    click Downloader "https://github.com/lvzhenbo/screc/blob/master/src/downloader.rs"
    click Utils "https://github.com/lvzhenbo/screc/blob/master/src/utils.rs"
    click CI "https://github.com/lvzhenbo/screc/blob/master/.github/workflows/ci.yml"
    click RelPipeline "https://github.com/lvzhenbo/screc/blob/master/.github/workflows/release.yml"
    click Manifest "https://github.com/lvzhenbo/screc/blob/master/Cargo.toml"
    click LICENSE "https://github.com/lvzhenbo/screc/tree/master/LICENSE"
    click README "https://github.com/lvzhenbo/screc/blob/master/README.md"

    %% Styles
    classDef internal fill:#D6EAF8,stroke:#2980B9,color:#1B4F72;
    classDef external fill:#D5F5E3,stroke:#27AE60,color:#196F3D;
    classDef io fill:#FCF3CF,stroke:#F1C40F,color:#7D6608;
```
