# shrimp2845-tw/nhdl architecture

> Architecture diagram of the GitHub repository shrimp2845-tw/nhdl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shrimp2845-tw/nhdl
- Repository: https://github.com/shrimp2845-tw/nhdl

## Overview

1. **What this repository is**
   - `nhdl` is a small, single-purpose Python package for fetching a nhentai gallery by **ID or URL**, downloading its pages, and assembling them into a **PDF**.
   - It is not a service or web app; the architecture is a **local, single-process download pipeline**.

2. **Public entrypoint**
   - The package exposes a simple top-level API: `from nhdl import download`.
   - That public function is the primary boundary for consumers and likely delegates into the main orchestration code in `nhdl/nh_dl.py`.

3. **Core orchestration layer**
   - `nhdl/nh_dl.py` is the central coordinator.
   - Its job is to turn one user input (`book` as URL or ID) into a complete end-to-end workflow: resolve the gallery, fetch page assets, manage retries/rest delays, and trigger PDF creation.

4. **Metadata / gallery discovery subsystem**
   - `nhdl/utils/get_info.py` appears to be responsible for extracting gallery information from nhentai.
   - This is the first external-network boundary: it likely normalizes the input and reads gallery metadata such as title, page list, and download targets.

5. **Image download subsystem**
   - `nhdl/utils/download.py` likely handles the actual HTTP downloads of page images.
   - This is the main I/O-heavy part of the system and probably includes the retry behavior described in the README.
   - The `rest` parameter in the public API suggests this module is rate-throttled between page fetches.

6. **Formatting / PDF generation subsystem**
   - `nhdl/utils/formatting.py` is the conversion layer that turns downloaded images into a PDF.
   - This is the final transformation step in the pipeline and forms the output boundary of the application.

7. **User feedback and progress**
   - The README mentions **tqdm** progress display, so the download pipeline likely reports progress while iterating through pages.
   - That makes the user-facing experience part of the orchestration layer rather than a separate subsystem.

8. **Data flow through the system**
   - Input: a gallery **ID or URL**.
   - Then: resolve gallery details from nhentai.
   - Then: iterate over page/image URLs, download each file with optional delay/retry.
   - Then: pass the collected images into the PDF formatter.
   - Output: a local **PDF file** on disk.

9. **External boundary**
   - The only major external dependency from an architecture perspective is the **nhentai website**, which supplies both metadata and page images.
   - All other behavior is local file and network I/O; there is no evidence of a database, queue, or background worker system.

10. **Package and runtime shape**
   - `pyproject.toml` indicates a modern Python package layout.
   - The README states **Python 3.8+**.
   - `nhdl/__init__.py` exists to present a clean package API rather than requiring callers to know internal module structure.

11. **Testing boundary**
   - `test/test.py` is the repository’s dedicated test area.
   - Given the project shape, tests are likely focused on the download/formatting pipeline or input handling rather than distributed integration components.

12. **How to draw the architecture**
   - Draw one client entrypoint feeding a single orchestration module.
   - From there, branch to three utility responsibilities: **gallery info retrieval**, **page downloading**, and **PDF formatting**.
   - Show nhentai as the remote external system on the far side of the metadata/download modules, and local filesystem/PDF output on the far side of the formatter.

Main entry points: [`nhdl/nh_dl.py`](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/nh_dl.py) (nh_dl), [`nhdl/utils/download.py`](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/utils/download.py) (Page download), [`nhdl/utils/formatting.py`](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/utils/formatting.py) (PDF format), [`nhdl/__init__.py`](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/__init__.py) (download)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_package["nhdl package"]
  node_api["download<br/>public API<br/>[__init__.py]"]
  node_orchestrator["nh_dl<br/>pipeline orchestrator<br/>[nh_dl.py]"]
end

subgraph group_utils["Utilities"]
  node_info["Gallery info<br/>metadata fetcher<br/>[get_info.py]"]
  node_downloader["Page download<br/>image fetcher<br/>[download.py]"]
  node_formatter["PDF format<br/>pdf builder<br/>[formatting.py]"]
end

subgraph group_external["External systems"]
  node_caller(("Caller<br/>consumer"))
  node_nhentai(("nhentai<br/>remote site"))
  node_progress(("tqdm<br/>progress UI"))
  node_input["Gallery ID/URL<br/>input"]
  node_fs[("Filesystem<br/>local storage")]
