muckrake 0.2.1__py3-none-any.whl

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 (48) hide show
  1. muckrake/__init__.py +61 -0
  2. muckrake/artifacts.py +72 -0
  3. muckrake/cli.py +522 -0
  4. muckrake/crawl.py +223 -0
  5. muckrake/dataset.py +231 -0
  6. muckrake/db.py +478 -0
  7. muckrake/dedupe/README.md +52 -0
  8. muckrake/dedupe/__init__.py +37 -0
  9. muckrake/dedupe/cluster.py +534 -0
  10. muckrake/dedupe/dedupe.py +133 -0
  11. muckrake/dedupe/dedupe_edges.py +214 -0
  12. muckrake/dedupe/review.py +329 -0
  13. muckrake/entity_query.py +114 -0
  14. muckrake/entity_write.py +244 -0
  15. muckrake/env.py +52 -0
  16. muckrake/export.py +40 -0
  17. muckrake/extract/__init__.py +0 -0
  18. muckrake/extract/fetch.py +186 -0
  19. muckrake/extract/ner/README.md +210 -0
  20. muckrake/extract/ner/__init__.py +4 -0
  21. muckrake/extract/ner/engines/__init__.py +32 -0
  22. muckrake/extract/ner/engines/base.py +14 -0
  23. muckrake/extract/ner/engines/delimited.py +39 -0
  24. muckrake/extract/ner/engines/llm.py +164 -0
  25. muckrake/extract/ner/engines/llm_prompt.py +44 -0
  26. muckrake/extract/ner/materialize.py +255 -0
  27. muckrake/extract/ner/pipeline.py +151 -0
  28. muckrake/extract/ner/review.py +706 -0
  29. muckrake/extract/ner/storage.py +200 -0
  30. muckrake/id.py +3 -0
  31. muckrake/load.py +70 -0
  32. muckrake/logging.py +56 -0
  33. muckrake/py.typed +0 -0
  34. muckrake/release.py +356 -0
  35. muckrake/runs.py +228 -0
  36. muckrake/search.py +335 -0
  37. muckrake/serialize.py +119 -0
  38. muckrake/settings.py +41 -0
  39. muckrake/store.py +117 -0
  40. muckrake/util.py +27 -0
  41. muckrake/utils/__init__.py +13 -0
  42. muckrake/utils/dates.py +438 -0
  43. muckrake/utils/gb_coh.py +36 -0
  44. muckrake/view.py +58 -0
  45. muckrake-0.2.1.dist-info/METADATA +136 -0
  46. muckrake-0.2.1.dist-info/RECORD +48 -0
  47. muckrake-0.2.1.dist-info/WHEEL +4 -0
  48. muckrake-0.2.1.dist-info/entry_points.txt +3 -0
