<jupyter_start><jupyter_text>Google Civic APIhttps://developers.google.com/civic-information/docs/using_apiNotes:* voterinfo returns a subset of documented info. I assume that Google is still populating* normalizedInput does *NOT* validate addresses<jupyter_code>import json
import os
import pprint
import requests

from IPython.display import HTML, Markdown, display
import tabulate
# get a key at https://console.developers.google.com/apis/credentials
creds_fn = os.path.expanduser("~/.credentials/google-civic-api.json")
google_api_key = json.load(open(creds_fn))["api_key"]
google_session = requests.Session()
google_session.params.update({"key": google_api_key})<jupyter_output><empty_output><jupyter_text>Elections<jupyter_code>elections_resp = google_session.get("https://www.googleapis.com/civicinfo/v2/elections")
elections_resp.raise_for_status()
elections = elections_resp.json()
elections.keys()
len(elections["elections"])
elections["elections"]
election_id = 2000<jupyter_output><empty_output><jupyter_text>VoterInfo<jupyter_code>voterinfo_resp = google_session.get("https://www.googleapis.com/civicinfo/v2/voterinfo",
                                    params={"address":"15032 Sussex St., 94131",
                                            "electionId": election_id
                                           })
voterinfo_resp.raise_for_status()
voterinfo = voterinfo_resp.json()
# response keys; responses may not have all keys
voterinfo_keys = [
    'kind', 'election', 'otherElections', 'normalizedInput', 'mailOnly',
    'pollingLocations', 'earlyVotingSites', 'dropOffLocations', 'contests', 'state' ]
def vi_markdown(vi, vik):
    d = voterinfo.get(vik, None)
    if d is None:
        t = "None"
    elif isinstance(d, list):
        t = f"list; {len(d)} elements"
    elif isinstance(d, dict):
        t = f"dict; {len(d.keys())} keys"
    else:
        t = type(d).__name__
    s = f"#### {vik} ({t})\n\n"
    if d is not None:
        s += "```\n" + pprint.pformat(d) + "\n```\n"
    return s

def vi_display(vi, vik):
    display(Markdown(vi_markdown(vi, vik)))
vi_display(voterinfo, "kind")
vi_display(voterinfo, "election")
vi_display(voterinfo, "otherElections")
vi_display(voterinfo, "normalizedInput")
vi_display(voterinfo, "mailOnly")
vi_display(voterinfo, "pollingLocations")
vi_display(voterinfo, "earlyVotingSites")
vi_display(voterinfo, "dropOffLocations")
vi_display(voterinfo, "contests")
vi_display(voterinfo, "state")<jupyter_output><empty_output><jupyter_text>Partial Queries<jupyter_code>params={"address":"1 Sussex St., 94131", "electionId": election_id}
google_session.get("https://www.googleapis.com/civicinfo/v2/voterinfo",
                   params={**params, "fields": "pollingLocations,earlyVoteSites,dropOffLocations"}).json()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13848344606621574, 'fraction_numerical': 0.008187967248131008, 'mean_word_length': 4.758196721311475, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 5, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '465411', 'license': 'Apache-2.0', 'n_tokens_mistral': 973, 'n_tokens_neox': 937, 'n_words': 167, 'path': 'misc/Google Civic API.ipynb', 'repo_name': 'reece/voter-reg-check', 'size': '2809'}