Created
January 27, 2017 20:05
-
-
Save colegleason/70f79256c8d90a1168bb1a142cf0bc85 to your computer and use it in GitHub Desktop.
A quick script to grab many of the elected officials for Allegheny County from Google's Civic Info API
This file contains 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
from apiclient.discovery import build | |
import csv | |
import os | |
service = build('civicinfo', 'v2', | |
developerKey=os.environ['API_KEY']) | |
r = service.representatives() | |
d = service.divisions() | |
def format_address(address): | |
line1 = address["line1"] + ", " | |
if "line2" in address: | |
line2 = address["line2"] + ", " | |
else: | |
line2 = "" | |
city = address["city"] | |
state = address["state"] | |
zip = address["zip"] | |
return "{0}{1}{2}, {3} {4}".format(line1, line2, city, state, zip) | |
divisions = [ | |
("ocd-division/country:us", False), | |
("ocd-division/country:us/state:pa", False), | |
("ocd-division/country:us/state:pa/cd:14", True), | |
("ocd-division/country:us/state:pa/cd:18", True), | |
("ocd-division/country:us/state:pa/cd:12", True), | |
("ocd-division/country:us/state:pa/sldu:37", True), | |
("ocd-division/country:us/state:pa/sldu:38", True), | |
("ocd-division/country:us/state:pa/sldu:42", True), | |
("ocd-division/country:us/state:pa/sldu:43", True), | |
("ocd-division/country:us/state:pa/sldu:45", True), | |
("ocd-division/country:us/state:pa/sldl:16", True), | |
("ocd-division/country:us/state:pa/sldl:19", True), | |
("ocd-division/country:us/state:pa/sldl:20", True), | |
("ocd-division/country:us/state:pa/sldl:21", True), | |
("ocd-division/country:us/state:pa/sldl:23", True), | |
("ocd-division/country:us/state:pa/sldl:24", True), | |
("ocd-division/country:us/state:pa/sldl:25", True), | |
("ocd-division/country:us/state:pa/sldl:27", True), | |
("ocd-division/country:us/state:pa/sldl:28", True), | |
("ocd-division/country:us/state:pa/sldl:30", True), | |
("ocd-division/country:us/state:pa/sldl:32", True), | |
("ocd-division/country:us/state:pa/sldl:33", True), | |
("ocd-division/country:us/state:pa/sldl:34", True), | |
("ocd-division/country:us/state:pa/sldl:35", True), | |
("ocd-division/country:us/state:pa/sldl:36", True), | |
("ocd-division/country:us/state:pa/sldl:38", True), | |
("ocd-division/country:us/state:pa/sldl:39", True), | |
("ocd-division/country:us/state:pa/sldl:40", True), | |
("ocd-division/country:us/state:pa/sldl:42", True), | |
("ocd-division/country:us/state:pa/sldl:44", True), | |
("ocd-division/country:us/state:pa/sldl:45", True), | |
("ocd-division/country:us/state:pa/sldl:46", True), | |
("ocd-division/country:us/state:pa/sldl:54", True), | |
("ocd-division/country:us/state:pa/county:allegheny", True) | |
] | |
# response = d.search( | |
# query = '"ocd-division/country:us/state:pa/county:allegheny"').execute() | |
# for div in response["results"]: | |
# divisions.append((div["ocdId"], True)) | |
csv_results = [["office", "name", "address", "phone"]] | |
for d in divisions: | |
req = r.representativeInfoByDivision(ocdId=d[0], recursive=d[1]) | |
response = req.execute() | |
if not "officials" in response: | |
continue | |
officials = response["officials"] | |
offices = response["offices"] | |
for office in offices: | |
officials_idxs = office["officialIndices"] | |
for i in officials_idxs: | |
output = [] | |
output.append(office["name"]) | |
official = officials[i] | |
output.append(official["name"]) | |
if "address" in official: | |
output.append(format_address(official["address"][0])) | |
else: | |
output.append(None) | |
if "phones" in official: | |
output.append(official["phones"][0]) | |
else: | |
output.append(None) | |
csv_results.append(output) | |
with open("output.csv", "wb") as f: | |
writer = csv.writer(f) | |
writer.writerows(csv_results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment