-
Notifications
You must be signed in to change notification settings - Fork 3
/
Represent.py
105 lines (88 loc) · 3.32 KB
/
Represent.py
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
# Represent!
#
# Simple example of using the Google GEO API and CIVIC API to lookup Senate and House Representatives
# Note: Does not handle all errors properly
#
# Uses: https://developers.google.com/civic-information
# https://developers.google.com/maps/documentation/geocoding/overview
#
#
# For CS160 Fall 2020
# Eric Paulos
import requests
import json
import random
# put your API key here
API_KEY = "YOUR_API_KEY_HERE"
CIVIC_URL = "https://www.googleapis.com/civicinfo/v2/representatives"
GEO_URL = "https://maps.googleapis.com/maps/api/geocode/json"
# example address to test with (i.e. Jacobs Hall)
ADDRESS = "2530 Ridge Rd, Berkeley, CA"
# crude way to set range of lat/lng for inside US – this is very imcomplete and better methods exist
LAT_MAX = 41.8
LAT_MIN = 33.8
LNG_MAX = -81.5
LNG_MIN = -116.2
def main():
print("Represent!\n")
# if you have a valid US postal address you can look up the representatives directly
Representatives(ADDRESS)
# if you have a valid zip code you can look up the representatives directly
Representatives("10011")
# you can get an address from a GEO location
gps = addressToLatLng("77836")
address = LatLngToAddress(gps[0], gps[1])
Representatives(address)
# or pick random GEO locations within the US (this is an incomplete boundry)
for i in range(1,3):
randLat = random.uniform(LAT_MIN, LAT_MAX)
randLng = random.uniform(LNG_MIN, LNG_MAX)
address = LatLngToAddress(randLat, randLng)
Representatives(address)
# Convert address to LAT/LNG
def addressToLatLng(address):
latlng = []
# replace spaces with '+'
place_url = "?address=" + address.replace(' ', '+')
full_url = GEO_URL + place_url + "&key=" + API_KEY
response = json.loads(requests.get(full_url).text)
lat = response["results"][0]["geometry"]["location"]["lat"]
lng = response["results"][0]["geometry"]["location"]["lng"]
latlng.append(lat)
latlng.append(lng)
#print("%f %f" % (lat , lng))
return latlng
# Convert LAT/LNG to postal address (if possible)
def LatLngToAddress(lat, lng):
gps_url = "?latlng={lat_val},{lng_val}".format(lat_val = lat, lng_val = lng)
full_url = GEO_URL + gps_url + "&key=" + API_KEY
response = json.loads(requests.get(full_url).text)
address = response["results"][0]["formatted_address"]
return address
# Lookup Representatives for a given US address
def Representatives(address):
# replace spaces with '+'
place_url = "?address=" + address.replace(' ', '+')
full_url = CIVIC_URL + place_url + "&key=" + API_KEY
response = json.loads(requests.get(full_url).text)
# print the city and state
print(response["normalizedInput"]["city"] + ", " + response["normalizedInput"]["state"])
offices = response["offices"]
officials = response["officials"]
for i in range(0,len(offices)):
if (offices[i]["name"] == "U.S. Senator"):
print("U.S. Senators")
index = offices[i]["officialIndices"]
for j in range(0, len(index)):
printPerson(officials[index[j]])
elif(offices[i]["name"] == "U.S. Representative"):
print("U.S. Representative")
index = offices[i]["officialIndices"]
for j in range(0, len(index)):
printPerson(officials[index[j]])
print(" ")
# Print name and other information for elected offical
def printPerson(person):
print(" " + person["name"] + " [" + person["party"][0] + "] " + person["phones"][0] + " " +person["urls"][0])
if __name__ == "__main__":
main()