Archived
1
0
This repository has been archived on 2025-04-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
schools/main/great_schools.py

45 lines
1.2 KiB
Python
Executable File

#!/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, 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': lat,
'lon': lon,
'school_type': "public",
'distance': dist,
}
headers = {
"x-api-key": key
}
# Construct loop to collate pages
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()
if len(r['schools']) == 0:
non_empty = False
else:
for school in r['schools']:
schools.append(school)
count = count + 1
return schools