Last active
January 11, 2017 20:34
-
-
Save bmorris3/34f4e6923b42ed0045daae9754d38a01 to your computer and use it in GitHub Desktop.
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Two-step querying for more complete results!\n", | |
| "\n", | |
| "1. Query Google API to convert addresses to names of MoCs\n", | |
| "\n", | |
| "2. Get detailed metadata on MoCs from [unitedstates/congress-legislators](https://github.com/unitedstates/congress-legislators)\n", | |
| "\n", | |
| "\n", | |
| "### Query google for MoCs:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 34, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Query google for MoCs by address: \n", | |
| "\n", | |
| "url = 'https://www.googleapis.com/civicinfo/v2/representatives'\n", | |
| "key = 'AIzaSyBDUwLqoWAiAS4sTfFccdaz583W060jbok'\n", | |
| "\n", | |
| "payload = {'address': \"60 Garden St, Cambridge, MA 02138\",\n", | |
| " 'includeOffices': 'True', 'levels': 'country',\n", | |
| " 'key': key}\n", | |
| "import json\n", | |
| "import urllib.parse\n", | |
| "import urllib.request\n", | |
| "\n", | |
| "data = urllib.parse.urlencode(payload)\n", | |
| "req = url + '?' + data\n", | |
| "\n", | |
| "with urllib.request.urlopen(req) as response:\n", | |
| " output = json.loads(response.read().decode('ascii'))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 35, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "['Katherine M. Clark', 'Edward J. Markey', 'Elizabeth Warren']\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# Figure out which \"officials\" returned by Google API are in House or Senate\n", | |
| "\n", | |
| "local_members_of_congress = []\n", | |
| "\n", | |
| "for official in output['officials']:\n", | |
| " official_address = (official['address'][0]['line1'] + \n", | |
| " ('' if 'line2' not in official['address'][0] else official['address'][0]['line2']))\n", | |
| " if (('senate' in official_address.lower()) or ('house' in official_address.lower() or 'sob' in official_address.lower() or\n", | |
| " 'hob' in official_address.lower()) and not ('white' in official_address.lower())):\n", | |
| " \n", | |
| " local_members_of_congress.append(official['name'])\n", | |
| "\n", | |
| "print(local_members_of_congress)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Find MoC data from the local copy of unitedstates/congress-legislators" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 36, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Get detailed data from unitedstates\n", | |
| "import yaml\n", | |
| "\n", | |
| "with open(\"legislators-current.yaml\", 'r') as stream:\n", | |
| " leg = yaml.load(stream)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 37, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Create dictionary for easy lookups by name of current data for each MoC\n", | |
| "\n", | |
| "members_of_congress = {l['name']['official_full']: l['terms'][-1] for l in leg}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 39, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Katherine M. Clark {'type': 'rep', 'office': '1415 Longworth House Office Building', 'party': 'Democrat', 'url': 'https://katherineclark.house.gov', 'state': 'MA', 'address': '1415 Longworth HOB; Washington DC 20515-2105', 'start': '2017-01-03', 'district': 5, 'end': '2019-01-03', 'phone': '202-225-2836'} \n", | |
| "\n", | |
| "Edward J. Markey {'type': 'sen', 'office': '255 Dirksen Senate Office Building', 'party': 'Democrat', 'url': 'http://www.markey.senate.gov', 'start': '2015-01-06', 'state': 'MA', 'address': '255 Dirksen Senate Office Building Washington DC 20510', 'class': 2, 'end': '2021-01-03', 'phone': '202-224-2742', 'state_rank': 'junior', 'contact_form': 'http://www.markey.senate.gov/contact'} \n", | |
| "\n", | |
| "Elizabeth Warren {'type': 'sen', 'office': '317 Hart Senate Office Building', 'party': 'Democrat', 'url': 'http://www.warren.senate.gov', 'start': '2013-01-03', 'state': 'MA', 'address': '317 Hart Senate Office Building Washington DC 20510', 'class': 1, 'end': '2019-01-03', 'rss_url': 'http://www.warren.senate.gov/rss/?p=hot_topic', 'state_rank': 'senior', 'phone': '202-224-4543', 'contact_form': 'http://www.warren.senate.gov/?p=email_senator#thisForm'} \n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "for local_moc in local_members_of_congress:\n", | |
| " print(local_moc, members_of_congress[local_moc], '\\n')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.5.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey