muckrake 0.2.1__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. muckrake-0.3.1/PKG-INFO +288 -0
  2. muckrake-0.3.1/README.md +262 -0
  3. muckrake-0.3.1/pyproject.toml +83 -0
  4. muckrake-0.3.1/pyproject.toml.orig +77 -0
  5. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/artifacts.py +13 -14
  6. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/cli.py +77 -22
  7. muckrake-0.3.1/src/muckrake/crawl.py +311 -0
  8. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/dataset.py +79 -37
  9. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/db.py +60 -55
  10. muckrake-0.3.1/src/muckrake/dedupe/README.md +60 -0
  11. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/dedupe/cluster.py +45 -50
  12. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/dedupe/dedupe.py +7 -10
  13. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/dedupe/dedupe_edges.py +10 -15
  14. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/dedupe/review.py +32 -28
  15. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/entity_query.py +5 -4
  16. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/entity_write.py +8 -7
  17. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/export.py +8 -8
  18. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/fetch.py +99 -33
  19. muckrake-0.3.1/src/muckrake/extract/ner/README.md +242 -0
  20. muckrake-0.3.1/src/muckrake/extract/ner/__init__.py +4 -0
  21. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/ner/engines/base.py +4 -0
  22. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/ner/engines/llm.py +18 -9
  23. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/ner/materialize.py +4 -7
  24. muckrake-0.3.1/src/muckrake/extract/ner/pipeline.py +172 -0
  25. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/ner/review.py +19 -21
  26. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/ner/storage.py +132 -24
  27. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/load.py +9 -16
  28. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/logging.py +7 -8
  29. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/release.py +135 -31
  30. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/runs.py +58 -20
  31. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/search.py +9 -5
  32. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/serialize.py +7 -15
  33. muckrake-0.3.1/src/muckrake/settings.py +154 -0
  34. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/store.py +50 -11
  35. muckrake-0.3.1/src/muckrake/units.py +124 -0
  36. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/util.py +5 -4
  37. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/utils/__init__.py +8 -1
  38. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/utils/dates.py +66 -41
  39. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/utils/gb_coh.py +2 -3
  40. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/view.py +5 -5
  41. muckrake-0.2.1/PKG-INFO +0 -136
  42. muckrake-0.2.1/README.md +0 -110
  43. muckrake-0.2.1/pyproject.toml +0 -40
  44. muckrake-0.2.1/src/muckrake/crawl.py +0 -223
  45. muckrake-0.2.1/src/muckrake/dedupe/README.md +0 -52
  46. muckrake-0.2.1/src/muckrake/extract/ner/README.md +0 -210
  47. muckrake-0.2.1/src/muckrake/extract/ner/__init__.py +0 -4
  48. muckrake-0.2.1/src/muckrake/extract/ner/pipeline.py +0 -151
  49. muckrake-0.2.1/src/muckrake/settings.py +0 -41
  50. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/__init__.py +0 -0
  51. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/dedupe/__init__.py +5 -5
  52. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/env.py +0 -0
  53. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/__init__.py +0 -0
  54. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/ner/engines/__init__.py +0 -0
  55. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/ner/engines/delimited.py +0 -0
  56. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/extract/ner/engines/llm_prompt.py +0 -0
  57. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/id.py +0 -0
  58. {muckrake-0.2.1 → muckrake-0.3.1}/src/muckrake/py.typed +0 -0
