# megadose/onionsearch architecture

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

- Interactive diagram: https://gitdiagram.com/megadose/onionsearch
- Repository: https://github.com/megadose/onionsearch

## Overview

##### Project type and purpose
- This is a Python-based CLI utility (“OnionSearch”) that crawls multiple “.onion” search engines over Tor, aggregates results and writes them to a CSV (or other) file.
- It is an open-source tool (packaged via setup.py / PyPI), not a web app or microservice.

##### File structure
- Top level:
- setup.py — installer and entry point definition
- LICENSE, .gitignore, README.md
- onionsearch/ (the Python package)
- Inside onionsearch/:
- __init__.py — package marker and possibly version
- core.py — the only module; contains CLI parsing, engine definitions, scraping logic, multiprocessing orchestration and output routines

##### Extract architecture hints from the README
- Uses argparse (CLI), runs multiple scrapers in parallel (multiprocessing), talks to Tor proxy by default at 127.0.0.1:9050.
- Supports a list of search-engine backends (ahmia, darksearchio, …), each one implemented as a function or class in core.py.
- Aggregates link fields and writes CSV (with custom fields, delimiter, progressive write).

##### Main components
a. CLI Layer
- Argument Parser (argparse in core.py)
- Validation of options (engines list, output path, limits, proxies, mp_units)
b. Orchestration Layer
- Multiprocessing Manager – spawns worker processes (pool of size mp_units)
- Task Queue – engine scrape requests with search term + per-engine limit
c. Scraper Engines
- Individual engine modules/functions (ahmia, darksearchio, etc.)
- Factory/Dispatcher that chooses which engine function to invoke
d. Network/Proxy Layer
- Tor Proxy (127.0.0.1:9050)
- HTTP client (e.g., requests with SOCKS5 support)
e. Output Layer
- Result Aggregator – collects tuples of (engine, name, url, domain, …)
- File Writer – writes to CSV (batch or continuous), format/filename generator
f. Utilities
- Sanitization / CSV field escaping
- Progress bar display (optional, per-process)

##### Relationships and data flows
- User invokes “onionsearch” CLI → Argument Parser
- Parser produces a job spec → Multiprocessing Manager
- Manager enqueues scraping tasks → Worker Processes
- Each worker:
- Picks one engine
- Calls the corresponding scraper function
- Sends HTTP requests via Tor Proxy
- Parses HTML results into structured records
- Returns record list to Manager
- Manager aggregates all results → passes to File Writer
- File Writer writes final CSV (or progressively writes as tasks complete)

##### Architectural patterns & design principles
- Separation of concerns: clean split between CLI, orchestration, network, engine logic, and I/O
- Concurrency via multiprocessing pool
- Plug-in / factory pattern for adding new engines
- Single-responsibility for each component (parsing, scraping, writing)

##### Technologies, frameworks, libraries
- Python 3.x
- argparse (CLI)
- multiprocessing (parallelism)
- requests or httpx + PySocks (network over Tor)
- csv module (output)
- setuptools / entry_points (CLI installation via setup.py)

## Diagram (Mermaid)

```mermaid
flowchart TD
    Entry_Point(["Entry Point & Installer (setup.py)"]):::entry
    Package_Marker(["Package Marker & Version (__init__.py)"]):::entry

    subgraph "OnionSearch Process"
        CLI_Parser(["CLI Parser\n(argparse)"]):::core
        MP_Manager(["Multiprocessing Manager\n& Task Queue"]):::core
        Worker(["Worker Process"]):::core
        Engine_Plugins(["Engine Plugins\n(ahmia, darksearchio, ...)"]):::engine
        Result_Agg(["Result Aggregator"]):::core
        File_Writer(["File Writer\n(CSV formatting/writes)"]):::core
        Utilities(["Utilities\n(CSV escaping, progress bar)"]):::util
    end

    subgraph "External Systems"
        Network_Proxy(["Tor Proxy\n127.0.0.1:9050"]):::external
    end

    Entry_Point -->|starts| CLI_Parser
    Package_Marker -->|provides version| CLI_Parser
    CLI_Parser -->|"job spec"| MP_Manager
    MP_Manager -->|"spawn tasks"| Worker
    Worker -->|"invoke plugin"| Engine_Plugins
    Engine_Plugins -->|"HTTP requests"| Network_Proxy
    Network_Proxy -->|"HTTP response"| Engine_Plugins
    Engine_Plugins -->|"records"| MP_Manager
    MP_Manager -->|"aggregate results"| Result_Agg
    Result_Agg -->|"write data"| File_Writer

    Utilities --> File_Writer
    Utilities --> Engine_Plugins

    click CLI_Parser "https://github.com/megadose/onionsearch/blob/master/onionsearch/core.py"
    click MP_Manager "https://github.com/megadose/onionsearch/blob/master/onionsearch/core.py"
    click Engine_Plugins "https://github.com/megadose/onionsearch/blob/master/onionsearch/core.py"
    click Network_Proxy "https://github.com/megadose/onionsearch/blob/master/onionsearch/core.py"
    click Result_Agg "https://github.com/megadose/onionsearch/blob/master/onionsearch/core.py"
    click File_Writer "https://github.com/megadose/onionsearch/blob/master/onionsearch/core.py"
    click Utilities "https://github.com/megadose/onionsearch/blob/master/onionsearch/core.py"
    click Entry_Point "https://github.com/megadose/onionsearch/blob/master/setup.py"
    click Package_Marker "https://github.com/megadose/onionsearch/blob/master/onionsearch/__init__.py"

    classDef core fill:#D0E8FF,stroke:#000,stroke-width:1px
    classDef engine fill:#DFFFD0,stroke:#000,stroke-width:1px
    classDef external fill:#FFE0B2,stroke:#000,stroke-width:1px
    classDef util fill:#EEEEEE,stroke:#000,stroke-width:1px
    classDef entry fill:#F3E5F5,stroke:#000,stroke-width:1px
```
