Last active
September 13, 2018 18:49
-
-
Save mojowen/13df2865986dd6e554dced1cc1db7d3b to your computer and use it in GitHub Desktop.
Pizza to the Polls Zap Code Steps
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
""" | |
input_data = { | |
"api_key": "some-api-key", | |
"zip": "11235", # | |
"state": "NY", # State of polling place | |
"street": "510 Claremont Ave", # Address of Polling Place | |
"vicinities": "address1,address2,address3", # Comma separated list of vicinities returbed from Google Places query | |
} | |
Example Google Places Query: | |
https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=KEY&location=40.6826467,-73.9690907&rankby=distance&type=lodging | |
Note: Must geocode address into lat/lng | |
""" | |
import requests | |
URL = 'https://www.googleapis.com/civicinfo/v2/voterinfo' | |
STREET = input_data['street'] | |
STATE = input_data['state'] | |
ZIP = input_data['zip'] | |
API_KEY = input_data['api_key'] | |
def check_address(address): | |
locations = ( | |
requests.get( | |
URL, | |
params={"key": API_KEY, | |
"address": address} | |
).json() | |
.get('pollingLocations', []) | |
) | |
for location in locations: | |
address = location.get('address', {}) | |
if STREET == address.get('line1') and ZIP == address.get('zip'): | |
return True | |
return False | |
if check_address("{} {} {}".format(STREET, STATE, ZIP)): | |
return { "polling_place_found": True } | |
for address in input_data.get('vicinities', '').split(','): | |
if check_address(address): | |
return { "polling_place_found": True } | |
return { "polling_place_found": False } |
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
""" | |
input_data = { | |
"api_key": "some-api-key", | |
"results": "placeid1,placeid2", # Comma separated list of place ids returbed from Google Places query | |
} | |
Example Google Places Query: | |
https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=KEY&location=40.6826467,-73.9690907&keyword=pizza | |
Note: Must geocode address into lat/lng | |
""" | |
import requests | |
URL = 'https://maps.googleapis.com/maps/api/place/details/json' | |
API_KEY = input_data['api_key'] | |
output = { "places_websites": [] } | |
for place_id in input_data.get('results', '').split(',')[0:5]: | |
url = '?placeid={}&key={}&fields=website'.format(result, input_data['api_key']) | |
website = requests.get( | |
url, | |
params={"key": API_KEY, "placeid": place_id, "fields": "website"} | |
).json().get('result', {}).get('website') | |
if website: | |
output["places_websites"].append(website) | |
return output |
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
""" | |
input_data = { | |
"html": "<!doctype ...", | |
} | |
Example Slice Life Query: | |
https://slicelife.com/search?search_shipping_type_filter=delivery&address=40.6826467,-73.9690907 | |
Note: Can use address or lat/lng in the address query | |
""" | |
import re | |
pattern = re.compile('(?<=href=").*?(?=")') | |
output = { "slicelife_restaurants": [] } | |
html = input_data['html'] | |
if html and 'no restaurants available in your area' not in html: | |
search_results = html.split('class="search-results">')[1] | |
search_results = search_results.split('<script>')[0] | |
output['slicelife_restaurants'] = ["https://slicelife.com{}".format(store) | |
for store in | |
pattern.findall(search_results)] | |
return output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment