muckrake 0.2.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 (47) hide show
  1. muckrake-0.2.1/PKG-INFO +136 -0
  2. muckrake-0.2.1/README.md +110 -0
  3. muckrake-0.2.1/pyproject.toml +40 -0
  4. muckrake-0.2.1/src/muckrake/__init__.py +61 -0
  5. muckrake-0.2.1/src/muckrake/artifacts.py +72 -0
  6. muckrake-0.2.1/src/muckrake/cli.py +522 -0
  7. muckrake-0.2.1/src/muckrake/crawl.py +223 -0
  8. muckrake-0.2.1/src/muckrake/dataset.py +231 -0
  9. muckrake-0.2.1/src/muckrake/db.py +478 -0
  10. muckrake-0.2.1/src/muckrake/dedupe/README.md +52 -0
  11. muckrake-0.2.1/src/muckrake/dedupe/__init__.py +37 -0
  12. muckrake-0.2.1/src/muckrake/dedupe/cluster.py +534 -0
  13. muckrake-0.2.1/src/muckrake/dedupe/dedupe.py +133 -0
  14. muckrake-0.2.1/src/muckrake/dedupe/dedupe_edges.py +214 -0
  15. muckrake-0.2.1/src/muckrake/dedupe/review.py +329 -0
  16. muckrake-0.2.1/src/muckrake/entity_query.py +114 -0
  17. muckrake-0.2.1/src/muckrake/entity_write.py +244 -0
  18. muckrake-0.2.1/src/muckrake/env.py +52 -0
  19. muckrake-0.2.1/src/muckrake/export.py +40 -0
  20. muckrake-0.2.1/src/muckrake/extract/__init__.py +0 -0
  21. muckrake-0.2.1/src/muckrake/extract/fetch.py +186 -0
  22. muckrake-0.2.1/src/muckrake/extract/ner/README.md +210 -0
  23. muckrake-0.2.1/src/muckrake/extract/ner/__init__.py +4 -0
  24. muckrake-0.2.1/src/muckrake/extract/ner/engines/__init__.py +32 -0
  25. muckrake-0.2.1/src/muckrake/extract/ner/engines/base.py +14 -0
  26. muckrake-0.2.1/src/muckrake/extract/ner/engines/delimited.py +39 -0
  27. muckrake-0.2.1/src/muckrake/extract/ner/engines/llm.py +164 -0
  28. muckrake-0.2.1/src/muckrake/extract/ner/engines/llm_prompt.py +44 -0
  29. muckrake-0.2.1/src/muckrake/extract/ner/materialize.py +255 -0
  30. muckrake-0.2.1/src/muckrake/extract/ner/pipeline.py +151 -0
  31. muckrake-0.2.1/src/muckrake/extract/ner/review.py +706 -0
  32. muckrake-0.2.1/src/muckrake/extract/ner/storage.py +200 -0
  33. muckrake-0.2.1/src/muckrake/id.py +3 -0
  34. muckrake-0.2.1/src/muckrake/load.py +70 -0
  35. muckrake-0.2.1/src/muckrake/logging.py +56 -0
  36. muckrake-0.2.1/src/muckrake/py.typed +0 -0
  37. muckrake-0.2.1/src/muckrake/release.py +356 -0
  38. muckrake-0.2.1/src/muckrake/runs.py +228 -0
  39. muckrake-0.2.1/src/muckrake/search.py +335 -0
  40. muckrake-0.2.1/src/muckrake/serialize.py +119 -0
  41. muckrake-0.2.1/src/muckrake/settings.py +41 -0
  42. muckrake-0.2.1/src/muckrake/store.py +117 -0
  43. muckrake-0.2.1/src/muckrake/util.py +27 -0
  44. muckrake-0.2.1/src/muckrake/utils/__init__.py +13 -0
  45. muckrake-0.2.1/src/muckrake/utils/dates.py +438 -0
  46. muckrake-0.2.1/src/muckrake/utils/gb_coh.py +36 -0
  47. muckrake-0.2.1/src/muckrake/view.py +58 -0
