-
-
Notifications
You must be signed in to change notification settings - Fork 167
Lobbying data pipeline #2158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nesanders
wants to merge
11
commits into
codeforboston:main
Choose a base branch
from
nesanders:lobbying-data-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lobbying data pipeline #2158
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0348b29
initial plan
nesanders 774568e
feat: add lobbying disclosure ingestion pipeline
nesanders 0074294
feat: add Python Cloud Run scraper for lobbying disclosures
nesanders 2bcb783
refactor: remove dead TypeScript scraper code from lobbying module
nesanders df7c78e
Merge remote-tracking branch 'upstream/main' into lobbying-data-pipeline
nesanders 26150ef
feat: parser fixes for all 4 disclosure eras + GCS archiving infrastr…
nesanders b66576a
fix: Firestore write path bugs found during dev write test
nesanders 6129130
docs: update lobbying ingestion doc to reflect Python scraper and all…
nesanders 1ef618a
docs: add real Firestore examples and fix registrantId description
nesanders 5c6bcb1
refactor: replace _total_salary_ sentinel with legacyTotalCompensatio…
nesanders e9bbdcd
test: add writer unit tests and fix types.ts formatting
nesanders File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from "./types" | ||
| export { normalizeEntityName } from "./normalize" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * Entity name normalization pipeline. | ||
| * | ||
| * The SoS portal does not enforce consistent name formatting. The same client or | ||
| * registrant may appear as "Acme Corp.", "ACME CORPORATION", "Acme, Inc. d/b/a | ||
| * Acme Consulting", etc. across filings and years. | ||
| * | ||
| * The steps must be applied in the exact order | ||
| * listed here; changing the order produces different (incorrect) output. | ||
| */ | ||
|
|
||
| // Step 2: strip d/b/a trade-name suffix before any other transforms so the | ||
| // trade name doesn't bleed into the canonical form. | ||
| const DBA_RE = /\s+D\s*\/+B\s*\/+A?\s+.*|\s+DBA\s+.*/i | ||
|
|
||
| // Step 5: remove legal entity type words with whole-word matching so | ||
| // "INCORPORATED" and "CORP" are caught in addition to "LLC"/"INC". | ||
| const LEGAL_ENTITY_RE = | ||
| /\b(LLC|LLP|INC|INCORPORATED|CORPORATION|CORP|LTD|LIMITED|PC|PLLC)\b/g | ||
|
|
||
| // Step 6: remove "THE" as a whole word anywhere (not just as a leading prefix). | ||
| const THE_RE = /\bTHE\b/g | ||
|
|
||
| // Step 9: professional suffix phrases to remove wholesale. | ||
| const MISC_PHRASES = [ | ||
| "LAW OFFICE OF", | ||
| "AND ASSOCIATES", | ||
| "& ASSOCIATES", | ||
| "AND ASSOC", | ||
| "ATTORNEY AT LAW", | ||
| "ATTORNEY@LAW", | ||
| "ATTORNET AT LAW", // known portal typo | ||
| "AND PARTNERS", | ||
| "PUBLIC POLICY GROUP", | ||
| "LEGISLATIVE SERVICES", | ||
| "POLICY GROUP", | ||
| "ASSOCIATES", | ||
| "COUNSELLORS AT LAW" | ||
| ] | ||
|
|
||
| export function normalizeEntityName(raw: string | null | undefined): string { | ||
| if (!raw) return "" | ||
|
|
||
| let x = raw.toUpperCase() // Step 1: uppercase | ||
|
|
||
| x = x.replace(DBA_RE, "") // Step 2: strip d/b/a suffix | ||
|
|
||
| x = x.replace(/-/g, " ") // Step 3: hyphen → space | ||
|
|
||
| // Step 4: punctuation → space (not empty string, so ",INC" → " INC" → caught | ||
| // by step 5's whole-word removal). | ||
| for (const ch of [",", ".", "'", "‘", "’", "(", ")"]) { | ||
| x = x.split(ch).join(" ") | ||
| } | ||
|
|
||
| x = x.replace(LEGAL_ENTITY_RE, " ") // Step 5: remove legal entity type words | ||
|
|
||
| x = x.replace(THE_RE, " ") // Step 6: remove THE anywhere | ||
|
|
||
| x = x.replace(/&/g, "AND") // Step 7: ampersand → AND | ||
|
|
||
| x = x.replace("ASSICIATES", "ASSOCIATES") // Step 8: fix known portal typo | ||
|
|
||
| // Step 9: remove professional suffix phrases | ||
| for (const phrase of MISC_PHRASES) { | ||
| x = x.split(phrase).join(" ") | ||
| } | ||
|
|
||
| x = x.replace(/\s+/g, " ").trim() // Step 10: collapse whitespace | ||
|
|
||
| return x | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { | ||
| Array, | ||
| InstanceOf, | ||
| Literal, | ||
| Number, | ||
| Null, | ||
| Record, | ||
| Static, | ||
| String, | ||
| Union | ||
| } from "runtypes" | ||
| import { Timestamp } from "../firebase" | ||
|
|
||
| export type LobbyingChamber = Static<typeof LobbyingChamber> | ||
| export const LobbyingChamber = Union( | ||
| Literal("House Bill"), | ||
| Literal("Senate Bill"), | ||
| Literal("House Docket"), | ||
| Literal("Senate Docket"), | ||
| Literal("Executive"), | ||
| Literal("Other") | ||
| ) | ||
|
|
||
| export type LobbyingClient = Static<typeof LobbyingClient> | ||
| export const LobbyingClient = Record({ | ||
| clientName: String, | ||
| clientNameNorm: String, | ||
| compensation: Null.Or(Number) | ||
| }) | ||
|
|
||
| export type LobbyingRegistrant = Static<typeof LobbyingRegistrant> | ||
| export const LobbyingRegistrant = Record({ | ||
| registrantId: String, | ||
| entityName: String, | ||
| entityNameNorm: String, | ||
| year: Number, | ||
| generalCourt: Number, | ||
| regType: Union(Literal("Lobbyist"), Literal("Employer")), | ||
| clients: Array(LobbyingClient), | ||
| legacyTotalCompensation: Null.Or(Number), | ||
| disclosureUrls: Array(String), | ||
| fetchedAt: InstanceOf(Timestamp) | ||
| }) | ||
|
|
||
| export type LobbyingFiling = Static<typeof LobbyingFiling> | ||
| export const LobbyingFiling = Record({ | ||
| filingId: String, | ||
| entityName: String, | ||
| entityNameNorm: String, | ||
| clientName: String, | ||
| clientNameNorm: String, | ||
| year: Number, | ||
| generalCourt: Number, | ||
| chamber: LobbyingChamber, | ||
| // Non-null only for legislative chambers (House Bill, Senate Bill, House Docket, | ||
| // Senate Docket). For Executive and Other, no bill join should be attempted. | ||
| billId: Null.Or(String), | ||
| activityTitle: String, | ||
| position: String, | ||
| amount: Null.Or(Number), | ||
| fetchedAt: InstanceOf(Timestamp) | ||
| }) | ||
|
|
||
| /** Firestore path for lobbying registrant documents */ | ||
| export const REGISTRANTS_COLLECTION = "lobbyingRegistrants" | ||
|
|
||
| /** Firestore path for lobbying filing documents */ | ||
| export const FILINGS_COLLECTION = "lobbyingFilings" | ||
|
|
||
| /** Firestore path for the live scraper cursor document */ | ||
| export const SCRAPER_DOC = "/scrapers/lobbying" | ||
|
|
||
| /** Firestore path for the backfill cursor subcollection */ | ||
| export const BACKFILL_DOC = "/scrapers/lobbyingBackfill" | ||
| export const BACKFILL_URLS_COLLECTION = "processedUrls" | ||
|
|
||
| /** Earliest year with portal data */ | ||
| export const FIRST_LOBBYING_YEAR = 2005 | ||
|
|
||
| /** | ||
| * Sentinel clientName used for pre-2013 legacy filings where compensation is | ||
| * reported as a single total rather than broken down per client. | ||
| */ | ||
| export const LEGACY_TOTAL_CLIENT = "_total_salary_" | ||
|
|
||
| /** | ||
| * Chamber prefix map for constructing billId values that match MAPLE's Bill.id. | ||
| * Typed as a plain index signature so portal.ts can look up any LobbyingChamber | ||
| * without triggering "Property X does not exist" on the Partial. | ||
| */ | ||
| export const CHAMBER_PREFIXES: { [chamber: string]: string | undefined } = { | ||
| "House Bill": "H", | ||
| "Senate Bill": "S", | ||
| "House Docket": "HD", | ||
| "Senate Docket": "SD" | ||
| } | ||
|
|
||
| /** Canonical chamber values for legacy short-form codes found in older filings */ | ||
| export const LEGACY_CHAMBER_MAP: { [raw: string]: LobbyingChamber } = { | ||
| HB: "House Bill", | ||
| SB: "Senate Bill" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| __pycache__/ | ||
| *.pyc | ||
| *.pyo | ||
| .env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| FROM python:3.12-slim | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY requirements.txt . | ||
| RUN pip install --no-cache-dir -r requirements.txt | ||
|
|
||
| COPY normalize.py portal.py writer.py scrape.py ./ | ||
|
|
||
| # Cloud Run sets PORT; we don't use it (this is a job, not a server). | ||
| # Cloud Scheduler invokes the container via HTTP POST to /; handle it minimally. | ||
| ENV PYTHONUNBUFFERED=1 | ||
|
|
||
| # ENTRYPOINT is the fixed executable; CMD provides default args that --args overrides. | ||
| ENTRYPOINT ["python3", "scrape.py"] | ||
| CMD ["--mode", "weekly"] |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also add
__pycache/or*.pychere?