Metadata-Version: 2.4
Name: forktex-crawl
Version: 1.0.0
Summary: Harvest public APIs politely and prove what you harvested: robots.txt per RFC 9309, per-origin pacing, verified filters and a coverage report that names what it could not reach.
License-Expression: AGPL-3.0-or-later OR LicenseRef-ForkTex-Commercial
License-File: LICENSE
License-File: NOTICE
Keywords: crawl,harvest,robots.txt,politeness,api,async
Author: FORKTEX
Author-email: info@forktex.com
Requires-Python: >=3.14,<4.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Dist: forktex (>=1.2,<2)
Requires-Dist: httpx (>=0.28,<0.29)
Requires-Dist: pydantic (>=2.14,<3)
Project-URL: Bug Tracker, https://github.com/forktex/forktex-crawl/issues
Project-URL: Changelog, https://github.com/forktex/forktex-crawl/blob/master/CHANGELOG.md
Project-URL: Documentation, https://github.com/forktex/forktex-crawl/tree/master/docs
Project-URL: Homepage, https://forktex.com
Project-URL: Repository, https://github.com/forktex/forktex-crawl
Description-Content-Type: text/markdown

<p align="center">
  <img src="https://raw.githubusercontent.com/forktex/forktex-crawl/master/docs/assets/crawl-banner.png" alt="forktex-crawl" height="120">
</p>

<p align="center">
  <a href="https://pypi.org/project/forktex-crawl/"><img src="https://img.shields.io/pypi/v/forktex-crawl.svg" alt="PyPI"></a>
  <a href="https://pypi.org/project/forktex-crawl/"><img src="https://img.shields.io/pypi/pyversions/forktex-crawl.svg" alt="Python"></a>
  <a href="https://github.com/forktex/forktex-crawl/blob/master/LICENSE"><img src="https://img.shields.io/pypi/l/forktex-crawl.svg" alt="License"></a>
</p>

Harvest public APIs from Python, politely, and be able to prove what you
harvested. robots.txt is read per RFC 9309, each origin is paced at the rate you
declare, and the retries a server asks for are honoured. A harvest checks every
row against every filter its contract lists, splits a capped result set until
the server answers in full, and returns a coverage report that names whatever
it could not reach.

```bash
pip install forktex-crawl
```

```python
import asyncio

from forktex_crawl import HttpTransport, Politeness, PolitenessSpec, Request


async def main() -> None:
    async with HttpTransport() as http:
        response = await Politeness(agent="my-bot (+https://example.org/bot)").request(
            http, Request(url="https://example.com/"), PolitenessSpec(rate_per_second=1)
        )
    print(response.status, response.text()[:80])


asyncio.run(main())
```

## Requirements

| | |
|---|---|
| **Python** | 3.14 |
| **Nothing else** | No service, no database and no global state. Requests go out through httpx; a run is written to a directory you name, or to a sink of your own. |

## What you can build

