consier now that the API documentation is here:…
- User
consier now that the API documentation is here: https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/support/apis
Assistant
Great! Let's refine the Python code based on the official API documentation from the EU Funding & Tenders Portal. The API documentation provides detailed information on endpoints, parameters, and response structures.
Step 1: Understand the API Structure
The portal provides a REST API for open data. Key endpoints include:
- Search projects: Filter by
projectCodeoracronym. - Project details: Retrieve partners, funding, and summaries using a project ID.
Step 2: Python Code Implementation
Here's the updated Python code to interact with the EC Funding and Tenders Portal API:
import requests import json from typing import Dict, List, Optional # Base URL for the EC Funding & Tenders API BASE_URL = "https://ec.europa.eu/info/funding-tenders/opportunities/data/api/v1" def search_project_by_code_or_acronym( query: str, is_code: bool = True ) -> Optional[List[Dict]]: """ Search for projects using project code or acronym. :param query: Project code or acronym to search. :param is_code: True if query is a project code, False if acronym. :return: List of matching projects (or None if no results). """ endpoint = f"{BASE_URL}/projects" params = { "projectCode": query if is_code else None, "acronym": query if not is_code else None, "size": 10 # Limit results (adjust as needed) } try: response = requests.get(endpoint, params=params, timeout=10) response.raise_for_status() # Raise error for 4xx/5xx status data = response.json() # Check if results exist if data.get("totalElements", 0) == 0: print(f"No projects found for query: {query}") return None return data.get("content", []) # List of project summaries except requests.exceptions.RequestException as e: print(f"Search request failed: {e}") return None def get_project_details(project_id: str) -> Optional[Dict]: """ Retrieve detailed project information (partners, funding, summary). :param project_id: Unique ID of the project (from search results). :return: Detailed project data (or None if failed). """ endpoint = f"{BASE_URL}/projects/{project_id}" try: response = requests.get(endpoint, timeout=10) response.raise_for_status() data = response.json() return data except requests.exceptions.RequestException as e: print(f"Details request failed for project {project_id}: {e}") return None def extract_key_info(project_details: Dict) -> Dict: """ Extract key fields from project details. :param project_details: Raw JSON response from the API. :return: Structured data with partners, funding, and summary. """ # Extract basic info basic_info = { "project_id": project_details.get("id"), "project_code": project_details.get("projectCode"), "acronym": project_details.get("acronym"), "title": project_details.get("title", {}).get("en"), # English title "status": project_details.get("status") } # Extract funding (adjust based on API response structure) funding = { "total_amount": project_details.get("funding", {}).get("totalAmount"), "currency": project_details.get("funding", {}).get("currency") } # Extract partners (participants) partners = [] for participant in project_details.get("participants", []): partners.append({ "name": participant.get("name", {}).get("en"), "country": participant.get("country"), "role": participant.get("role") # e.g., "coordinator", "partner" }) # Extract summary (description/objective) summary = project_details.get("description", {}).get("en") or \ project_details.get("objective", {}).get("en") return { **basic_info, "funding": funding, "partners": partners, "summary": summary } if __name__ == "__main__": # Example: Search for a project by code (replace with real code/acronym) PROJECT_QUERY = "EUROPEAN-GREEN-DEAL-2021" # Example code is_code = True # Set to False if using an acronym # Step 1: Search for the project search_results = search_project_by_code_or_acronym(PROJECT_QUERY, is_code) if not search_results: exit() # Step 2: Get details for the first matching project first_project = search_results[0] project_id = first_project.get("id") project_details = get_project_details(project_id) if not project_details: exit() # Step 3: Extract and print key info key_info = extract_key_info(project_details) print("Extracted Project Information:") print(json.dumps(key_info, indent=2, ensure_ascii=False))Key Notes:
- API Endpoints: Verify the latest
BASE_URLand endpoints from the official documentation. The portal may update its API structure. - Rate Limiting: Add delays (
time.sleep()) between requests to avoid hitting rate limits. - Response Structure: The
extract_key_infofunction assumes a specific JSON structure. Adjust field names (e.g.,funding,participants) based on the actual API response. - Authentication: Some API endpoints may require an API key. Check the portal's documentation for authentication requirements.
How to Use:
- Replace
PROJECT_QUERYwith a real project code or acronym. - Set
is_codetoFalseif searching by acronym. - The code will output structured data including partners, funding, and the project summary.
This approach uses the portal's official API, ensuring reliability and compliance with their terms of service (unlike web scraping). Always review the portal's data usage policy before use.
- Search projects: Filter by
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.