@@ -0,0 +1,288 @@
1
+ Metadata-Version: 2.3
2
+ Name: muckrake
3
+ Version: 0.3.1
4
+ Summary: A data-pipeline framework for crawling, deduplicating and publishing FollowTheMoney entities.
5
+ Author: Nicu Calcea
6
+ Author-email: Nicu Calcea <mail@nicu.md>
7
+ Requires-Dist: beautifulsoup4>=4.14.3
8
+ Requires-Dist: click>=8.3.1
9
+ Requires-Dist: datapatch>=1.2.4
10
+ Requires-Dist: followthemoney>=4.4.0
11
+ Requires-Dist: org-id>=0.1.1
12
+ Requires-Dist: lxml>=6.0.2
13
+ Requires-Dist: nomenklatura>=4.4.1
14
+ Requires-Dist: openpyxl>=3.1.5
15
+ Requires-Dist: pandas>=2.3.3
16
+ Requires-Dist: pydantic-ai-slim[anthropic,google,openrouter]>=1.0.0,<2
17
+ Requires-Dist: psycopg[binary]>=3.2.12
18
+ Requires-Dist: plyvel>=1.5.1
19
+ Requires-Dist: pyyaml>=6.0.3
20
+ Requires-Dist: requests>=2.32.5
21
+ Requires-Dist: xlrd>=2.0.2
22
+ Requires-Dist: logfire[httpx,sqlalchemy]>=4.24.0
23
+ Requires-Dist: odfpy>=1.4.1
24
+ Requires-Python: >=3.13
25
+ Description-Content-Type: text/markdown
26
+
27
+ # muckrake
28
+
29
+ A reusable pipeline for crawling public data sources into
30
+ [FollowTheMoney](https://followthemoney.tech/) (FtM) entities, resolving duplicates
31
+ across sources, and publishing immutable releases to a read-only database.
32
+ It is partly inspired by [`zavod`](https://zavod.opensanctions.org/) and
33
+ [other tools in the FollowTheMoney ecosystem](https://followthemoney.tech/community/stack/).
34
+
35
+ > [!WARNING]
36
+ > This is a work in progress. Expect breaking changes and incomplete features.
37
+
38
+ muckrake ships no datasets of its own: a project keeps its crawlers in its own
39
+ repository and runs muckrake from there. [OpenLobbying](https://github.com/openlobbying/openlobbying)
40
+ (UK lobbying and political finance) is the main project built on it.
41
+
42
+ - [Quickstart](#quickstart)
43
+ - [Installation](#installation)
44
+ - [How it works](#how-it-works)
45
+ - [Guides](#guides): [writing a crawler](#writing-a-crawler), [entity extraction](#entity-extraction-ner), [deduplication](#deduplication), [releases](#releases-and-publishing), [Postgres](#using-postgres)
46
+ - Reference: [CLI](docs/cli.md), [environment variables](docs/env.md), [NER](src/muckrake/extract/ner/README.md), [dedupe review](src/muckrake/dedupe/README.md), [changelog](CHANGELOG.md), [contributing](CONTRIBUTING.md)
47
+
48
+ ## Quickstart
49
+
50
+ Crawl the bundled example dataset, publish it, and search it, using SQLite only
51
+ (no Docker, no network access after installation). You need
52
+ [uv](https://docs.astral.sh/uv/) and the ICU and LevelDB libraries (see
53
+ [Installation](#installation); on macOS, `brew install icu4c pkg-config leveldb`).
54
+
55
+ ```bash
56
+ git clone https://github.com/openlobbying/muckrake.git
57
+ cd muckrake
58
+
59
+ # macOS only: let the PyICU build find Homebrew's ICU
60
+ export PKG_CONFIG_PATH="$(brew --prefix icu4c)/lib/pkgconfig"
61
+ uv sync
62
+
63
+ # Discover the example dataset in examples/hello_dataset/
64
+ export MUCKRAKE_DATASET_PATHS=examples
65
+ # release-publish writes to a separate published database, and refuses to run without one
66
+ export MUCKRAKE_PUBLISHED_DATABASE_URL=sqlite:///data/published.db
67
+
68
+ uv run muckrake list
69
+ uv run muckrake -v crawl hello # writes data/datasets/hello/statements.pack.csv
70
+ uv run muckrake -v load hello # loads it into the working DB, data/muckrake.db
71
+ uv run muckrake release-build hello # prints "Built release 1"
72
+ uv run muckrake -v release-publish 1 # copies release 1 into data/published.db
73
+ uv run muckrake search acme --pretty # finds the two example companies
74
+ ```
75
+
76
+ `-v` turns on progress logging; without it, `crawl`, `load` and `release-publish`
77
+ print nothing on success. The example crawler is
78
+ [`examples/hello_dataset/crawler.py`](examples/hello_dataset/crawler.py): copy it
79
+ as the starting point for your own.
80
+
81
+ ## Installation
82
+
83
+ muckrake needs Python 3.13 (uv installs it) and two system libraries that some
84
+ dependencies compile against:
85
+
86
+ - **ICU** and **pkg-config**, for PyICU (used for name normalisation). Required.
87
+ - macOS: `brew install icu4c pkg-config`. Homebrew doesn't link `icu4c` into
88
+ the default search path, so export
89
+ `PKG_CONFIG_PATH="$(brew --prefix icu4c)/lib/pkgconfig"` before `uv sync`.
90
+ - Debian/Ubuntu: `sudo apt-get install libicu-dev pkg-config`.
91
+ - **LevelDB**, for plyvel. Required to install: plyvel is a dependency with no
92
+ binary wheels, so `uv sync` compiles it against the LevelDB headers.
93
+ - macOS: `brew install leveldb`.
94
+ - Debian/Ubuntu: `sudo apt-get install libleveldb-dev`.
95
+
96
+ At runtime only the `xref`, `dedupe`, `dedupe-edges` and `export` commands load
97
+ plyvel. On macOS, plyvel builds against Homebrew's LevelDB but fails on import
98
+ (`symbol not found ... leveldb10ComparatorE`), so run those four commands in the
99
+ container (see [Using Postgres](#using-postgres)); every other command runs
100
+ natively. Making plyvel an optional dependency is tracked in
101
+ [Make plyvel (LevelDB) an optional extra](https://github.com/openlobbying/muckrake/issues/54).
102
+
103
+ Then, in a clone of this repo, `uv sync`. To use muckrake from another project,
104
+ add it as a dependency (`uv add muckrake`) and run `uv run muckrake` from that
105
+ project's root.
106
+
107
+ Configuration is through environment variables, read after loading the nearest
108
+ `.env` file from the current directory upward; `.env.example` is a commented
109
+ template, and [docs/env.md](docs/env.md) lists every variable.
110
+
111
+ ## How it works
112
+
113
+ A project has a `datasets/` directory with one subdirectory per data source, each
114
+ holding a `config.yml` and a `crawler.py`. muckrake discovers datasets in
115
+ `./datasets/` under the current directory and in any `MUCKRAKE_DATASET_PATHS`.
116
+ Data moves through these stages:
117
+
118
+ 1. **Crawl** (`muckrake crawl <name>`) runs the dataset's `crawl(dataset)`
119
+ function, which emits FtM entities as statements. Each crawl records a row in
120
+ `dataset_runs` and writes an immutable artifact under `MUCKRAKE_ARTIFACT_PATH`
121
+ (default `data/artifacts/`); the latest successful or `partial` run is mirrored to
122
+ `data/datasets/<name>/statements.pack.csv`.
123
+ 2. **Extract** (`ner-extract`, `ner-review`, optional) splits text fields that
124
+ name several entities at once into separate entity candidates for review.
125
+ 3. **Load** (`muckrake load`) reads the statement packs, applies approved
126
+ extraction candidates, and writes the statements into the working database.
127
+ 4. **Deduplicate** (`xref`, `dedupe`, `dedupe-edges`) suggests, reviews and
128
+ records which entities are the same, using
129
+ [nomenklatura](https://github.com/opensanctions/nomenklatura)'s resolver.
130
+ 5. **Release** (`release-build`, `release-publish`) snapshots the latest dataset
131
+ runs into an immutable release, and publishes it into the separate
132
+ **published database** that applications read.
133
+
134
+ There are two databases:
135
+
136
+ - The **working database** (`MUCKRAKE_DATABASE_URL`, default SQLite at
137
+ `data/muckrake.db`) holds everything the pipeline produces and decides: crawl
138
+ runs, loaded statements, the resolver, extraction candidates, releases, and the
139
+ HTTP cache that `dataset.fetch_text/json/html` use (the `cache` table).
140
+ - The **published database** (`MUCKRAKE_PUBLISHED_DATABASE_URL`) holds only the
141
+ current release's statements and resolver, for applications to read.
142
+ `release-publish` refuses to run unless it is a different database from the
143
+ working one. Never `load` directly into it.
144
+
145
+ The `add`, `get`, `search` and `update` commands read and edit single entities in
146
+ the working database, for building a graph by hand during an investigation.
147
+
148
+ Anything that uses FtM must `import muckrake` first: on import it merges any
149
+ schema extensions (from `./ftm_schema_ext/` or `MUCKRAKE_FTM_SCHEMA_PATHS`) into
150
+ the FtM model and points `FTM_MODEL_PATH` at the result.
151
+
152
+ ## Guides
153
+
154
+ ### Writing a crawler
155
+
156
+ At minimum a dataset is a `config.yml` (FtM dataset metadata; `name` is what you
157
+ pass to `muckrake crawl`) and a `crawler.py` defining `crawl(dataset)`. The
158
+ crawler creates entities with `dataset.make(schema)`, gives each a stable ID with
159
+ `dataset.make_id(...)` (pass `reg_nr=` and `register=` to use an
160
+ [org-id](https://org-id.guide/) identifier such as `GB-COH-01234567`), and writes
161
+ them with `dataset.emit(entity)`. See the
162
+ [FtM schema explorer](https://followthemoney.tech/explorer/schemata/) for schemata
163
+ and properties.
164
+
165
+ Fetch sources with `dataset.fetch_text`, `fetch_json` or `fetch_html` (cached in
166
+ the working database's `cache` table for `cache_days=N`) or `dataset.fetch_resource`
167
+ (downloaded to `data/datasets/<name>/resources/`). Requests share a session that
168
+ retries with backoff; set `MUCKRAKE_HTTP_MIN_INTERVAL` to rate-limit each host.
169
+
170
+ A crawler should crash on data it can't interpret rather than emit a guess.
171
+
172
+ #### Units of work and `--retry-failed`
173
+
174
+ A crawler can split its work into independently retryable units (one source file,
175
+ one page, one record) by wrapping each in `dataset.run_unit(key, func, *args)`. A
176
+ unit's statements reach the pack only if `func` returns. If it raises, the unit is
177
+ recorded as failed, nothing from it is emitted, and the crawl carries on. A crawl
178
+ that completes with failed units finishes as `partial`: its output is usable by
179
+ `load` and `release-build`, and the failures are counted by error type in the run's
180
+ stats and the end-of-crawl log. Errors outside a unit still fail the whole crawl.
181
+
182
+ Each run writes a `units.jsonl` ledger next to its pack, recording where every
183
+ succeeded unit sits in the pack. `uv run muckrake crawl <name> --retry-failed`
184
+ copies the units that succeeded in the dataset's latest crawl into a new run and
185
+ re-runs everything else. That covers both retrying failures (e.g. after a parser
186
+ fix) and resuming a crawl that crashed or was killed partway. Pack and ledger are
187
+ written in place under the run's artifact directory as the crawl goes, so even a
188
+ killed crawl leaves its completed units behind.
189
+
190
+ #### Completeness warnings
191
+
192
+ When a crawler knows its output is missing something (a pagination cap was hit,
193
+ an API returned fewer items than it reported, a linked file has gone), it records
194
+ it with `dataset.warn_incomplete(reason, **context)` rather than only logging it.
195
+ Keep `reason` a fixed phrase and put the specifics in `context`. The run finishes
196
+ as `partial`, the warnings are counted by reason in the end-of-crawl log and
197
+ listed in the run's stats, and warnings raised inside a unit are carried over by
198
+ `--retry-failed`. `release-build` logs every `partial` input and records what it's
199
+ missing (failed units and warnings by reason) in the release manifest.
200
+
201
+ ### Entity extraction (NER)
202
+
203
+ Source data often packs several entities into one field, such as a list of
204
+ meeting attendees in one cell. `ner-extract` sends such texts to a splitter (a
205
+ deterministic `delimiter` splitter, or an LLM via OpenRouter or Anthropic) and
206
+ stores the result as candidates in the working database; `ner-review` approves or
207
+ rejects them, and only approved candidates are applied by `load`, `xref` and
208
+ `release-build`. `ner-export` and `ner-import` keep candidates outside the
209
+ database, so paid LLM results survive a lost database.
210
+
211
+ ```bash
212
+ uv run muckrake ner-extract <dataset> --extractor llm --limit 50
213
+ uv run muckrake ner-review
214
+ ```
215
+
216
+ `--extractor llm` needs `LLM_MODEL` with a provider prefix
217
+ (`openrouter:<model>` or `anthropic:<model>`) and the matching API key. See the
218
+ [NER docs](src/muckrake/extract/ner/README.md).
219
+
220
+ ### Deduplication
221
+
222
+ ```bash
223
+ uv run muckrake xref # suggest pairs of entities that may be the same
224
+ uv run muckrake dedupe # review the suggestions in a terminal UI
225
+ uv run muckrake dedupe-edges # merge duplicate Representation edges, no review step
226
+ ```
227
+
228
+ Decisions are stored in the resolver table in the working database and applied
229
+ when a release is published. `xref` considers at most 50,000 candidate pairs per
230
+ run (`--limit`). Set `MUCKRAKE_PROFILE_URL` to show a link to each entity in your
231
+ own application in the review UI. These commands, and `export`, need LevelDB (see
232
+ [Installation](#installation)). muckrake also provides the backend for a
233
+ multi-user web review UI, which an application can build on; see the
234
+ [dedupe docs](src/muckrake/dedupe/README.md).
235
+
236
+ ### Releases and publishing
237
+
238
+ ```bash
239
+ uv run muckrake release-build # every discovered dataset
240
+ uv run muckrake release-build <dataset> <dataset> # or just these
241
+ uv run muckrake release-list
242
+ uv run muckrake release-publish <release-id>
243
+ ```
244
+
245
+ `release-build` takes the latest successful (or `partial`) run of each dataset
246
+ and fails if any dataset has none, so name the datasets you want when some have
247
+ never been crawled. Set `MUCKRAKE_PUBLISHED_DATABASE_URL` to a separate database
248
+ before publishing.
249
+
250
+ `load` is atomic: every dataset in it is cleared and reloaded in a single
251
+ transaction, so a load that fails partway leaves the previously loaded statements
252
+ in place. A dataset with no statements pack is skipped with a warning, and its
253
+ loaded statements are kept. `load <dataset> --run-id N` loads a specific
254
+ immutable crawl run instead of the latest.
255
+
256
+ Publishing writes the release's statements in a single transaction, so readers
257
+ keep seeing the previous release until it commits, and a failure leaves the
258
+ previous statements in place. On Postgres, the release is loaded with `COPY` into
259
+ a new statement table, indexed and given its own search view, then swapped in with
260
+ renames, so readers only wait for the swap; a release of ~5M statements publishes
261
+ in minutes. On SQLite, the published statements are cleared and rewritten row by
262
+ row. One caveat: the resolver (deduplication decisions) is copied from the working
263
+ database first, in its own transaction, so a publish that fails after that step
264
+ leaves the previous statements with the current resolver.
265
+
266
+ ### Using Postgres
267
+
268
+ SQLite is the default and needs no setup. For larger datasets, run Postgres with
269
+ the bundled compose file (Postgres 18 on host port **5433**; an init script
270
+ creates the `muckrake` and `muckrake_published` databases):
271
+
272
+ ```bash
273
+ docker compose up -d db
274
+ ```
275
+
276
+ and set, in `.env`:
277
+
278
+ ```bash
279
+ MUCKRAKE_DATABASE_URL=postgresql://muckrake:muckrake@localhost:5433/muckrake
280
+ MUCKRAKE_PUBLISHED_DATABASE_URL=postgresql://muckrake:muckrake@localhost:5433/muckrake_published
281
+ ```
282
+
283
+ The compose file also has a CLI runner with LevelDB built in:
284
+ `docker compose run --rm muckrake <command>` is the container equivalent of
285
+ `uv run muckrake <command>`. It bind-mounts this repository at `/work` (its
286
+ working directory, so `datasets/`, `data/` and `.env` come from the host) and uses
287
+ the compose Postgres databases, so run the whole pipeline through it rather than
288
+ mixing it with a native SQLite setup.
@@ -0,0 +1,262 @@
1
+ # muckrake
2
+
3
+ A reusable pipeline for crawling public data sources into
4
+ [FollowTheMoney](https://followthemoney.tech/) (FtM) entities, resolving duplicates
5
+ across sources, and publishing immutable releases to a read-only database.
6
+ It is partly inspired by [`zavod`](https://zavod.opensanctions.org/) and
7
+ [other tools in the FollowTheMoney ecosystem](https://followthemoney.tech/community/stack/).
8
+
9
+ > [!WARNING]
10
+ > This is a work in progress. Expect breaking changes and incomplete features.
11
+
12
+ muckrake ships no datasets of its own: a project keeps its crawlers in its own
13
+ repository and runs muckrake from there. [OpenLobbying](https://github.com/openlobbying/openlobbying)
14
+ (UK lobbying and political finance) is the main project built on it.
15
+
16
+ - [Quickstart](#quickstart)
17
+ - [Installation](#installation)
18
+ - [How it works](#how-it-works)
19
+ - [Guides](#guides): [writing a crawler](#writing-a-crawler), [entity extraction](#entity-extraction-ner), [deduplication](#deduplication), [releases](#releases-and-publishing), [Postgres](#using-postgres)
20
+ - Reference: [CLI](docs/cli.md), [environment variables](docs/env.md), [NER](src/muckrake/extract/ner/README.md), [dedupe review](src/muckrake/dedupe/README.md), [changelog](CHANGELOG.md), [contributing](CONTRIBUTING.md)
21
+
22
+ ## Quickstart
23
+
24
+ Crawl the bundled example dataset, publish it, and search it, using SQLite only
25
+ (no Docker, no network access after installation). You need
26
+ [uv](https://docs.astral.sh/uv/) and the ICU and LevelDB libraries (see
27
+ [Installation](#installation); on macOS, `brew install icu4c pkg-config leveldb`).
28
+
29
+ ```bash
30
+ git clone https://github.com/openlobbying/muckrake.git
31
+ cd muckrake
32
+
33
+ # macOS only: let the PyICU build find Homebrew's ICU
34
+ export PKG_CONFIG_PATH="$(brew --prefix icu4c)/lib/pkgconfig"
35
+ uv sync
36
+
37
+ # Discover the example dataset in examples/hello_dataset/
38
+ export MUCKRAKE_DATASET_PATHS=examples
39
+ # release-publish writes to a separate published database, and refuses to run without one
40
+ export MUCKRAKE_PUBLISHED_DATABASE_URL=sqlite:///data/published.db
41
+
42
+ uv run muckrake list
43
+ uv run muckrake -v crawl hello # writes data/datasets/hello/statements.pack.csv
44
+ uv run muckrake -v load hello # loads it into the working DB, data/muckrake.db
45
+ uv run muckrake release-build hello # prints "Built release 1"
46
+ uv run muckrake -v release-publish 1 # copies release 1 into data/published.db
47
+ uv run muckrake search acme --pretty # finds the two example companies
48
+ ```
49
+
50
+ `-v` turns on progress logging; without it, `crawl`, `load` and `release-publish`
51
+ print nothing on success. The example crawler is
52
+ [`examples/hello_dataset/crawler.py`](examples/hello_dataset/crawler.py): copy it
53
+ as the starting point for your own.
54
+
55
+ ## Installation
56
+
57
+ muckrake needs Python 3.13 (uv installs it) and two system libraries that some
58
+ dependencies compile against:
59
+
60
+ - **ICU** and **pkg-config**, for PyICU (used for name normalisation). Required.
61
+ - macOS: `brew install icu4c pkg-config`. Homebrew doesn't link `icu4c` into
62
+ the default search path, so export
63
+ `PKG_CONFIG_PATH="$(brew --prefix icu4c)/lib/pkgconfig"` before `uv sync`.
64
+ - Debian/Ubuntu: `sudo apt-get install libicu-dev pkg-config`.
65
+ - **LevelDB**, for plyvel. Required to install: plyvel is a dependency with no
66
+ binary wheels, so `uv sync` compiles it against the LevelDB headers.
67
+ - macOS: `brew install leveldb`.
68
+ - Debian/Ubuntu: `sudo apt-get install libleveldb-dev`.
69
+
70
+ At runtime only the `xref`, `dedupe`, `dedupe-edges` and `export` commands load
71
+ plyvel. On macOS, plyvel builds against Homebrew's LevelDB but fails on import
72
+ (`symbol not found ... leveldb10ComparatorE`), so run those four commands in the
73
+ container (see [Using Postgres](#using-postgres)); every other command runs
74
+ natively. Making plyvel an optional dependency is tracked in
75
+ [Make plyvel (LevelDB) an optional extra](https://github.com/openlobbying/muckrake/issues/54).
76
+
77
+ Then, in a clone of this repo, `uv sync`. To use muckrake from another project,
78
+ add it as a dependency (`uv add muckrake`) and run `uv run muckrake` from that
79
+ project's root.
80
+
81
+ Configuration is through environment variables, read after loading the nearest
82
+ `.env` file from the current directory upward; `.env.example` is a commented
83
+ template, and [docs/env.md](docs/env.md) lists every variable.
84
+
85
+ ## How it works
86
+
87
+ A project has a `datasets/` directory with one subdirectory per data source, each
88
+ holding a `config.yml` and a `crawler.py`. muckrake discovers datasets in
89
+ `./datasets/` under the current directory and in any `MUCKRAKE_DATASET_PATHS`.
90
+ Data moves through these stages:
91
+
92
+ 1. **Crawl** (`muckrake crawl <name>`) runs the dataset's `crawl(dataset)`
93
+ function, which emits FtM entities as statements. Each crawl records a row in
94
+ `dataset_runs` and writes an immutable artifact under `MUCKRAKE_ARTIFACT_PATH`
95
+ (default `data/artifacts/`); the latest successful or `partial` run is mirrored to
96
+ `data/datasets/<name>/statements.pack.csv`.
97
+ 2. **Extract** (`ner-extract`, `ner-review`, optional) splits text fields that
98
+ name several entities at once into separate entity candidates for review.
99
+ 3. **Load** (`muckrake load`) reads the statement packs, applies approved
100
+ extraction candidates, and writes the statements into the working database.
101
+ 4. **Deduplicate** (`xref`, `dedupe`, `dedupe-edges`) suggests, reviews and
102
+ records which entities are the same, using
103
+ [nomenklatura](https://github.com/opensanctions/nomenklatura)'s resolver.
104
+ 5. **Release** (`release-build`, `release-publish`) snapshots the latest dataset
105
+ runs into an immutable release, and publishes it into the separate
106
+ **published database** that applications read.
107
+
108
+ There are two databases:
109
+
110
+ - The **working database** (`MUCKRAKE_DATABASE_URL`, default SQLite at
111
+ `data/muckrake.db`) holds everything the pipeline produces and decides: crawl
112
+ runs, loaded statements, the resolver, extraction candidates, releases, and the
113
+ HTTP cache that `dataset.fetch_text/json/html` use (the `cache` table).
114
+ - The **published database** (`MUCKRAKE_PUBLISHED_DATABASE_URL`) holds only the
115
+ current release's statements and resolver, for applications to read.
116
+ `release-publish` refuses to run unless it is a different database from the
117
+ working one. Never `load` directly into it.
118
+
119
+ The `add`, `get`, `search` and `update` commands read and edit single entities in
120
+ the working database, for building a graph by hand during an investigation.
121
+
122
+ Anything that uses FtM must `import muckrake` first: on import it merges any
123
+ schema extensions (from `./ftm_schema_ext/` or `MUCKRAKE_FTM_SCHEMA_PATHS`) into
124
+ the FtM model and points `FTM_MODEL_PATH` at the result.
125
+
126
+ ## Guides
127
+
128
+ ### Writing a crawler
129
+
130
+ At minimum a dataset is a `config.yml` (FtM dataset metadata; `name` is what you
131
+ pass to `muckrake crawl`) and a `crawler.py` defining `crawl(dataset)`. The
132
+ crawler creates entities with `dataset.make(schema)`, gives each a stable ID with
133
+ `dataset.make_id(...)` (pass `reg_nr=` and `register=` to use an
134
+ [org-id](https://org-id.guide/) identifier such as `GB-COH-01234567`), and writes
135
+ them with `dataset.emit(entity)`. See the
136
+ [FtM schema explorer](https://followthemoney.tech/explorer/schemata/) for schemata
137
+ and properties.
138
+
139
+ Fetch sources with `dataset.fetch_text`, `fetch_json` or `fetch_html` (cached in
140
+ the working database's `cache` table for `cache_days=N`) or `dataset.fetch_resource`
141
+ (downloaded to `data/datasets/<name>/resources/`). Requests share a session that
142
+ retries with backoff; set `MUCKRAKE_HTTP_MIN_INTERVAL` to rate-limit each host.
143
+
144
+ A crawler should crash on data it can't interpret rather than emit a guess.
145
+
146
+ #### Units of work and `--retry-failed`
147
+
148
+ A crawler can split its work into independently retryable units (one source file,
149
+ one page, one record) by wrapping each in `dataset.run_unit(key, func, *args)`. A
150
+ unit's statements reach the pack only if `func` returns. If it raises, the unit is
151
+ recorded as failed, nothing from it is emitted, and the crawl carries on. A crawl
152
+ that completes with failed units finishes as `partial`: its output is usable by
153
+ `load` and `release-build`, and the failures are counted by error type in the run's
154
+ stats and the end-of-crawl log. Errors outside a unit still fail the whole crawl.
155
+
156
+ Each run writes a `units.jsonl` ledger next to its pack, recording where every
157
+ succeeded unit sits in the pack. `uv run muckrake crawl <name> --retry-failed`
158
+ copies the units that succeeded in the dataset's latest crawl into a new run and
159
+ re-runs everything else. That covers both retrying failures (e.g. after a parser
160
+ fix) and resuming a crawl that crashed or was killed partway. Pack and ledger are
161
+ written in place under the run's artifact directory as the crawl goes, so even a
162
+ killed crawl leaves its completed units behind.
163
+
164
+ #### Completeness warnings
165
+
166
+ When a crawler knows its output is missing something (a pagination cap was hit,
167
+ an API returned fewer items than it reported, a linked file has gone), it records
168
+ it with `dataset.warn_incomplete(reason, **context)` rather than only logging it.
169
+ Keep `reason` a fixed phrase and put the specifics in `context`. The run finishes
170
+ as `partial`, the warnings are counted by reason in the end-of-crawl log and
171
+ listed in the run's stats, and warnings raised inside a unit are carried over by
172
+ `--retry-failed`. `release-build` logs every `partial` input and records what it's
173
+ missing (failed units and warnings by reason) in the release manifest.
174
+
175
+ ### Entity extraction (NER)
176
+
177
+ Source data often packs several entities into one field, such as a list of
178
+ meeting attendees in one cell. `ner-extract` sends such texts to a splitter (a
179
+ deterministic `delimiter` splitter, or an LLM via OpenRouter or Anthropic) and
180
+ stores the result as candidates in the working database; `ner-review` approves or
181
+ rejects them, and only approved candidates are applied by `load`, `xref` and
182
+ `release-build`. `ner-export` and `ner-import` keep candidates outside the
183
+ database, so paid LLM results survive a lost database.
184
+
185
+ ```bash
186
+ uv run muckrake ner-extract <dataset> --extractor llm --limit 50
187
+ uv run muckrake ner-review
188
+ ```
189
+
190
+ `--extractor llm` needs `LLM_MODEL` with a provider prefix
191
+ (`openrouter:<model>` or `anthropic:<model>`) and the matching API key. See the
192
+ [NER docs](src/muckrake/extract/ner/README.md).
193
+
194
+ ### Deduplication
195
+
196
+ ```bash
197
+ uv run muckrake xref # suggest pairs of entities that may be the same
198
+ uv run muckrake dedupe # review the suggestions in a terminal UI
199
+ uv run muckrake dedupe-edges # merge duplicate Representation edges, no review step
200
+ ```
201
+
202
+ Decisions are stored in the resolver table in the working database and applied
203
+ when a release is published. `xref` considers at most 50,000 candidate pairs per
204
+ run (`--limit`). Set `MUCKRAKE_PROFILE_URL` to show a link to each entity in your
205
+ own application in the review UI. These commands, and `export`, need LevelDB (see
206
+ [Installation](#installation)). muckrake also provides the backend for a
207
+ multi-user web review UI, which an application can build on; see the
208
+ [dedupe docs](src/muckrake/dedupe/README.md).
209
+
210
+ ### Releases and publishing
211
+
212
+ ```bash
213
+ uv run muckrake release-build # every discovered dataset
214
+ uv run muckrake release-build <dataset> <dataset> # or just these
215
+ uv run muckrake release-list
216
+ uv run muckrake release-publish <release-id>
217
+ ```
218
+
219
+ `release-build` takes the latest successful (or `partial`) run of each dataset
220
+ and fails if any dataset has none, so name the datasets you want when some have
221
+ never been crawled. Set `MUCKRAKE_PUBLISHED_DATABASE_URL` to a separate database
222
+ before publishing.
223
+
224
+ `load` is atomic: every dataset in it is cleared and reloaded in a single
225
+ transaction, so a load that fails partway leaves the previously loaded statements
226
+ in place. A dataset with no statements pack is skipped with a warning, and its
227
+ loaded statements are kept. `load <dataset> --run-id N` loads a specific
228
+ immutable crawl run instead of the latest.
229
+
230
+ Publishing writes the release's statements in a single transaction, so readers
231
+ keep seeing the previous release until it commits, and a failure leaves the
232
+ previous statements in place. On Postgres, the release is loaded with `COPY` into
233
+ a new statement table, indexed and given its own search view, then swapped in with
234
+ renames, so readers only wait for the swap; a release of ~5M statements publishes
235
+ in minutes. On SQLite, the published statements are cleared and rewritten row by
236
+ row. One caveat: the resolver (deduplication decisions) is copied from the working
237
+ database first, in its own transaction, so a publish that fails after that step
238
+ leaves the previous statements with the current resolver.
239
+
240
+ ### Using Postgres
241
+
242
+ SQLite is the default and needs no setup. For larger datasets, run Postgres with
243
+ the bundled compose file (Postgres 18 on host port **5433**; an init script
244
+ creates the `muckrake` and `muckrake_published` databases):
245
+
246
+ ```bash
247
+ docker compose up -d db
248
+ ```
249
+
250
+ and set, in `.env`:
251
+
252
+ ```bash
253
+ MUCKRAKE_DATABASE_URL=postgresql://muckrake:muckrake@localhost:5433/muckrake
254
+ MUCKRAKE_PUBLISHED_DATABASE_URL=postgresql://muckrake:muckrake@localhost:5433/muckrake_published
255
+ ```
256
+
257
+ The compose file also has a CLI runner with LevelDB built in:
258
+ `docker compose run --rm muckrake <command>` is the container equivalent of
259
+ `uv run muckrake <command>`. It bind-mounts this repository at `/work` (its
260
+ working directory, so `datasets/`, `data/` and `.env` come from the host) and uses
261
+ the compose Postgres databases, so run the whole pipeline through it rather than
262
+ mixing it with a native SQLite setup.
@@ -0,0 +1,83 @@
1
+ [project]
2
+ name = "muckrake"
3
+ version = "0.3.1"
4
+ description = "A data-pipeline framework for crawling, deduplicating and publishing FollowTheMoney entities."
5
+ readme = "README.md"
6
+ requires-python = ">=3.13"
7
+ dependencies = [
8
+ "beautifulsoup4>=4.14.3",
9
+ "click>=8.3.1",
10
+ "datapatch>=1.2.4",
11
+ "followthemoney>=4.4.0",
12
+ "org-id>=0.1.1",
13
+ "lxml>=6.0.2",
14
+ "nomenklatura>=4.4.1",
15
+ "openpyxl>=3.1.5",
16
+ "pandas>=2.3.3",
17
+ "pydantic-ai-slim[anthropic,google,openrouter]>=1.0.0,<2",
18
+ "psycopg[binary]>=3.2.12",
19
+ "plyvel>=1.5.1",
20
+ "pyyaml>=6.0.3",
21
+ "requests>=2.32.5",
22
+ "xlrd>=2.0.2",
23
+ "logfire[httpx,sqlalchemy]>=4.24.0",
24
+ "odfpy>=1.4.1",
25
+ ]
26
+
27
+ [[project.authors]]
28
+ name = "Nicu Calcea"
29
+ email = "mail@nicu.md"
30
+
31
+ [project.scripts]
32
+ muckrake = "muckrake.cli:cli"
33
+
34
+ [build-system]
35
+ requires = ["uv_build>=0.9.17,<0.10.0"]
36
+ build-backend = "uv_build"
37
+
38
+ [dependency-groups]
39
+ dev = [
40
+ "pytest>=9.0.3",
41
+ "ruff>=0.15",
42
+ "mypy>=1.19",
43
+ "pre-commit>=4.0",
44
+ "types-requests>=2.32",
45
+ "types-pyyaml>=6.0",
46
+ ]
47
+
48
+ [tool.pytest.ini_options]
49
+ testpaths = ["tests"]
50
+
51
+ [tool.ruff]
52
+ line-length = 100
53
+ target-version = "py313"
54
+
55
+ [tool.ruff.lint]
56
+ select = [
57
+ "E",
58
+ "F",
59
+ "I",
60
+ "UP",
61
+ "B",
62
+ ]
63
+
64
+ [tool.ruff.lint.per-file-ignores]
65
+ "src/muckrake/extract/ner/engines/llm_prompt.py" = ["E501"]
66
+
67
+ [tool.mypy]
68
+ python_version = "3.13"
69
+ check_untyped_defs = true
70
+ warn_unused_ignores = true
71
+
72
+ [[tool.mypy.overrides]]
73
+ module = [
74
+ "banal.*",
75
+ "datapatch.*",
76
+ "plyvel.*",
77
+ "org_id.*",
78
+ "xlrd.*",
79
+ "odf.*",
80
+ "lxml.*",
81
+ "bs4.*",
82
+ ]
83
+ ignore_missing_imports = true