Metadata-Version: 2.5
Name: statement-fetch
Version: 0.5.1
Summary: Zero-LLM async fetcher, structural validator, and pluggable sink runner for statements, invoices, and documents from authenticated web portals.
Project-URL: Homepage, https://github.com/benjamin-di/statement-fetch-oss
Project-URL: Repository, https://github.com/benjamin-di/statement-fetch-oss
Author: benjamin-di
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.11
Provides-Extra: playwright
Requires-Dist: playwright<2,>=1.52; extra == 'playwright'
Description-Content-Type: text/markdown

# statement-fetch

Zero-LLM async fetcher, structural validator, and pluggable sink runner for statements, invoices, and documents from authenticated web portals.

## What It's For

Any web portal behind human login/2FA that periodically issues PDFs, CSVs, JSONs, or ZIP archives can be automated as a small deterministic provider plugin — without ever handing credentials or live portal sessions to an LLM:

- **Banking, cards & brokerage:** monthly statements, transaction CSVs, trade confirmations, custody and tax reports.
- **Private funds & wealth portals:** quarterly partner reports, capital calls, distributions, K-1s, and audit letters.
- **Insurance & healthcare:** policy documents, claims summaries, explanation-of-benefits (EOBs), and annual tax certificates from insurer postboxes.
- **Billing, utilities & SaaS/cloud:** monthly VAT invoices and usage exports from telecoms, utilities, cloud providers, and SaaS billing portals.
- **Payroll, HR & pensions:** payslips, annual wage/tax statements, and retirement or pension summaries.
- **Government, tax & property:** tax assessment notices, social-security records, and mortgage/HOA/property-management statements.
- **Downstream pipelines (via `statement_fetch.sinks`):** route verified, SHA-256-hashed manifests directly into personal accounting ledgers, document management systems (e.g. Paperless-ngx), encrypted local folders, or private knowledge bases.

## Security Model ("Zero-LLM-in-Portal")

1. **Human sign-in:** A real Chromium browser opens the portal's login URL. The human completes authentication and 2FA; no credentials or LLM tokens touch the live session.
2. **Host confinement:** Top-level navigations, HTTP 3xx redirects (single/multi-hop and POST), `target="_blank"` popups, and in-page `fetch_bytes()` calls are confined to the provider's declared `allowed_hosts` via Chromium CDP `Fetch.enable` and context routing.
3. **Deterministic fetch & atomic staging:** After `is_logged_in()` succeeds, deterministic provider code downloads documents into a per-run `0o700` staging directory, validates every artifact using its declared validator (defaulting to non-empty; built-in validators cover PDF, CSV, JSON, and bounded ZIP), writes `0o600` artifacts with SHA-256 digests, and produces a portable `manifest.json`.
4. **Guaranteed logoff & pluggable sinks:** `logoff()` and browser teardown run in `finally:` even on error or Ctrl-C. Sinks (`statement_fetch.sinks`) verify every manifest entry and SHA-256 before delivering.

## Installation

Requires Python 3.11+:

```bash
pip install "statement-fetch[playwright]"
playwright install chromium
```

## Quickstart (Demo Portal, From a Clone)

Install Chromium into the project environment:

```bash
uv run --extra playwright playwright install chromium
```

In one terminal, start the bundled zero-dependency loopback demo portal:

```bash
uv run python -m statement_fetch.demo --port 8765
```

In another terminal, fetch statements and export them to a local directory with an immutable receipt manifest:

```bash
uv run --extra playwright statement-fetch fetch example-bank \
  --opt base_url=http://127.0.0.1:8765 \
  --upload --sink dir --sink-dir ./exported
```

Verify a staged or exported manifest offline:

```bash
uv run statement-fetch upload ./exported/manifest-*.json --local-only --json
```

## Plugin API (`API_VERSION = 1`)

Third-party providers and sinks import from `statement_fetch.api` (or top-level `statement_fetch`) and register via Python entry points:

```toml
[project.entry-points."statement_fetch.providers"]
my-portal = "my_pkg.provider:MyPortalProvider"

[project.entry-points."statement_fetch.sinks"]
my-sink = "my_pkg.sink:MySink"
```

- Reference provider: `src/statement_fetch/providers/example_bank.py`
- Reference sink: `src/statement_fetch/sinks/directory.py`
- Versioning: the entry-point group names (`statement_fetch.providers`, `statement_fetch.sinks`) define the `API_VERSION = 1` compatibility boundary.

## CLI Exit Codes & Manifest Schema

- **Exit codes:** `0` (`EXIT_OK`, ok/empty), `1` (`EXIT_ERROR`), `2` (`EXIT_USAGE`), `3` (`EXIT_LOGIN_TIMEOUT`), `4` (`EXIT_PARTIAL`), `130` (interrupted / Ctrl-C).
- **`manifest.json` (`schema_version = 1`):** `schema_version`, `api_version`, `tool_version`, `created_at`, `provider`, `period_start`, `period_end`, `artifacts[]` (`artifact_id`, `name`, `path` relative to manifest dir, `sha256`, `bytes`, `provider`, `kind`, `period`, `valid`, optional `currency`/`error`/`observed_start`/`observed_end`/`meta`), `skipped[]`, `errors[]`, `timings_s`.

## Development Checks

```bash
make check
```
