import datetime as dt import requests # Predico instance URL. Switch BASE_URL to the environment you want to use: # - Playground: https://playground.predico.inesctec.pt # - Production: https://predico.inesctec.pt BASE_URL = "https://predico.inesctec.pt" API_URL = f"{BASE_URL}/api/v1" # Authenticate via `/token` endpoint access_token = "your_access_token_here" headers = { 'Authorization': f'Bearer {access_token}', 'Accept': 'application/json' } # Challenge resource name. resource_name = "wind_farm_1" # Retrieve datetime range (last 30 days up to 3 days ahead, by default). # Adjust to match the period you actually want to inspect. now = dt.datetime.now(dt.timezone.utc).replace(minute=0, second=0, microsecond=0) start_datetime = (now - dt.timedelta(days=30)).strftime("%Y-%m-%dT%H:%M:%SZ") end_datetime = (now + dt.timedelta(days=3)).strftime("%Y-%m-%dT%H:%M:%SZ") # Get challenges target resource identifier (UUID) # (this is also available via the /market/challenge/ endpoint ) response = requests.get( url=f"{API_URL}/user/resource", params={'resource_name': resource_name}, headers=headers, timeout=30 ) # Check if the request was successful if response.status_code == 200: resources = response.json() else: print("Failed to retrieve resource data.") print(f"Status code: {response.status_code}") print(f"Response: {response.content}") exit() # Get resource ID: resource_id = resources["data"][0]["id"] print("Resource ID:", resource_id) # Download continuous forecasts data for this resource: params = { "resource": resource_id, "start_date": start_datetime, "end_date": end_datetime } next_url = f"{API_URL}/data/continuous-forecasts" dataset = [] # -- Note: This will stop once all the samples are retrieved. # -- next_url indicates the URL of the next page (pagination) to be requested) first_request = True while next_url is not None: print(f"Requesting data...\n{next_url}") # This may take a while # The first request needs `params`; subsequent `next_url` values returned # by the paginator already contain every query parameter, so we drop them. response = requests.get( url=next_url, params=params if first_request else None, headers=headers, timeout=30, ) first_request = False print("Response status code:", response.status_code) # Check if the request was successful if response.status_code != 200: print("Failed to retrieve forecasts.") print(f"Status code: {response.status_code}") print(f"Response: {response.content}") exit("Exiting...") else: dataset += response.json()["data"]["results"] next_url = response.json()["data"]["next"] # -- Note: This will stop once all the samples are retrieved. print("-"*79) print(f"Retrieved {len(dataset)} records") print("Continuous forecasts data (first 10 records preview):") print(dataset[:10])