You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kmftools/solarforecast/backend/fetch_ned_data.py

53 lines
1.2 KiB

import os
import requests
from dotenv import load_dotenv
from datetime import datetime, timedelta
from db import init_db, insert_forecast_records
load_dotenv(dotenv_path="../.env")
NED_API_KEY = os.getenv("NED_API_KEY")
def fetch_forecast():
url = "https://api.ned.nl/v1/utilizations"
headers = {
"X-AUTH-TOKEN": NED_API_KEY,
"Accept": "application/ld+json"
}
today = datetime.utcnow().date()
tomorrow = today + timedelta(days=1)
params = {
"point": 9,
"type": 2,
"granularity": 3,
"granularitytimezone": 1,
"classification": 2,
"activity": 1,
"validfrom[after]": today.strftime("%Y-%m-%d"),
"validfrom[strictly_before]": tomorrow.strftime("%Y-%m-%d")
}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
return response.json()
def main():
print("📡 Ophalen zonneproductievoorspelling van NED.nl")
data = fetch_forecast()
records = data.get("hydra:member", [])
print(records)
print(f"Gevonden records: {len(records)}")
init_db()
insert_forecast_records(records)
print("✅ Data opgeslagen in database")
if __name__ == "__main__":
main()