-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Integrate GBIF API to display biodiversity data #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/initial-project-structure
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 = [] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (code-quality): Convert for loop into list comprehension ( |
||
| 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 | ||
|
Comment on lines
+42
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (python.django.security.injection.ssrf.ssrf-injection-requests): Data from request object is passed to a new server-side request. This could lead to a server-side request forgery (SSRF). To mitigate, ensure that schemes and hosts are validated against an allowlist, do not forward the response to the user, and ensure proper authentication and transport-layer security in the proxied request. See https://owasp.org/www-community/attacks/Server_Side_Request_Forgery to learn more about SSRF vulnerabilities. Source: opengrep |
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(debug=True) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = `<p>Error fetching data: ${error.message}</p>`; | ||
| } | ||
| } | ||
|
|
||
| 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 = `<p>Error fetching data: ${data.error}</p>`; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-document-method): User controlled data in methods like Source: opengrep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-innerhtml): User controlled data in a Source: opengrep |
||
| return; | ||
| } | ||
|
|
||
| let html = '<ul>'; | ||
| data.forEach(item => { | ||
| html += `<li><a href="${item.url}" target="_blank">${item.species}</a></li>`; | ||
| }); | ||
| html += '</ul>'; | ||
|
|
||
| gbifOccurrencesContainer.innerHTML = html; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-document-method): User controlled data in methods like Source: opengrep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-innerhtml): User controlled data in a Source: opengrep |
||
|
|
||
| } catch (error) { | ||
| gbifOccurrencesContainer.innerHTML = `<p>Error fetching data: ${error.message}</p>`; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-document-method): User controlled data in methods like Source: opengrep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-innerhtml): User controlled data in a Source: opengrep |
||
| } | ||
| } | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
security (python.requests.best-practice.use-timeout): Detected a 'requests' call without a timeout set. By default, 'requests' calls wait until the connection is closed. This means a 'requests' call without a timeout will hang the program if a response is never received. Consider setting a timeout for all 'requests'.
Source: opengrep