muckrake/view.py ADDED
@@ -0,0 +1,58 @@
1
+ from __future__ import annotations
2
+
3
+ from functools import lru_cache
4
+ from typing import Any, Dict, List
5
+
6
+ from nomenklatura.db import get_engine
7
+ from sqlalchemy import text
8
+
9
+ from muckrake.dataset import list_dataset_names
10
+ from muckrake.serialize import get_all_datasets_metadata, serialize_entity
11
+ from muckrake.settings import PUBLISHED_SQL_URI
12
+ from muckrake.store import get_sql_store
13
+
14
+
15
+ @lru_cache(maxsize=1)
16
+ def get_published_engine():
17
+ return get_engine(PUBLISHED_SQL_URI)
18
+
19
+
20
+ def _list_db_dataset_names() -> List[str]:
21
+ engine = get_published_engine()
22
+ with engine.connect() as conn:
23
+ result = conn.execute(
24
+ text(
25
+ "SELECT DISTINCT dataset FROM statement "
26
+ "WHERE dataset IS NOT NULL AND dataset != '' ORDER BY dataset"
27
+ )
28
+ )
29
+ return [row[0] for row in result]
30
+
31
+
32
+ @lru_cache(maxsize=1)
33
+ def list_all_dataset_names() -> List[str]:
34
+ names = set(list_dataset_names())
35
+ try:
36
+ names.update(_list_db_dataset_names())
37
+ except Exception:
38
+ pass
39
+ return sorted(names)
40
+
41
+
42
+ @lru_cache(maxsize=1)
43
+ def get_view():
44
+ store = get_sql_store(list_all_dataset_names(), uri=PUBLISHED_SQL_URI)
45
+ return store.default_view(external=True)
46
+
47
+
48
+ @lru_cache(maxsize=2000)
49
+ def get_entity_details(entity_id: str) -> Dict[str, str]:
50
+ view = get_view()
51
+ ent = view.get_entity(entity_id)
52
+ if ent is None:
53
+ return {"caption": entity_id, "schema": "Entity"}
54
+ return {"caption": ent.caption, "schema": ent.schema.name}
55
+
56
+
57
+ def serialize_view_entity(ent) -> Dict[str, Any]:
58
+ return serialize_entity(ent, get_all_datasets_metadata(), get_entity_details)
@@ -0,0 +1,136 @@
1
+ Metadata-Version: 2.3
2
+ Name: muckrake
3
+ Version: 0.2.1
4
+ Summary: A framework for creating and storing FollowTheMoney entities, used by OpenLobbying.
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.0
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[google,openrouter]>=1.0.0
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
+ A reusable framework for creating and storing [FollowTheMoney](https://followthemoney.tech/) entities.
28
+
29
+ > [!WARNING]
30
+ > This is a work in progress. Expect breaking changes and incomplete features.
31
+
32
+ ## Muckrake
33
+
34
+ Muckrake is the data pipeline. It is partially inspired by [`zavod`](https://zavod.opensanctions.org/) and [other tools in the FollowTheMoney ecosystems](https://followthemoney.tech/community/stack/).
35
+
36
+ Run `uv run muckrake --help` for a full list of available commands.
37
+
38
+ Install Python dependencies with `uv sync`. This now includes the external `org-id` package used for structured organization identifiers.
39
+
40
+ OpenLobbying-specific code now lives in the sibling `../openlobbying/` repository. That repo owns:
41
+
42
+ - the OpenLobbying dataset crawlers
43
+ - the OpenLobbying FastAPI application
44
+ - the OpenLobbying Svelte frontend
45
+ - any project-specific FtM schema extensions
46
+ - deployment assets for the public site
47
+
48
+ ### Crawlers
49
+
50
+ Muckrake discovers crawler configs from `./datasets/` in the current working directory and any paths listed in `MUCKRAKE_DATASET_PATHS`. At a minimum, each dataset consists of a `config.yml` with metadata and a `crawler.py` script that outputs FollowTheMoney statements in CSV format.
51
+
52
+ To crawl a dataset, run `uv run muckrake crawl {dataset_name}`. Run `uv run muckrake list` to see available datasets.
53
+
54
+ Each crawl now creates a `dataset_runs` record in Postgres and stores immutable artifacts under `MUCKRAKE_ARTIFACT_PATH` (defaults to `data/artifacts`). The latest successful run remains mirrored into `data/datasets/{name}/statements.pack.csv` for local compatibility.
55
+
56
+ ### AI-based NER
57
+
58
+ Many data sources have [composite fields that contain multiple entities](https://openaccess.transparency.org.uk/?meeting=10088). We use LLMs to extract unique entities and relationships from these fields, and store them as candidates in the database for review and approval. See [NER docs](/src/muckrake/extract/ner/README.md) for details.
59
+
60
+ ```bash
61
+ # Create extraction candidates for one dataset
62
+ uv run muckrake ner-extract open_access --extractor llm --limit 50
63
+
64
+ # Review candidates in a terminal UI
65
+ uv run muckrake ner-review
66
+ ```
67
+
68
+ ### Dedupe
69
+
70
+ Our goal is to link entities across datasets to provide a unified view of lobbying and political finance for any given person, company, or organisation.
71
+
72
+ ```bash
73
+ # Create dedupe candidates across all datasets
74
+ uv run muckrake xref
75
+
76
+ # Review candidates in a terminal UI
77
+ uv run muckrake dedupe
78
+ ```
79
+
80
+ We also want to collapse duplicate relationship edges across datasets, especially for ORCL and PRCA. This is done automatically, no review step required.
81
+
82
+ ```bash
83
+ uv run muckrake dedupe-edges
84
+ ```
85
+
86
+ ### Loading
87
+
88
+ Statements are loaded into Postgres with `uv run muckrake load`. This reads the statements CSV files and applies any approved NER candidates before materialising entities and relationships.
89
+
90
+ To load from a specific immutable crawl snapshot instead of the local workspace copy:
91
+
92
+ ```bash
93
+ uv run muckrake load gb_political_finance --run-id 123
94
+ ```
95
+
96
+ For the published site, prefer the release workflow instead of loading directly into the serving database:
97
+
98
+ ```bash
99
+ uv run muckrake release-build
100
+ uv run muckrake release-publish 1
101
+ ```
102
+
103
+
104
+ ## OpenLobbying
105
+
106
+ The primary user of Muckrake data is [OpenLobbying](https://openlobbying.org/), an open database of lobbying and political finance data. See `../openlobbying/README.md` for app setup, API serving, and frontend development.
107
+
108
+ ## Environment setup
109
+
110
+ - Copy `.env.example` to `.env` in the repo root.
111
+ - By default `muckrake` loads the nearest `.env` from the current working directory upward. Override that with `MUCKRAKE_ENV_FILE` if needed.
112
+ - Default local database setup:
113
+ - working DB: `sqlite:///data/muckrake.db`
114
+ - published DB: same as the working DB unless `MUCKRAKE_PUBLISHED_DATABASE_URL` is set
115
+ - Required only if you want Postgres:
116
+ - `MUCKRAKE_DATABASE_URL`
117
+ - Common local settings:
118
+ - `MUCKRAKE_PUBLISHED_DATABASE_URL` for a separate published API database
119
+ - Optional local overrides:
120
+ - `MUCKRAKE_DATA_PATH`
121
+ - `MUCKRAKE_ARTIFACT_PATH`
122
+ - `MUCKRAKE_DATASET_PATHS`
123
+ - `MUCKRAKE_FTM_SCHEMA_PATHS`
124
+ - `MUCKRAKE_ENV_FILE`
125
+ - `FTM_MODEL_PATH` if you need to override the merged FollowTheMoney model entirely
126
+ - `OPENROUTER_API_KEY`, `LLM_MODEL`, `NER_LLM_PROMPT_FILE`, `LOGFIRE_TOKEN`
127
+ - Example:
128
+
129
+ ```bash
130
+ cp .env.example .env
131
+ ```
132
+
133
+ ## Consumers
134
+
135
+ - `../openlobbying/`: OpenLobbying application repo built on top of `muckrake`
136
+ - `../us-congress-lobbying/`: project-specific investigative sandbox
@@ -0,0 +1,48 @@
1
+ muckrake/__init__.py,sha256=RNe0XtS_Af9Dk69lxzbVmMJiuwv1ltK0X8p9yG0d_pI,1730
2
+ muckrake/artifacts.py,sha256=gTzs9B8ceu8SHEpyJHxF-yWTqLYBdaGvJfsz5CnUovg,2194
3
+ muckrake/cli.py,sha256=amzDmCFWIbD8hcqhgNFy1_qXvKcHZkGHb5X5oDloPSQ,16389
4
+ muckrake/crawl.py,sha256=f1IJP2lB0-NA2_FWROcFQKAv4eEkTEGL22kxhJA3834,8198
5
+ muckrake/dataset.py,sha256=QrkjviZi7evcZbyB7t9GgKgUScbo5f1urq9nLUuNqMU,7628
6
+ muckrake/db.py,sha256=bRcUdPJhq21tmXs73M9HdeBuo3sCCu9b-7_ZygIu40g,16680
7
+ muckrake/dedupe/README.md,sha256=hvRpYGGXS7AgIv3n8r5-nTdqQd_xblpROEVYO2uTTAE,4071
8
+ muckrake/dedupe/__init__.py,sha256=YmK4IQ-6At9ZMUnWH8WvIjOTdYXw94fuNxnXptZMSz4,777
9
+ muckrake/dedupe/cluster.py,sha256=zyWp9oGOdCExaf7IEuGXOVHYPfP2sgDYscZQLRlQxLQ,15877
10
+ muckrake/dedupe/dedupe.py,sha256=loFDLfaWmj4lcpKQkRAk1saejD9FSS_AGumeJyclwkY,4067
11
+ muckrake/dedupe/dedupe_edges.py,sha256=EE1qwxAEi_-D_tMVI8bveMWwruz3VJgL3gVnfC_X5GE,6292
12
+ muckrake/dedupe/review.py,sha256=iMmRlINLgueXmB49r9RBoF5rYaNEYJBpoH3XTJ1dEEo,9087
13
+ muckrake/entity_query.py,sha256=gJi0Rjx133QYLHq7PjyEhI9Mxi_QPIyX40cg5mt2W6I,3455
14
+ muckrake/entity_write.py,sha256=5RmpCtoaRZIUookNmupbBzB5I32ZenvpXPVxLdOQawQ,7493
15
+ muckrake/env.py,sha256=2vm4CjRKvWjWSvacC28nmKvrv2XfomZQpw7gix2jxjg,1374
16
+ muckrake/export.py,sha256=jnpAu3wBLh56aonzdi2G30-5wxcGLQvJZTxoBdbQefQ,1424
17
+ muckrake/extract/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
+ muckrake/extract/fetch.py,sha256=XAb6BAdn4JP5VQgP-VVKXZrghA30NvoTgY_9xWzyg30,5114
19
+ muckrake/extract/ner/README.md,sha256=0p07znmVm26vBx324wtz6ufzL3mV8vNHZVQJTa0dhHo,5853
20
+ muckrake/extract/ner/__init__.py,sha256=_pSVu8bnJxgEKnCGcTsF6lUc7HuBGaHrwgmn5B6BUFs,122
21
+ muckrake/extract/ner/engines/__init__.py,sha256=kVTsFwuWGjre2hG--ikl3vdeIcxLiOQWF606-vqtNUw,719
22
+ muckrake/extract/ner/engines/base.py,sha256=QWYpqnbR25fvIU8E8NZUGzoJUxerUVNRaMf4itbIQS4,266
23
+ muckrake/extract/ner/engines/delimited.py,sha256=uULBnHdY7CQPwS1qIiQSAhR6E7YIy3jBwRdhOZ_HEAk,1059
24
+ muckrake/extract/ner/engines/llm.py,sha256=xawwFxS7X9WTP08s3MMlzDZ67d1idKlc9KF5j2hikQI,6348
25
+ muckrake/extract/ner/engines/llm_prompt.py,sha256=NB_gRTFYIaOFcoNazlv38FX7mNU0hT98kxbd6Ieni4U,3071
26
+ muckrake/extract/ner/materialize.py,sha256=rUlySO6P5b9SZL5xC40yDDqRT94UV-kRNVUTbe1VUfw,8760
27
+ muckrake/extract/ner/pipeline.py,sha256=YR3KVZPlRT-yZ2NdeRCA_eIv_IqgbJVJFx7BSh1Z1V8,4333
28
+ muckrake/extract/ner/review.py,sha256=Upq2_kRV4Q8wAQPqXM3kpXtV3ke778L48UNL3AACKPI,21454
29
+ muckrake/extract/ner/storage.py,sha256=FZiCOKvQO1-a-G8wd5MYenRtRDywusqA4ep3YZcmHT0,5655
30
+ muckrake/id.py,sha256=uBs84UKqqBNNHRurk4Nnfj3AjWC7_MFr6yemoH0v--A,116
31
+ muckrake/load.py,sha256=tIAo-JJr6BVUkWsBEuseNy62RbyxET6Js9V5a6WKxqc,2722
32
+ muckrake/logging.py,sha256=BIOE9RV7wyxxpiqgX9WqtnqQkCPkz7a9OxBg8MW2avY,1489
33
+ muckrake/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
34
+ muckrake/release.py,sha256=Klzty_gM8GWttHNs5EhxBmBC8YSKn7dUSy9bvpSDrSw,12005
35
+ muckrake/runs.py,sha256=AZvoF9bfhvwiFUVsM8WNeP75rniys5cqsbJ6apAYgxU,6451
36
+ muckrake/search.py,sha256=QRe3lyftr4iFi5yQORE2RnoRiulN-LwQFhXyTb5-uC0,10083
37
+ muckrake/serialize.py,sha256=O_8YTXQJKmOSwpRoFNUgh2U_33yZBrUBQKJfcvYJ8so,3917
38
+ muckrake/settings.py,sha256=F4Eq2yxHg5PP2H3r6dc97kKP4a2mX5D0E6Je6ZTSsPg,1239
39
+ muckrake/store.py,sha256=9rN6u8rI6T1ZSB9lh0iDcU8u3ul9EeB2BUMgEC-Zvrg,4328
40
+ muckrake/util.py,sha256=Xn40-923a5_HjrfFGxo8sfSADH_1iyx4IbSxnYMjpPA,758
41
+ muckrake/utils/__init__.py,sha256=wqaB3KP6pl0G6v08Kr9JJkxx-xggOAxpsmNA6V3uw-4,404
42
+ muckrake/utils/dates.py,sha256=EU9fN-D7R8v4Pr6yi7HZnbHjAuWVgBFErDtWDYzPc-8,14668
43
+ muckrake/utils/gb_coh.py,sha256=aeATRSh7Z3gJ1cyYsJE7Rr9wyzp2vixnaq5eX4eBmWQ,1135
44
+ muckrake/view.py,sha256=qMbwtqsflRWWYZXqWLcw7AjFo0e5WH0ms_DFPJyGhjY,1645
45
+ muckrake-0.2.1.dist-info/WHEEL,sha256=V5-3dKee3Zs8C4JP6swr6zdqriLsOpItBEQxe6_oWpY,81
46
+ muckrake-0.2.1.dist-info/entry_points.txt,sha256=yJQSNbslNsaKG4KL7cZdIynaO066n-8X6IxBlIzhhLw,47
47
+ muckrake-0.2.1.dist-info/METADATA,sha256=z3Puicx0WbluUczaexyjKX4WseOa1bAgvihKhXWzcX4,5381
48
+ muckrake-0.2.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.11.18
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ muckrake = muckrake.cli:cli
3
+