# casualsnek/onthespot architecture

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

- Interactive diagram: https://gitdiagram.com/casualsnek/onthespot
- Repository: https://github.com/casualsnek/onthespot

## Overview

##### Project type and purpose
- Onthespot is a standalone, Qt-based desktop music downloader written in Python. There is no remote backend—everything runs on the user’s machine. It provides a GUI frontend, a set of background workers, and integrations with external services (Spotify API, FFmpeg).

##### File structure
- Top-level:
- build scripts and packaging (build_*.sh / .bat, PKGBUILD)
- pyproject.toml, requirements.txt, setup.cfg for build & packaging
- src/onthespot: core Python package
- Inside src/onthespot:
- gui/ (GUI layer: main windows, dialogs, Qt .ui files)
- worker/ (background tasks: downloader, media conversion, session management)
- utils/ (Spotify API wrappers, generic utilities)
- otsconfig.py and runtimedata.py (configuration & runtime state)
- exceptions.py (error definitions)
- __main__.py (entrypoint)
- portable.py provides a uniform entrypoint for portable builds.

##### Key insights from README
- The app requires Python3, Qt, FFmpeg, and Spotify credentials.
- It spawns multiple download “workers” (threads) to fetch and convert media.
- Builds can be packaged into platform-specific binaries or wheels.

##### Main components
a. Frontend (GUI Layer)
- MainUI, MiniDialog, DL_ProgressBtn, ThumbListItem
- Qt UI definitions (.ui files)
b. Application Core
- Controller logic in __main__.py and mainui.py
- Configuration manager (otsconfig.py)
- Runtime state (runtimedata.py)
c. Background Workers
- Session manager (worker/session.py) – handles Spotify OAuth & requests
- Downloader (worker/downloader.py) – fetches raw audio chunks
- Media processor (worker/media.py) – uses FFmpeg for conversion & metadata
- Utility worker functions (worker/utility.py)
d. Utilities
- Spotify API wrapper (utils/spotify.py)
- File & path utilities (utils/utils.py)
e. External Services
- Spotify Web API (network calls)
- FFmpeg CLI (conversion subprocess)
f. File System
- Download directory, chunk storage, metadata & thumbnails
g. Packaging / Build System (optional)
- build scripts, pyproject.toml, setup.cfg

##### Relationships & data flows
- User clicks “Search” / “Download” in GUI → Controller reads config, pushes tasks into Worker Pool
- Session Manager authenticates & queries Spotify → returns track/album metadata
- Downloader fetches audio data in chunks → posts progress updates back to GUI
- Media Processor invokes FFmpeg subprocess → converts, tags, embeds thumbnail
- Final file is written to disk → GUI Progress Tab is updated
- All errors flow through exceptions.py and are surfaced in dialogs

##### Architectural patterns & design principles
- Layered architecture: GUI → Controller → Background Workers → External Services → File System
- Separation of concerns:
- gui/ holds only presentation logic
- worker/ handles heavy I/O & CPU work off the main thread
- utils/ isolates third-party API calls
- Thread-safe communication: use Qt’s signal/slot or thread-safe queues for progress updates
- Configuration & runtime state centralized in otsconfig.py and runtimedata.py