| You want to | Read |
|---|---|
| Fetch pages from a site within its robots.txt and a rate it will tolerate | [Fetch a page politely](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md#fetch-a-page-politely) |
| Write down how a site's own API is called, as a reviewable file | [Write a contract](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md#write-a-contract) |
| Harvest a capped API completely, with every filter verified | [Harvest an API](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md#harvest-an-api-with-its-cap-defeated) |
| Know exactly what a run did not cover, and why | [Read the coverage report](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md#read-the-coverage-report) |
| Keep each run's records, raw responses and coverage on disk, and read them back | [Keep a run on disk](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md#keep-a-run-on-disk), [Read a run back](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md#read-a-run-back) |
| Ask an official API for known keys and learn which it lacks | [Ask an official API for known keys](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md#ask-an-official-api-for-known-keys) |
| Test your own harvest without a network and without waiting | [Test a harvest](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md#test-a-harvest-without-a-network-or-waiting) |

## How it works

| Concept | What it is |
|---|---|
| **Transport** | Somewhere a request goes and a response comes back: `HttpTransport`, or your own. Stateless, so it can be retried, paced and replayed. |
| **Politeness** | robots.txt, per-origin pacing, retries and redirect hops, applied around a transport. One object, naming who you are, shared by every task. |
| **RequestContract** | One endpoint: URL, the headers the remote requires, paging, each filter with where its effect must show in a row, how to partition by date, and how hard it may be pushed. |
| **Run** | What a harvest needs, as one value: transport, politeness, sink, run id and a page limit. |
| **Sink** | Where records go and exchanges are kept: `RunDirectory`, or your own. |
| **Coverage** | What a harvest covered and, by partition, what it did not: `truncated`, and `failed` with a `kind` saying why. |
| **CrawlError** | Every failure raised, with a `kind` — the same vocabulary as a recorded failure — and one `AppErrorCode` per kind. |

## Things to know

| | |
|---|---|
| **A filter is proved, not trusted** | A remote can ignore a filter it does not know and answer `200`. Every returned row is checked against every filter the contract lists; a contradiction, or a page on which no row shows the filtered field at all, raises `CrawlError` of kind `drift` and stops the run. Fixed fields in `body_template` are sent unchecked. |
| **Complete means complete** | A non-2xx answer, rows missing from where the contract says they are, fewer rows than the reported total, a page shorter than `page_size` with more after it, a key batch the remote did not wholly account for, a repeated page or the page limit is a recorded failure, so a failure never shows up as an empty, complete run. A run that asked for nothing — an empty or reversed date range — is empty and complete. A run whose manifest was never written — stopped by `drift`, or killed — has no `manifest()`. |
| **Your mistake is refused at once** | A URL that is not absolute `http`/`https`, a header HTTP cannot carry, a request with both a JSON body and a form, or an agent that cannot go on the wire is refused where it is written, never sent and reported as the remote's failure. |
| **robots.txt fails closed** | An unreachable robots.txt (`5xx`, no answer) disallows the origin until it is back, per RFC 9309; a `4xx` other than `429` allows everything. A harvest records it as a `robots` failure. |
| **Non-2xx is an answer** | `Politeness.request` returns a `404` as a `Response`; it raises only for robots, throttling, exhausted retries, a body it will not read, more than five redirects, a redirect into a private address or somewhere HTTP cannot go, a request HTTP cannot send, or a closed transport. |
| **POST is sent once** | Only idempotent methods are retried unless a source's `PolitenessSpec` sets `retry_non_idempotent`, and a POST is never replayed to another origin by a redirect. |
| **The remote's failure is not your caller's** | A failure caused by the remote carries `unavailable` or `failed`, never a code that blames whoever called your API. |
| **Files name their format** | Contracts, manifests and kept exchanges carry `"format": 1` and ignore fields they do not know; a later format, or a value outside one of their closed vocabularies, is refused rather than guessed at. |
| **No stealth** | No user-agent rotation, proxies or fingerprint patching. The agent you pass names you. |

## Documentation

| Page | For |
|---|---|
| [Guide](https://github.com/forktex/forktex-crawl/blob/master/docs/guide.md) | Runnable recipes for each way to use it |
| [Reference](https://github.com/forktex/forktex-crawl/blob/master/docs/reference.md) | Every public name, field, error and file format |
| [Operating](https://github.com/forktex/forktex-crawl/blob/master/docs/operating.md) | Robots, rates, redirects, limits, the storage layout, what to watch |

## Stability

Semantic versioning covers every name in `forktex_crawl.__all__`, the nine
error kinds and the `AppErrorCode` each maps to, and the persisted formats —
contracts, manifests and kept exchanges, at `"format": 1`. A new field in a
file is a minor change; a new value in one of its closed vocabularies
(`Method`, `AssertKind`, `paging`, `end_bound`, `Kind`) is a new format. Anything else —
module paths included — is internal. Changes are listed in the
[CHANGELOG](https://github.com/forktex/forktex-crawl/blob/master/CHANGELOG.md).

## Contributing

Working on `forktex-crawl` itself? Start with
[`docs/development.md`](https://github.com/forktex/forktex-crawl/blob/master/docs/development.md). Security issues go to the
address in [SECURITY.md](https://github.com/forktex/forktex-crawl/blob/master/SECURITY.md).

## Licence

AGPL-3.0-or-later, or a commercial licence from FORKTEX S.R.L. See
[LICENSE](https://github.com/forktex/forktex-crawl/blob/master/LICENSE) and [NOTICE](https://github.com/forktex/forktex-crawl/blob/master/NOTICE).

