Spaces:
Sleeping
Sleeping
Create data_fetcher.py
Browse files- data_fetcher.py +27 -0
data_fetcher.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from config import NREL_API_KEY
|
| 4 |
+
|
| 5 |
+
# Fetch data from NREL API
|
| 6 |
+
def fetch_nrel_data():
|
| 7 |
+
url = f"https://developer.nrel.gov/api/hydrogen/v1/stations.json?api_key={NREL_API_KEY}"
|
| 8 |
+
response = requests.get(url)
|
| 9 |
+
|
| 10 |
+
if response.status_code == 200:
|
| 11 |
+
return response.json()
|
| 12 |
+
|
| 13 |
+
return {"error": "Failed to fetch data from NREL API"}
|
| 14 |
+
|
| 15 |
+
# Function to clean and structure data
|
| 16 |
+
def get_hydrogen_data():
|
| 17 |
+
data = fetch_nrel_data()
|
| 18 |
+
|
| 19 |
+
if "error" in data:
|
| 20 |
+
return data
|
| 21 |
+
|
| 22 |
+
stations = data.get("fuel_stations", [])
|
| 23 |
+
|
| 24 |
+
df = pd.DataFrame(stations)
|
| 25 |
+
df = df[["station_name", "state", "status", "capacity_kg_day", "street_address", "latitude", "longitude"]]
|
| 26 |
+
|
| 27 |
+
return df
|