diff --git a/README.md b/README.md index a264c39..3025b38 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ This project is a web application designed to promote environmental protection a This application integrates with the World Bank API to fetch and display data on environmental indicators. Currently, it displays the forest area percentage for Brazil, Indonesia, and the Democratic Republic of Congo. +## GBIF API Integration + +This application also integrates with the Global Biodiversity Information Facility (GBIF) API to fetch and display species occurrence data. Users can search for the 5 most recent occurrences in any country by providing a 2-letter country code. By default, it displays the data for Togo. + ## Usage To run this project locally, you will need to have Python and Flask installed. diff --git a/eco_project/backend/app.py b/eco_project/backend/app.py index 7fdad93..de3affc 100644 --- a/eco_project/backend/app.py +++ b/eco_project/backend/app.py @@ -1,4 +1,4 @@ -from flask import Flask, jsonify +from flask import Flask, jsonify, request import requests app = Flask(__name__) @@ -36,5 +36,32 @@ def forest_area(): except requests.exceptions.RequestException as e: return jsonify({"error": str(e)}), 500 +@app.route('/api/gbif_occurrences') +def gbif_occurrences(): + # Get country code from request arguments, default to Togo (TG) + country_code = request.args.get('country', 'TG') + + # GBIF API URL for occurrences + url = f"https://api.gbif.org/v1/occurrence/search?country={country_code}&limit=5" + + try: + response = requests.get(url) + data = response.json() + + if data and data['results']: + # Clean and format the data + formatted_data = [] + for entry in data['results']: + formatted_data.append({ + 'species': entry.get('scientificName', 'N/A'), + 'url': f"https://www.gbif.org/occurrence/{entry['key']}" + }) + return jsonify(formatted_data) + else: + return jsonify({"error": "No data found for the selected criteria."}), 404 + + except requests.exceptions.RequestException as e: + return jsonify({"error": str(e)}), 500 + if __name__ == '__main__': app.run(debug=True) diff --git a/eco_project/frontend/index.html b/eco_project/frontend/index.html index 320a601..94f1a39 100644 --- a/eco_project/frontend/index.html +++ b/eco_project/frontend/index.html @@ -28,6 +28,13 @@

Development Projects

Tracking and managing sustainable development goals. Here is some data from the World Bank:

+
+

Biodiversity Search

+

Enter a 2-letter country code (e.g., TG for Togo, US for United States) to search for recent species occurrences from the Global Biodiversity Information Facility (GBIF).

+ + +
+

AI Assistant

Your personal guide to environmental protection.

diff --git a/eco_project/frontend/script.js b/eco_project/frontend/script.js index 4e14520..afdcf69 100644 --- a/eco_project/frontend/script.js +++ b/eco_project/frontend/script.js @@ -3,9 +3,14 @@ document.addEventListener('DOMContentLoaded', () => { const chatInput = document.getElementById('chat-input'); const chatWindow = document.getElementById('chat-window'); const worldBankDataContainer = document.getElementById('world-bank-data'); + const gbifOccurrencesContainer = document.getElementById('gbif-occurrences'); + const countryCodeInput = document.getElementById('country-code-input'); + const searchGbifBtn = document.getElementById('search-gbif-btn'); // Fetch and display World Bank data on page load fetchWorldBankData(); + // Fetch and display GBIF data for Togo on page load + fetchGbifData('TG'); sendBtn.addEventListener('click', () => { const userInput = chatInput.value; @@ -20,6 +25,13 @@ document.addEventListener('DOMContentLoaded', () => { } }); + searchGbifBtn.addEventListener('click', () => { + const countryCode = countryCodeInput.value.trim().toUpperCase(); + if (countryCode) { + fetchGbifData(countryCode); + } + }); + function appendMessage(message) { const messageElement = document.createElement('p'); messageElement.textContent = message; @@ -52,4 +64,30 @@ document.addEventListener('DOMContentLoaded', () => { worldBankDataContainer.innerHTML = `

Error fetching data: ${error.message}

`; } } + + async function fetchGbifData(countryCode) { + try { + const response = await fetch(`/api/gbif_occurrences?country=${countryCode}`); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + + if (data.error) { + gbifOccurrencesContainer.innerHTML = `

Error fetching data: ${data.error}

`; + return; + } + + let html = ''; + + gbifOccurrencesContainer.innerHTML = html; + + } catch (error) { + gbifOccurrencesContainer.innerHTML = `

Error fetching data: ${error.message}

`; + } + } });