Modularize nearby_schools wrapper and pull data for Boston and Buffalo.
This commit is contained in:
27
main/great_schools.py
Normal file → Executable file
27
main/great_schools.py
Normal file → Executable 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
|
||||
32
main/run.py
Normal file → Executable file
32
main/run.py
Normal file → Executable file
@@ -1,21 +1,27 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from secret import get_key
|
||||
from great_schools import get_nearby_schools
|
||||
|
||||
def get_file_contents(filename):
|
||||
""" Given a filename,
|
||||
return the contents of that file
|
||||
"""
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
# It's assumed our file contains a single line,
|
||||
# with our API key
|
||||
return f.read().strip()
|
||||
except FileNotFoundError:
|
||||
print("'%s' file not found" % filename)
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
# Get secret.
|
||||
api_key_file = '../keys/api.key'
|
||||
api_key = get_file_contents(api_key_file)
|
||||
api_key = get_key(api_key_file)
|
||||
|
||||
# Grab data for Boston.
|
||||
refresh = False
|
||||
if refresh:
|
||||
boston_nearby_schools_file = '../data/nearby_schools/boston.csv'
|
||||
boston_schools = get_nearby_schools(api_key,"42.3","-71.2","50")
|
||||
boston_df = pd.DataFrame.from_dict(boston_schools)
|
||||
boston_df.to_csv(boston_nearby_schools_file)
|
||||
|
||||
get_nearby_schools(api_key)
|
||||
# Grab data for Buffalo.
|
||||
refresh = True
|
||||
if refresh:
|
||||
buffalo_nearby_schools_file = '../data/nearby_schools/buffalo.csv'
|
||||
buffalo_schools = get_nearby_schools(api_key,"42.9625","-78.7425","50")
|
||||
buffalo_df = pd.DataFrame.from_dict(buffalo_schools)
|
||||
buffalo_df.to_csv(buffalo_nearby_schools_file)
|
||||
17
main/secret.py
Executable file
17
main/secret.py
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
def get_key(filename: str):
|
||||
"""
|
||||
Provide a path to a valid file, this will return the contents.
|
||||
Parameters:
|
||||
filename (str): path to file
|
||||
Returns:
|
||||
schools (dict): Collated response from API
|
||||
"""
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
# It's assumed our file contains a single line,
|
||||
# with our API key
|
||||
return f.read().strip()
|
||||
except FileNotFoundError:
|
||||
print("'%s' file not found" % filename)
|
||||
Reference in New Issue
Block a user