graphPaname/app/request_datasets.py

32 lines
708 B
Python
Raw Normal View History

2020-06-05 13:48:47 +02:00
from json import dump
2020-05-21 18:45:51 +02:00
from requests import get
2020-06-05 13:48:47 +02:00
from constants import FILENAMES, URL
2020-05-21 18:45:51 +02:00
2020-06-05 13:48:47 +02:00
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
"""
2020-05-27 20:45:21 +02:00
data_dir = "data/"
2020-06-05 13:48:47 +02:00
with open(data_dir + FILENAMES[dataset] + ".json", "w") as f:
2020-05-21 18:45:51 +02:00
dump(data, f, ensure_ascii=False)
2020-06-05 13:48:47 +02:00
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()
2020-06-05 13:48:47 +02:00
save_json(data=data, dataset=dataset)