# Media Automator

Automates the pipeline:

```
qBittorrent (Mint VM)
    ↓  [completed torrent detected]
Category: 2 - copying  →  files move to transient dir
    ↓  [copy video files, flattened]
namer watch/
    ├── renamed/  →  Whisparr import dir  →  Whisparr manual import API
    └── failed/   →  Stash library dir   →  Stash scan + identify (StashDB)
                                                 ↓  [rename plugin fires]
                                         stash-temp/  →  Whisparr import dir  →  import
Category: 3 - seeding  →  files move to spinning array
```

---

## Setup

### 1. Prerequisites

- Docker + Docker Compose on your LXC
- The Mint VM's NFS shares mounted on the LXC (see below)
- qBittorrent Web UI enabled on the Mint VM
- Whisparr and Stash running and accessible from the LXC

### 2. NFS Mounts (LXC side)

The automator needs to see the Mint VM's SSD paths. On your LXC, add to `/etc/fstab`:

```
# Mint VM transient dir
<MINT_VM_IP>:/transfer    /mnt/ssd1/transfer    nfs    defaults,_netdev    0 0
```

Then: `mount -a`

Verify the path `/mnt/ssd1/transfer/2 - ready for copy/` is accessible from the LXC.

### 3. qBittorrent Categories

In qBittorrent on the Mint VM, configure these categories with their save paths:

| Category         | Save Path                              |
|------------------|----------------------------------------|
| `1 - downloading`| `/mnt/nvme/home/jkilloran/torrenting/downloads/` |
| `2 - copying`    | `/transfer/2 - ready for copy/`        |
| `3 - seeding`    | `/qbt/taz-transfer/seeding/`           |

Make sure "Automatically manage torrent location" is enabled per category.

### 4. Configure

Copy `config.yml` to your LXC and fill in all `< >` placeholders:

```bash
# On the LXC, in your docker-compose directory:
nano config.yml
```

Key values to fill in:
- `qbittorrent.host` — Mint VM IP address
- `qbittorrent.username` / `password` — qBit Web UI credentials
- `whisparr.host` / `api_key`
- `stash.host` / `api_key`
- `discord.webhook_url` — from Server Settings → Integrations → Webhooks

### 5. Build and Run

```bash
docker compose up -d --build
docker compose logs -f    # watch the logs
```

Web UI is at: `http://<LXC_IP>:8888`

---

## Flow Details

### Space Management

Before processing each torrent the automator checks:
- Free space on the namer SSD ≥ `namer_ssd_min_free_gb` + torrent size
- If not enough space: sends a Discord alert, pauses, and retries every `space_check_interval` seconds
- Resumes automatically once space is available (no intervention needed)

### Disk Space Alerts

You'll get Discord alerts for:
- Pipeline paused due to low space (and when it resumes)
- Each torrent starting to process
- Successful Whisparr import
- Files sent to Stash + identify result
- Any errors

### namer failed files

When namer can't identify a file it goes to `stash/`. The automator:
1. Moves the file to your Stash library path
2. Triggers `metadataScan` → waits for completion
3. Triggers `metadataIdentify` using StashDB → waits for completion
4. The `rename-file-on-update` plugin fires automatically → file appears in `stash-temp/`
5. Automator detects the file in `stash-temp/`, waits 30s for it to stabilise, moves to Whisparr import dir
6. Triggers Whisparr manual import

---

## Troubleshooting

### qBit category change isn't moving files
- Verify "Automatic torrent management" is enabled for each category in qBit settings
- Check the save paths match exactly what's in the qBit category config

### Whisparr import returns 0 matches
- The file naming matters — namer should produce `Studio.YYYY-MM-DD.Title.ext` format
- Check Whisparr's manual import UI for the file to see why it's not matching

### Stash identify never fires the rename plugin
- Confirm `rename-file-on-update` is enabled in Stash Settings → Plugins
- The plugin fires on scene update, not scan — identify must successfully update the scene

### Permission errors
- Uncomment `user: "1000:1000"` in `docker-compose.yml` and set to your LXC user's UID:GID
- Check NFS mount options on the LXC

---

## Adding the Docker qBittorrent Instance Later

When you're ready to add the LXC's qBit container to the same pipeline, the config
will gain a `qbittorrent_docker:` section with its own host/port/credentials, and the
automator will poll both instances in the same main loop. Paths will be LXC-native
(no NFS needed for that instance).
