Archived
1
0

Modularize nearby_schools wrapper and pull data for Boston and Buffalo.

This commit is contained in:
Shaun Setlock
2022-04-09 14:55:44 -04:00
parent 557c7103b4
commit 6a6fa834f3
6 changed files with 2201 additions and 21 deletions

27
main/great_schools.py Normal file → Executable file
View File

@@ -1,34 +1,45 @@
#!/usr/bin/env python3
import requests
import json
import os
# Helper file to facilitate calls to the greatschools.org API
# Endpoint: nearby-schools
def get_nearby_schools(key: str):
def get_nearby_schools(key: str, lat: str, lon: str, dist: str):
'''
Returns a dictionary of schools received from the nearby schools endpoint.
Parameters:
key (str): API key
lat (str): latitude
lon (str): longitude
dist (str): radius of search
Returns:
schools (dict): Collated response from API
'''
url = 'https://gs-api.greatschools.org/nearby-schools'
params = {
'lat': "42.3",
'lon': "-71.2",
'lat': lat,
'lon': lon,
'school_type': "public",
'distance': "50",
'distance': dist,
}
headers = {
"x-api-key": key
}
# Construct loop to collate pages
schools = {}
schools = []
count = 0
request_limit = 50
non_empty = True
while count <= request_limit and non_empty:
params['page'] = str(count)
r = requests.get(url=url, params=params, headers=headers).json()
print(r)
if len(r['schools']) == 0:
non_empty = False
else:
schools.update(r)
for school in r['schools']:
schools.append(school)
count = count + 1
return r
return schools