graphPaname/app/request_datasets.py

32 lines
708 B
Python

from json import dump
from requests import get
from constants import FILENAMES, URL
def format_url(dataset) -> str:
"""
Constructs the API's URL for the requested dataset
"""
link = URL.format(dataset)
return link
def save_json(data, dataset):
"""
Dumps the data into a JSON file
"""
data_dir = "data/"
with open(data_dir + FILENAMES[dataset] + ".json", "w") as f:
dump(data, f, ensure_ascii=False)
def request_dataset(dataset):
"""
Fetches the requested dataset from opendata's API
"""
url = format_url(dataset)
response = get(url)
response.raise_for_status()
data = response.json()
save_json(data=data, dataset=dataset)