- Component names (e.g., MainUI, SessionManager, Downloader)
- Module paths (e.g., src/onthespot/worker/downloader.py)
- Data flows with labels (“search request,” “audio chunk,” “conversion command”)
- Threading boundary between GUI thread and Worker threads
- Configuration load → global settings
- Packaging boundary, if relevant (pyproject.toml → wheel/binary)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        MainUI["MainUI"]:::internal
        MiniDialog["MiniDialog"]:::internal
        DL_ProgressBtn["DL_ProgressBtn"]:::internal
        ThumbListItem["ThumbListItem"]:::internal
        MainUI -->|uses UI file| MainUI_UI["main.ui"]:::internal
        MiniDialog -->|uses UI file| Notice_UI["notice.ui"]:::internal
    end

    %% Core Application
    subgraph "Application Core"
        direction TB
        Entrypoint["__main__.py"]:::internal
        Config["otsconfig.py"]:::internal
        Runtime["runtimedata.py"]:::internal
        Exceptions["exceptions.py"]:::internal
    end

    %% Background Workers
    subgraph "Background Workers"
        direction TB
        SessionManager["SessionManager"]:::internal
        Downloader["Downloader"]:::internal
        MediaProcessor["MediaProcessor"]:::internal
        UtilityWorker["UtilityWorker"]:::internal
    end

    %% Utilities
    subgraph "Utilities"
        direction TB
        SpotifyWrapper["SpotifyWrapper"]:::internal
        GenericUtils["GenericUtils"]:::internal
    end

    %% External Services
    subgraph "External Services"
        direction TB
        SpotifyAPI["Spotify Web API"]:::external
        FFmpeg["FFmpeg CLI"]:::external
    end

    %% File System Storage
    FileSystem[("File System")]:::storage

    %% Packaging / Build System
    subgraph "Packaging / Build System" style dashed
        direction TB
        BuildScripts["build_*.sh / .bat"]:::packaging
        PyProject["pyproject.toml"]:::packaging
        Requirements["requirements.txt"]:::packaging
        SetupCfg["setup.cfg"]:::packaging
        PKGBUILD["PKGBUILD"]:::packaging
    end

    %% Interconnections
    MainUI -->|triggers| Entrypoint
    Entrypoint -->|loads config| Config
    Entrypoint -->|initializes state| Runtime
    Entrypoint -->|spawns tasks| SessionManager
    Entrypoint -->|spawns tasks| Downloader
    Entrypoint -->|spawns tasks| MediaProcessor
    Entrypoint -->|spawns tasks| UtilityWorker

    SessionManager -->|uses wrapper| SpotifyWrapper
    SessionManager -->|auth & metadata| SpotifyAPI
    Downloader -->|fetches audio chunks| FileSystem
    Downloader -->|progress updates| DL_ProgressBtn
    MediaProcessor -->|invokes| FFmpeg
    MediaProcessor -->|writes final file| FileSystem
    UtilityWorker -->|uses utils| GenericUtils

    FileSystem -->|metadata & thumbnails| ThumbListItem
    Exceptions -->|error handling| MiniDialog

    %% Click Events
    click MainUI "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/gui/mainui.py"
    click MiniDialog "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/gui/minidialog.py"
    click DL_ProgressBtn "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/gui/dl_progressbtn.py"
    click ThumbListItem "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/gui/thumb_listitem.py"
    click MainUI_UI "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/gui/qtui/main.ui"
    click Notice_UI "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/gui/qtui/notice.ui"
    click Entrypoint "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/__main__.py"
    click Config "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/otsconfig.py"
    click Runtime "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/runtimedata.py"
    click Exceptions "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/exceptions.py"
    click SessionManager "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/worker/session.py"
    click Downloader "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/worker/downloader.py"
    click MediaProcessor "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/worker/media.py"
    click UtilityWorker "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/worker/utility.py"
    click SpotifyWrapper "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/utils/spotify.py"
    click GenericUtils "https://github.com/casualsnek/onthespot/blob/main/src/onthespot/utils/utils.py"
    click BuildScripts "https://github.com/casualsnek/onthespot/blob/main/build_linux.sh"
    click PyProject "https://github.com/casualsnek/onthespot/blob/main/pyproject.toml"
    click Requirements "https://github.com/casualsnek/onthespot/blob/main/requirements.txt"
    click SetupCfg "https://github.com/casualsnek/onthespot/blob/main/setup.cfg"
    click PKGBUILD "https://github.com/casualsnek/onthespot/tree/main/PKGBUILD"

    %% Styles
    classDef internal fill:#cfe2f3,stroke:#2f589e,stroke-width:1px
    classDef external fill:#d9ead3,stroke:#38761d,stroke-width:1px
    classDef storage fill:#fff2cc,stroke:#b45f06,stroke-width:1px
    classDef packaging fill:#f4cccc,stroke:#a61c00,stroke-width:1px,dasharray: 5 5
```