end

subgraph group_artifacts["Artifacts"]
  node_images["Images<br/>downloaded assets"]
  node_pdf["PDF file<br/>output artifact"]
end

subgraph group_tests["Tests"]
  node_tests["test.py<br/>test suite<br/>[test.py]"]
end

node_caller -->|"calls"| node_api
node_api -->|"delegates"| node_orchestrator
node_orchestrator -->|"accepts"| node_input
node_orchestrator -->|"resolves"| node_info
node_info -->|"queries"| node_nhentai
node_orchestrator -->|"downloads"| node_downloader
node_downloader -->|"fetches"| node_nhentai
node_downloader -->|"reports"| node_progress
node_downloader -->|"writes"| node_images
node_images -->|"stored in"| node_fs
node_orchestrator -->|"formats"| node_formatter
node_formatter -->|"creates"| node_pdf
node_formatter -->|"writes"| node_fs
node_tests -.->|"covers"| node_api
node_tests -.->|"covers"| node_orchestrator

click node_api "https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/__init__.py"
click node_orchestrator "https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/nh_dl.py"
click node_info "https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/utils/get_info.py"
click node_downloader "https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/utils/download.py"
click node_formatter "https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/utils/formatting.py"
click node_tests "https://github.com/shrimp2845-tw/nhdl/blob/main/test/test.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_orchestrator toneBlue
class node_info,node_downloader,node_formatter toneAmber
class node_caller,node_nhentai,node_progress,node_input,node_fs toneMint
class node_images,node_pdf toneRose
class node_tests toneIndigo
```

## Components

### nhdl package

Public API and orchestration

- [download](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/__init__.py): public API · Top-level package entrypoint · `nhdl/__init__.py`
- [nh_dl](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/nh_dl.py): pipeline orchestrator · Coordinates resolve, download, retry, and output steps · `nhdl/nh_dl.py`

### Utilities

Gallery lookup, download, and formatting helpers

- [Gallery info](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/utils/get_info.py): metadata fetcher · Resolves gallery details from ID or URL · `nhdl/utils/get_info.py`
- [Page download](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/utils/download.py): image fetcher · Downloads page images with retry and pacing · `nhdl/utils/download.py`
- [PDF format](https://github.com/shrimp2845-tw/nhdl/blob/main/nhdl/utils/formatting.py): pdf builder · Converts downloaded images into a PDF · `nhdl/utils/formatting.py`

### External systems

Remote site and local runtime boundaries

- **Caller**: consumer · User code or script invoking the package
- **nhentai**: remote site · External source for metadata and page images
- **tqdm**: progress UI · Progress feedback during iteration
- **Gallery ID/URL**: input · Gallery identifier accepted by the API
- **Filesystem**: local storage · Local read/write boundary for downloads and output

### Artifacts

Intermediate and final files

- **Images**: downloaded assets · Fetched page image files
- **PDF file**: output artifact · Final assembled local PDF

### Tests

Repository test boundary

- [test.py](https://github.com/shrimp2845-tw/nhdl/blob/main/test/test.py): test suite · Repository test coverage boundary · `test/test.py`

## Connections

- Caller → download: calls · Consumer imports and invokes the public entrypoint
- download → nh_dl: delegates · Public API hands control to the orchestration layer
- nh_dl → Gallery ID/URL: accepts · Orchestrator starts from a gallery ID or URL
- nh_dl → Gallery info: resolves · Fetches gallery metadata and page targets
- Gallery info → nhentai: queries · Reads gallery metadata from the remote site
- nh_dl → Page download: downloads · Coordinates page image retrieval
- Page download → nhentai: fetches · Requests page image assets from the remote site
- Page download → tqdm: reports · Shows download progress to the user
- Page download → Images: writes · Persists downloaded page images locally
- Images → Filesystem: stored in · Downloaded assets live on disk
- nh_dl → PDF format: formats · Sends collected images to the PDF builder
- PDF format → PDF file: creates · Produces the final PDF artifact
- PDF format → Filesystem: writes · Saves the assembled PDF locally
- test.py → download: covers · Tests likely exercise the public package boundary
- test.py → nh_dl: covers · Tests likely validate the pipeline coordinator
