# Choosing an interface

All of them use the same engine, the same defaults and the same files on disk. You can start with one and switch later without re-downloading anything.

| You are writing… | Use | Returns |
| --- | --- | --- |
| A script or a one-off job | [`machyon.download()`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.download> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-function\"></code>            <span class=\"doc doc-object-name doc-function-name\">download</span>") | [`Report`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Report> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-class\"></code>            <span class=\"doc doc-object-name doc-class-name\">Report</span>     <span class=\"doc doc-labels\">       <small class=\"doc doc-label doc-label-dataclass\"><code>dataclass</code></small>   </span>") |
| Code that already runs an event loop (Jupyter, FastAPI, an async worker) | [`await machyon.adownload()`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.adownload> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-function\"></code>            <span class=\"doc doc-object-name doc-function-name\">adownload</span>     <span class=\"doc doc-labels\">       <small class=\"doc doc-label doc-label-async\"><code>async</code></small>   </span>") | [`Report`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Report> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-class\"></code>            <span class=\"doc doc-object-name doc-class-name\">Report</span>     <span class=\"doc doc-labels\">       <small class=\"doc doc-label doc-label-dataclass\"><code>dataclass</code></small>   </span>") |
| An app that downloads more than once, or needs retries and failures as data | [`machyon.Downloader`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Downloader> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-class\"></code>            <span class=\"doc doc-object-name doc-class-name\">Downloader</span>") | [`Report`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Report> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-class\"></code>            <span class=\"doc doc-object-name doc-class-name\">Report</span>     <span class=\"doc doc-labels\">       <small class=\"doc doc-label doc-label-dataclass\"><code>dataclass</code></small>   </span>") per call |
| Nothing: you're in a terminal | [`machyon`](<https://divyanshusingh96.github.io/machyon/reference/cli/index.md>) | exit status 0, or 1 with `--strict` when anything failed |
| Nothing at all: you'd rather click | [`machyon ui`](<https://divyanshusingh96.github.io/machyon/getting-started/machyon-ui/index.md>), a local app in your browser | a run folder with the images, `report.json` and a gallery |
| Nothing, and nothing installed | The [Colab notebook](<https://colab.research.google.com/github/DivyanshuSingh96/machyon/blob/main/notebooks/machyon-colab.ipynb>): a form in Google Colab | the images as a ZIP or in your Google Drive, with the failures and their reasons |

## `download()`: one call

```python
report = machyon.download("urls.csv", "images/", names="{index}{ext}", max_retries=8)
```

Every [configuration option](<https://divyanshusingh96.github.io/machyon/reference/configuration/index.md>) is a keyword argument. The call blocks, and it runs on the fastest event loop installed. It raises `RuntimeError` if an event loop is already running in the thread.

## `adownload()`: one call, awaitable

```python
report = await machyon.adownload("urls.csv", "images/", names="{index}{ext}")
```

The same arguments as `download()`. It opens a `Downloader`, runs one download and closes it.

## `Downloader`: a long-lived object

async\_app.py

```python
import machyon


async def main() -> None:
    async with machyon.Downloader("images/", max_retries=8) as dl:
        report = await dl.download("urls.csv", names="{host}/{hash}{ext}")
        print(report)
        for failure in dl.failures:        # what is still missing, and why
            print(failure["reason"], failure["url"], "->", failure["name"])


machyon.run(main())   # asyncio.run(), on uvloop/winloop when installed
```

Use it when you want to:

- download several sources through one connection pool and one adaptive limiter;
- retry failures on your own schedule with [`retry_failed()`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Downloader.retry_failed> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-method\"></code>            <span class=\"doc doc-object-name doc-function-name\">retry_failed</span>     <span class=\"doc doc-labels\">       <small class=\"doc doc-label doc-label-async\"><code>async</code></small>   </span>");
- read [`failures`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Downloader.failures> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-attribute\"></code>            <span class=\"doc doc-object-name doc-attribute-name\">failures</span>     <span class=\"doc doc-labels\">       <small class=\"doc doc-label doc-label-property\"><code>property</code></small>   </span>") and [`failure_reasons()`](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#machyon.Downloader.failure_reasons> "<code class=\"doc-symbol doc-symbol-heading doc-symbol-method\"></code>            <span class=\"doc doc-object-name doc-function-name\">failure_reasons</span>") as data instead of parsing a file.

Always close it, ideally with `async with`, so the HTTP session and file-writer threads shut down cleanly.

## The CLI

```bash
machyon urls.csv -o images/
machyon retry -o images/
machyon names urls.csv --names "{label}/{hash}{ext}"
machyon doctor "https://example.com/img.jpg"
```

`download` is the default command, so `machyon urls.csv` and `machyon download urls.csv` are the same. [CLI reference](<https://divyanshusingh96.github.io/machyon/reference/cli/index.md>)

## The class-based API

`ImageDownloader` and `ImageRenamer` take explicit lists of links and names and give you step-by-step control. They run on the same engine. For new code the three interfaces above are simpler; the [Python API reference](<https://divyanshusingh96.github.io/machyon/reference/python-api/index.md#class-based-api>) documents both.
