File size: 7,017 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
"""
Re-run jurisdiction discovery for specific county GEOIDs (AL by default).

Uses ``intermediate.int_jurisdiction_websites`` for homepage seeds (override rows win).
Upserts optional ``jurisdiction_website_url_overrides``-style rows into
``int_jurisdiction_websites`` when ``--apply-homepage-overrides`` is set.

Usage:
  .venv/bin/python scripts/discovery/rescrape_county_geoids.py \\
    --geoids 01009,01021,01035,01053,01077,01091,01115 --apply-homepage-overrides

  .venv/bin/python scripts/discovery/rescrape_county_geoids.py --geoids 01053 --state AL
"""
from __future__ import annotations

import argparse
import asyncio
import hashlib
import sys
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from urllib.parse import urlparse

_root = Path(__file__).resolve().parents[2]
if str(_root) not in sys.path:
    sys.path.insert(0, str(_root))

from loguru import logger

from scripts.discovery.jurisdiction_discovery_pipeline import (
    JurisdictionDiscoveryPipeline,
    ensure_scraped_tables,
    resolve_database_url,
)

# Curated AL county homepages (override discovery seed URLs).
_HOMEPAGE_OVERRIDES: Dict[str, str] = {
    "01009": "https://www.blountcountyal.gov/",
    "01021": "https://chiltoncounty.org/",
    "01035": "https://www.conecuhcounty.us/",
    "01053": "https://www.escambiacountyal.gov/",
    "01077": "https://lauderdalecountyal.gov",
    "01091": "http://www.marengocountyal.com",
    "01115": "http://www.stclairco.com",
}


def _domain(url: str) -> Optional[str]:
    try:
        return (urlparse(url).hostname or "").lower() or None
    except Exception:
        return None


def _override_record_key(jurisdiction_id: str, website_url: str) -> str:
    return f"override|{jurisdiction_id}|{hashlib.md5(website_url.encode()).hexdigest()}"


def upsert_homepage_overrides(conn, geoids: List[str], state: str) -> int:
    import psycopg2

    n = 0
    with conn.cursor() as cur:
        for geoid in geoids:
            url = _HOMEPAGE_OVERRIDES.get(geoid)
            if not url:
                logger.warning("No homepage override for geoid {}", geoid)
                continue
            cur.execute(
                """
                SELECT jurisdiction_id, name, state_code, state
                FROM bronze.bronze_jurisdictions_counties
                WHERE geoid = %s
                """,
                (geoid.zfill(5),),
            )
            row = cur.fetchone()
            if not row:
                logger.warning("Unknown county geoid {}", geoid)
                continue
            jid, name, state_code, state_name = row
            cur.execute(
                """
                DELETE FROM intermediate.int_jurisdiction_websites
                WHERE jurisdiction_id = %s AND website_source = 'override'
                """,
                (jid,),
            )
            cur.execute(
                """
                INSERT INTO intermediate.int_jurisdiction_websites (
                    website_record_key, website_source, domain_name, website_url,
                    domain_type, jurisdiction_category, organization_name,
                    agency, city, state_code, state, jurisdiction_id,
                    ingestion_date, transformed_at, ocd_id
                ) VALUES (
                    %s, 'override', %s, %s,
                    NULL, 'county', %s,
                    NULL, NULL, %s, %s, %s,
                    CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, NULL
                )
                """,
                (
                    _override_record_key(jid, url),
                    _domain(url),
                    url,
                    name,
                    state_code or state,
                    state_name,
                    jid,
                ),
            )
            n += 1
            logger.info("Override homepage {} → {}", jid, url)
    conn.commit()
    return n


def load_counties(conn, geoids: List[str], state: str) -> List[Dict[str, Any]]:
    import psycopg2

    out: List[Dict[str, Any]] = []
    with conn.cursor() as cur:
        cur.execute(
            """
            SELECT geoid, usps, name, ansicode
            FROM bronze.bronze_jurisdictions_counties
            WHERE usps = %s AND geoid = ANY(%s)
            ORDER BY geoid
            """,
            (state.upper(), geoids),
        )
        for geoid, usps_r, name, ansi in cur.fetchall():
            gid = str(geoid or "").strip().replace("-", "")
            if not gid:
                continue
            out.append(
                {
                    "GEOID": gid,
                    "name": str(name or "").strip(),
                    "state_code": str(usps_r or "").strip().upper(),
                    "type": "county",
                    "population": 0,
                    "ANSICODE": str(ansi).strip() if ansi else "",
                    "full_name": str(name or "").strip(),
                }
            )
    return out


async def run_rescrape(
    geoids: List[str],
    *,
    state: str,
    apply_homepage_overrides: bool,
    max_concurrent: int,
) -> None:
    import psycopg2

    dbu = resolve_database_url()
    if not dbu:
        raise SystemExit("No database URL configured")

    geoids_norm = [g.strip().zfill(5) for g in geoids if g.strip()]
    conn = psycopg2.connect(dbu)
    try:
        if apply_homepage_overrides:
            upsert_homepage_overrides(conn, geoids_norm, state)
        jurisdictions = load_counties(conn, geoids_norm, state)
    finally:
        conn.close()

    if not jurisdictions:
        raise SystemExit(f"No counties found for {state} geoids={geoids_norm}")

    pipeline = JurisdictionDiscoveryPipeline(
        database_url=dbu,
        max_concurrent=max_concurrent,
        incremental=False,
    )
    try:
        ensure_scraped_tables(pipeline._conn())
        logger.info("Rescraping {} AL county(ies): {}", len(jurisdictions), geoids_norm)
        await pipeline.discover_batch(jurisdictions)
    finally:
        pipeline.close()


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--geoids",
        required=True,
        help="Comma-separated 5-digit county GEOIDs, e.g. 01009,01053",
    )
    parser.add_argument("--state", default="AL", help="USPS state code (default AL)")
    parser.add_argument(
        "--apply-homepage-overrides",
        action="store_true",
        help="Insert override rows into int_jurisdiction_websites before scrape",
    )
    parser.add_argument("--max-concurrent", type=int, default=4)
    args = parser.parse_args()
    geoids = [g.strip() for g in args.geoids.split(",") if g.strip()]
    asyncio.run(
        run_rescrape(
            geoids,
            state=args.state,
            apply_homepage_overrides=args.apply_homepage_overrides,
            max_concurrent=args.max_concurrent,
        )
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())