@@ -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,110 @@
1
+ A reusable framework for creating and storing [FollowTheMoney](https://followthemoney.tech/) entities.
2
+
3
+ > [!WARNING]
4
+ > This is a work in progress. Expect breaking changes and incomplete features.
5
+
6
+ ## Muckrake
7
+
8
+ 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/).
9
+
10
+ Run `uv run muckrake --help` for a full list of available commands.
11
+
12
+ Install Python dependencies with `uv sync`. This now includes the external `org-id` package used for structured organization identifiers.
13
+
14
+ OpenLobbying-specific code now lives in the sibling `../openlobbying/` repository. That repo owns:
15
+
16
+ - the OpenLobbying dataset crawlers
17
+ - the OpenLobbying FastAPI application
18
+ - the OpenLobbying Svelte frontend
19
+ - any project-specific FtM schema extensions
20
+ - deployment assets for the public site
21
+
22
+ ### Crawlers
23
+
24
+ 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.
25
+
26
+ To crawl a dataset, run `uv run muckrake crawl {dataset_name}`. Run `uv run muckrake list` to see available datasets.
27
+
28
+ 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.
29
+
30
+ ### AI-based NER
31
+
32
+ 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.
33
+
34
+ ```bash
35
+ # Create extraction candidates for one dataset
36
+ uv run muckrake ner-extract open_access --extractor llm --limit 50
37
+
38
+ # Review candidates in a terminal UI
39
+ uv run muckrake ner-review
40
+ ```
41
+
42
+ ### Dedupe
43
+
44
+ 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.
45
+
46
+ ```bash
47
+ # Create dedupe candidates across all datasets
48
+ uv run muckrake xref
49
+
50
+ # Review candidates in a terminal UI
51
+ uv run muckrake dedupe
52
+ ```
53
+
54
+ We also want to collapse duplicate relationship edges across datasets, especially for ORCL and PRCA. This is done automatically, no review step required.
55
+
56
+ ```bash
57
+ uv run muckrake dedupe-edges
58
+ ```
59
+
60
+ ### Loading
61
+
62
+ 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.
63
+
64
+ To load from a specific immutable crawl snapshot instead of the local workspace copy:
65
+
66
+ ```bash
67
+ uv run muckrake load gb_political_finance --run-id 123
68
+ ```
69
+
70
+ For the published site, prefer the release workflow instead of loading directly into the serving database:
71
+
72
+ ```bash
73
+ uv run muckrake release-build
74
+ uv run muckrake release-publish 1
75
+ ```
76
+
77
+
78
+ ## OpenLobbying
79
+
80
+ 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.
81
+
82
+ ## Environment setup
83
+
84
+ - Copy `.env.example` to `.env` in the repo root.
85
+ - By default `muckrake` loads the nearest `.env` from the current working directory upward. Override that with `MUCKRAKE_ENV_FILE` if needed.
86
+ - Default local database setup:
87
+ - working DB: `sqlite:///data/muckrake.db`
88
+ - published DB: same as the working DB unless `MUCKRAKE_PUBLISHED_DATABASE_URL` is set
89
+ - Required only if you want Postgres:
90
+ - `MUCKRAKE_DATABASE_URL`
91
+ - Common local settings:
92
+ - `MUCKRAKE_PUBLISHED_DATABASE_URL` for a separate published API database
93
+ - Optional local overrides:
94
+ - `MUCKRAKE_DATA_PATH`
95
+ - `MUCKRAKE_ARTIFACT_PATH`
96
+ - `MUCKRAKE_DATASET_PATHS`
97
+ - `MUCKRAKE_FTM_SCHEMA_PATHS`
98
+ - `MUCKRAKE_ENV_FILE`
99
+ - `FTM_MODEL_PATH` if you need to override the merged FollowTheMoney model entirely
100
+ - `OPENROUTER_API_KEY`, `LLM_MODEL`, `NER_LLM_PROMPT_FILE`, `LOGFIRE_TOKEN`
101
+ - Example:
102
+
103
+ ```bash
104
+ cp .env.example .env
105
+ ```
106
+
107
+ ## Consumers
108
+
109
+ - `../openlobbying/`: OpenLobbying application repo built on top of `muckrake`
110
+ - `../us-congress-lobbying/`: project-specific investigative sandbox
@@ -0,0 +1,40 @@
1
+ [project]
2
+ name = "muckrake"
3
+ version = "0.2.1"
4
+ description = "A framework for creating and storing FollowTheMoney entities, used by OpenLobbying."
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "Nicu Calcea", email = "mail@nicu.md" }
8
+ ]
9
+ requires-python = ">=3.13"
10
+ dependencies = [
11
+ "beautifulsoup4>=4.14.3",
12
+ "click>=8.3.1",
13
+ "datapatch>=1.2.4",
14
+ "followthemoney>=4.4.0",
15
+ "org-id>=0.1.0",
16
+ "lxml>=6.0.2",
17
+ "nomenklatura>=4.4.1",
18
+ "openpyxl>=3.1.5",
19
+ "pandas>=2.3.3",
20
+ "pydantic-ai-slim[google,openrouter]>=1.0.0",
21
+ "psycopg[binary]>=3.2.12",
22
+ "plyvel>=1.5.1",
23
+ "pyyaml>=6.0.3",
24
+ "requests>=2.32.5",
25
+ "xlrd>=2.0.2",
26
+ "logfire[httpx,sqlalchemy]>=4.24.0",
27
+ "odfpy>=1.4.1",
28
+ ]
29
+
30
+ [project.scripts]
31
+ muckrake = "muckrake.cli:cli"
32
+
33
+ [build-system]
34
+ requires = ["uv_build>=0.9.17,<0.10.0"]
35
+ build-backend = "uv_build"
36
+
37
+ [dependency-groups]
38
+ dev = [
39
+ "pytest>=9.0.3",
40
+ ]
@@ -0,0 +1,61 @@
1
+ import importlib.util
2
+ import os
3
+ import shutil
4
+ import tempfile
5
+ from pathlib import Path
6
+
7
+
8
+ def _iter_extension_dirs() -> list[Path]:
9
+ dirs: list[Path] = []
10
+
11
+ raw_paths = os.getenv("MUCKRAKE_FTM_SCHEMA_PATHS")
12
+ if raw_paths:
13
+ for raw_path in raw_paths.split(os.pathsep):
14
+ raw_path = raw_path.strip()
15
+ if not raw_path:
16
+ continue
17
+ path = Path(raw_path).expanduser().resolve()
18
+ if path.is_dir() and path not in dirs:
19
+ dirs.append(path)
20
+
21
+ cwd_dir = (Path.cwd() / "ftm_schema_ext").resolve()
22
+ if cwd_dir.is_dir() and cwd_dir not in dirs:
23
+ dirs.append(cwd_dir)
24
+
25
+ return dirs
26
+
27
+
28
+ def _find_followthemoney_schema_dir() -> Path | None:
29
+ spec = importlib.util.find_spec("followthemoney")
30
+ if spec is None or spec.origin is None:
31
+ return None
32
+ schema_dir = Path(spec.origin).resolve().parent / "schema"
33
+ if not schema_dir.is_dir():
34
+ return None
35
+ return schema_dir
36
+
37
+
38
+ def _configure_ftm_model_path() -> None:
39
+ if os.getenv("FTM_MODEL_PATH"):
40
+ return
41
+
42
+ schema_dir = _find_followthemoney_schema_dir()
43
+ extension_dirs = _iter_extension_dirs()
44
+ if schema_dir is None or not extension_dirs:
45
+ return
46
+
47
+ merged_dir = Path(tempfile.mkdtemp(prefix="muckrake-ftm-model-"))
48
+ shutil.copytree(schema_dir, merged_dir, dirs_exist_ok=True)
49
+ for extension_dir in extension_dirs:
50
+ for pattern in ("*.yaml", "*.yml"):
51
+ for extension in sorted(extension_dir.glob(pattern)):
52
+ shutil.copy2(extension, merged_dir / extension.name)
53
+
54
+ os.environ["FTM_MODEL_PATH"] = str(merged_dir)
55
+
56
+
57
+ _configure_ftm_model_path()
58
+
59
+
60
+ def hello() -> str:
61
+ return "Hello from muckrake!"
@@ -0,0 +1,72 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ import shutil
6
+ from dataclasses import dataclass
7
+ from pathlib import Path
8
+ from typing import Any
9
+
10
+ from muckrake.settings import ARTIFACT_PATH
11
+
12
+
13
+ @dataclass
14
+ class StoredArtifact:
15
+ storage_backend: str
16
+ storage_key: str
17
+ absolute_path: Path
18
+ sha256: str
19
+ size_bytes: int
20
+
21
+
22
+ class LocalArtifactStore:
23
+ storage_backend = "local"
24
+
25
+ def __init__(self, root: Path = ARTIFACT_PATH):
26
+ self.root = root
27
+
28
+ def resolve_path(self, storage_key: str) -> Path:
29
+ return self.root / storage_key
30
+
31
+ def exists(self, storage_key: str) -> bool:
32
+ return self.resolve_path(storage_key).exists()
33
+
34
+ def put_file(self, source_path: Path, storage_key: str) -> StoredArtifact:
35
+ target_path = self.resolve_path(storage_key)
36
+ target_path.parent.mkdir(parents=True, exist_ok=True)
37
+ shutil.copy2(source_path, target_path)
38
+ return StoredArtifact(
39
+ storage_backend=self.storage_backend,
40
+ storage_key=storage_key,
41
+ absolute_path=target_path,
42
+ sha256=file_sha256(target_path),
43
+ size_bytes=target_path.stat().st_size,
44
+ )
45
+
46
+ def put_bytes(self, data: bytes, storage_key: str) -> StoredArtifact:
47
+ target_path = self.resolve_path(storage_key)
48
+ target_path.parent.mkdir(parents=True, exist_ok=True)
49
+ target_path.write_bytes(data)
50
+ return StoredArtifact(
51
+ storage_backend=self.storage_backend,
52
+ storage_key=storage_key,
53
+ absolute_path=target_path,
54
+ sha256=file_sha256(target_path),
55
+ size_bytes=target_path.stat().st_size,
56
+ )
57
+
58
+ def put_json(self, payload: dict[str, Any], storage_key: str) -> StoredArtifact:
59
+ data = json.dumps(payload, indent=2, sort_keys=True).encode("utf-8")
60
+ return self.put_bytes(data, storage_key)
61
+
62
+
63
+ def file_sha256(path: Path) -> str:
64
+ digest = hashlib.sha256()
65
+ with path.open("rb") as fh:
66
+ for chunk in iter(lambda: fh.read(1024 * 1024), b""):
67
+ digest.update(chunk)
68
+ return digest.hexdigest()
69
+
70
+
71
+ def get_artifact_store() -> LocalArtifactStore:
72
+ return LocalArtifactStore()