Update municipal contacts

From atanasster/electionsbg · View interactive version

Update municipal contacts is an AI agent skill from the GitHub repository atanasster/electionsbg. Refresh the per-município mayor + deputy-mayor email contacts (data/officials/municipal_contacts/index.json) by scraping the iisda.government.bg "Кметове на общини" registry. Use when the daily watch report flags `iisda_mayors` as changed, when the user asks to refresh mayor contacts / municipal con

How to use this skill

Skill file

Update municipal contacts skill

<!-- pipeline-pointer:start (generated from the registry; edit scripts/pipeline/registry) -->

Pipeline: handler CODE0 runs this unattended — pnpm run pipeline:ingest --only update-municipal-contacts. The registry holds its commands, gates and publish set; this runbook covers what a script cannot do (operator steps, judgement calls, troubleshooting). When a step fails, start from the repair packet the run writes, not from the procedure below.

<!-- pipeline-pointer:end -->

Refreshes data/officials/municipal_contacts/index.json — the per-municipality mayor + deputy-mayor email contacts that power the inline mailto: icons next to each official's name on the My-Area dashboard's MyAreaGovernmentCard. Covers 258 of Bulgaria's 265 municipalities with at least one official, and ~600 deputy-mayor emails on top of the 260 município mayors. Sofia city contributes once under the synthetic SOF00 code (with the 12 city-wide deputies) and fans out to the 24 районы via the hook fallback.

Source contract:

| Field | Source | Granularity | Cadence |

|---|---|---|---|

| email, mayor, iisda_id, officials[] (role + name + email per person) | iisda.government.bg CODE4 detail pages — IDs in the contiguous block 4400..4950 | per municipality (obshtina code) | shifts on regular & partial local-election cycles, and on individual zам.-кмет appointments/dismissals |

scripts/officials/municipal_contacts/scrape_iisda.ts walks the ID range serially with a 200 ms polite delay, caches each detail page to raw_data/officials/iisda_mayors/<id>.html, filters for pages whose body contains Кмет на община AND Общинска администрация, then walks every <li class="level-1"> block on the page to pull (role label, full name, mailto: email) — one row for the mayor plus one per deputy mayor in the "Заместници" section. Each block must carry a <span class="li-sub-text-name"> and a mailto: href to be emitted; iisda's "Степен на разпоредител с бюджет" block doesn't and is skipped. Município name is matched against data/municipalities.json with two manual aliases (Добричка → DOB15, Столична община → SOF00).

When to run

| Trigger | Action |

|---|---|

| Daily watcher reports iisda.government.bg — Кметове на общини as changed | Step 1 |

| User asks to "refresh mayor contacts" / "update municipal contacts" / "rescrape iisda" | Step 1 |

| New regular-local cycle (mi*) or chmi cycle has finalized — new mayors took office | Step 1 with --force to refetch HTML |

| Fresh clone with empty contactsByObshtina | Step 1 |

The iisda registry is dormant between election cycles; routine fingerprint flips are usually individual chmi (частични избори) replacements, not bulk changes.

Step 1 — Scrape

pnpm exec tsx scripts/officials/municipal_contacts/scrape_iisda.ts          # uses cached HTML when present

Force a full re-fetch (after a regular-local cycle):

rm -rf raw_data/officials/iisda_mayors
pnpm exec tsx scripts/officials/municipal_contacts/scrape_iisda.ts          # ~2 min cold

Expected output on a routine run:

  scanned 100/551 · mayors=50 · matched=50
  scanned 200/551 · mayors=100 · matched=99
  scanned 300/551 · mayors=150 · matched=149
  ...
  scanned 550/551 · mayors=260 · matched=260

Done. Wrote .../data/officials/municipal_contacts/index.json — 260/260 mayors matched, 0 unmatched.

Step 2 — Spot-check

Cross-check the new entries against the email domains — they should align with the município's name conventions (kmet@varna.bg, obshtina@burgas.bg, etc.) — and confirm deputy-mayor coverage:

python3 -c "
import json
c = json.load(open('data/officials/municipal_contacts/index.json'))['contactsByObshtina']
samples = list(c.items())[:5]
for code, x in samples:
    deputies = len([o for o in x.get('officials', []) if o['role'] == 'deputy_mayor'])
    print(f'  {code}: {x.get(\"email\")}  (iisda_id={x.get(\"iisda_id\")}, deputies={deputies})')
deputy_total = sum(len([o for o in v.get('officials', []) if o['role']=='deputy_mayor']) for v in c.values())
with_officials = sum(1 for v in c.values() if v.get('officials'))
print(f'total: {len(c)} municipalities · {with_officials} with officials[] · {deputy_total} deputy-mayor emails')
"

Sanity checks:

If the unmatched-mayors line shows > 5, the name → obshtina-code join broke. Look at the printed "Unmatched" sample and either widen MANUAL_ALIASES in the script or extend the matching heuristics. If the deputy-mayor count drops sharply (< 400), the extractPeople() per-block parse likely broke — re-check the iisda HTML for layout changes in the <li class="level-1"> blocks.

Step 3 — Commit + bucket sync

git add data/officials/municipal_contacts/index.json
git commit -m "officials: refresh municipal mayor contacts from iisda"
pnpm run bucket:sync:dry
pnpm run bucket:sync

Step 4 — Stamp success

pnpm exec tsx scripts/stamp-ingest.ts update-municipal-contacts \
  --summary "260/260 mayors matched · 0 unmatched · ~600 deputy-mayor emails"

Only stamp on the public data-changes log when emails actually changed:

if [ -n "$(git diff --stat data/officials/municipal_contacts/)" ]; then
  pnpm exec tsx scripts/append-data-change.ts update-municipal-contacts \
    --summary "260/260 mayors matched · 0 unmatched · ~600 deputy-mayor emails" \
    --source "iisda.government.bg — Кметове на общини"
fi

Known limitations

What this skill does NOT do

File map

| Path | Purpose |

|---|---|

| scripts/officials/municipal_contacts/scrape_iisda.ts | CLI entry — walk iisda IDs, extract mayor + deputy-mayor emails via per-<li class="level-1"> block parser |

| raw_data/officials/iisda_mayors/<id>.html | cached detail-page HTML (skip-on-exist) |

| data/officials/municipal_contacts/index.json | output — contactsByObshtina map with {email, mayor, iisda_id, officials[]} per município |

| scripts/watch/sources/iisda_mayors.ts | watcher — fingerprints pagination count + first-page IDs |

| src/data/officials/useMunicipalContacts.tsx | React Query hook with Sofia район → SOF00 fallback + emailForName() lookup (exact + hyphen-stem fallback) |

| src/screens/myarea/MyAreaGovernmentCard.tsx | renders inline Mail icon (mailto: link) next to each official's name — mayor, council chair (if matched), deputy mayors, chief architect |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.